Writing to Event Log in ASP.NET application

 

Writing to Event Log is not simple, but very simple:

 

// Create an EventLog instance and assign its source.

EventLog myLog = new EventLog();

myLog.Source = "MySource";

 

// Write an informational entry to the event log.   

myLog.WriteEntry("Writing to event log.");

 

EventLog is in System.Diagnostics namespase.

 

Before using the source of the EventLog, you have to create it. But if you use the code like this:

//Create Event Log if It Doesn't Exist

if (! EventLog.SourceExists("MySource"))

      EventLog.CreateEventSource("MySource", "MySource");

 

you will get the error:

System.Security.SecurityException: Requested registry access is not allowed.

 

Instead, go to registry editor (regedit), locate key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application and create a new key with the source name