Monday, April 23, 2007

Writing into Event Log from an ASP.NET application

If you try to write an entry to events log form an ASP.NET application is probably that you find an exception like this "The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.". The reason is that the ASP.NET process cannot find the source of the entry. You have to create it manually because, by default, the ASP.NET process, cannot do it programmatically. To create it manually you have to add a new entry in the registry (regedit.exe) with the name of your source (in the example TEST under Application log) in the following path "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application". Then, with the following code, you can write your entries.

string sSource;
string sEvent;
sSource = "TEST";
sLog = "Application";
sEvent = "Sample Event";
EventLog.WriteEntry(sSource, sEvent);

Related Links: (example)