InvalidOperationException: Cannot open log for source {0}. You may not have write access.
Error Source: Enterprise Library Logging
Enterprise Library has been a great tool from the beginning. It allows me to focus on development to make my company more money while allowing Microsoft to figure out new ways to hit the database, catch exceptions, log anything and do encryption. This is how programming should be. That is why I love the framework so I can just be creative in how I use it.
Yesterday while enabling our site to use the logging in the Enterprise Library, I received the below error… actually to be more precise, I got the below error.
[InvalidOperationException: Cannot open log for source {0}. You may not have write access.]
System.Diagnostics.EventLog.OpenForWrite() +367
System.Diagnostics.EventLog.WriteEvent(Int32 eventID, Int16 category, EventLogEntryType type, String[] strings, Byte[] rawData) +280
System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) +465
System.Diagnostics.EventLog.WriteEntry(String source, String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) +68
System.Diagnostics.EventLog.WriteEntry(String source, String message, EventLogEntryType type, Int32 eventID, Int16 category) +21
System.Diagnostics.EventLog.WriteEntry(String source, String message, EventLogEntryType type, Int32 eventID) +15
System.Diagnostics.EventLog.WriteEntry(String source, String message, EventLogEntryType type) +11
Microsoft.Practices.EnterpriseLibrary.Logging.Distributor.DistributorEventLogger.WriteToLog(Exception exception, Severity severity) +143
Microsoft.Practices.EnterpriseLibrary.Logging.Distributor.DistributorEvents.LogProcessLogException(LogEntry logEntry, Exception ex) +412
Microsoft.Practices.EnterpriseLibrary.Logging.Distributor.LogDistributor.ProcessLog(LogEntry log) +72
Microsoft.Practices.EnterpriseLibrary.Logging.Distributor.InProcLogDistributionStrategy.SendLog(LogEntry logEntry) +10
Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter.SendMessage(LogEntry logEntry) +97
Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter.Write(LogEntry log) +126
Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(LogEntry log) +20
Matt.Common.Logging.Log.LogException(String message)
Matt.Web.PageController.cwc.Page_Load(Object sender, EventArgs e)
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750
Seems that the enterprise library loves to hit the event log. The nice part about this is you can turn it off, which I had already done… Why was I still getting this error??? Being the coder I am I went back to the changes that I had made. I am writing to a text file and a DB. I first made sure that I had a fresh compile of the EL. I then made sure that instrumentation was turned off and checked permissions on the text file and DB.
Still, I was pulling my hair out and spinning my wheels. I was calling the people on the EL discussions liars and got pretty high and mighty. I knew I shouldn’t have gone there, I was wrong… well, sort of. The problem WAS my DB connection. The user didn’t have permissions and because of this, I was throwing this error.
The Reason:
This one got me fired up. By default the EL logs to the EventViewer when an error occurs. Why? That is how it was designed is the answer I get. Well, if it were designed that way, then it should be able to give me a DB error back, and I consider this a bug. We don’t allow writing to the EventLog for errors. It is too problematic in our environment. I have a config file for this and should not be held hostage to the enviroment.
As to the jackass who said “it is better this way and if you don’t like it rewrite it”, bite me. You obviously come from a land where everything is the same and all businesses do it your way. Not true in this case, and no it isn’t a better way to handle this, just a good way for you to.
Word,
Matt
