Home >

Add an entry to event log in C#

4. March 2011

The Eventlog is a good place to leave some log messages. Here is an example how to do this:

if (!EventLog.SourceExists("TestCategory")) 
EventLog.CreateEventSource("TestCategory", "TestLog"); 
EventLog evtLog = new EventLog(); 
evtLog.Source = "TestCategory"; 
evtLog.WriteEntry("This is a test", EventLogEntryType.Information); 

First of all there is a check if the category exists. If not it will be created. Then we create an object of the typ EventLog. We set the given catetory. After that you can use the method WriteEntry to write a new entry to the eventlog.

If you want to use this with ASP.NET you have to do some additional steps. There is a registry key where the ASPNET-User and/or the user of the application pool must have the necessary rights:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog

You have to set the following rights:

  1. Query Value
  2. Set Value
  3. Create Subkey
  4. Read Subkey
  5. Notifications
  6. Read

After setting this rights it should be possible to create eventlog entries using ASP.NET

,

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading