Not logged in - Login

Writing to the Windows Event Log

The following code snippet is an example of how to write to the Windows Event Log using C#.

using System.Diagnostics;

// determine if the event source exists, if not, create it.
if (!EventLog.SourceExists("MySource"))
   EventLog.CreateEventSource("MySource", "MyLogName");

// set the event type & category
EventLogEntryType type = EventLogEntryType.Information;
short category = 0; // application specific category

// create an instance of an EventLog object.
using (System.Diagnostics.EventLog appLog = new System.Diagnostics.EventLog("MyLogName"))
{
   // set the event log source name
   appLog.Source = "MySource";
   // write the event entry
   appLog.WriteEntry("My event message", type, 0, category);
}