Monday, August 3, 2009

Exception Handling and Logging – Using Microsoft.Practices.EntrepriceLibrary

System.Security.SecurityException: The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security."

I received this error when I was trying to log application exception details to the event log using below method. With bit of googling I understood that my app is trying to write to the Event Log using a value for "Source" that has not been registered. As the error says it is a security issue, but I was wondering what it is, because my code tries to create the source if it is not available and I am running this application with administrative privileges (my domain account is added to local administrators group).

(My dev environment is VS 2008 running on Win Server 2008)


/// <summary>
/// Logs the message to the EventLog.
/// </summary>
private static void LogEvent(string message,
System.Diagnostics.EventLogEntryType eventLogEntryType)
{
// Log available in EventLog.
const string LOG_APPLICATION = "Application";
const string LOG_SOURCE = "Project1.Log.Test";

if (!System.Diagnostics.EventLog.SourceExists(LOG_SOURCE))
{
System.Diagnostics.EventLog.CreateEventSource(LOG_SOURCE, LOG_APPLICATION);
}

System.Diagnostics.EventLog.WriteEntry(LOG_SOURCE,message,eventLogEntryType);
}

So I tried executing "EventSourceInstaller.exe" again from my BIN directory and I noticed the same error.


To overcome this issue I had to run EventSourceInstaller with administrator account of the server and now all working fine. I guess this has something to do with Win Server 2008 but not with Win Server 2003. Here are the basic steps I had to follow for the purpose of logging with Enterprise Library.

  • Add references to the enterprise library dlls
  • Add necessary configuration entries to the Web.Config
  • Run 'EventSourceInstaller.exe' from application bin directory (With administrator account) – Make sure source value is same in Web.Config and EventSourceInstaller.exe.config are same
  • Use System.Diagnostics.EventLog.WriteEntry(…..) for logging