Logging Events in a Windows Service Program

First, MSDN is your friend. Make sure you check out the link, as there are some potential gotchas worth knowing.

Essentially, you create an EventLog object:

this.ServiceName = "MyService";
this.EventLog = new System.Diagnostics.EventLog();
this.EventLog.Source = this.ServiceName;
this.EventLog.Log = "Application";

You also need to create a source, if the above source doesn’t exist:

if (!EventLog.SourceExists(this.EventLog.Source))
    EventLog.CreateEventSource(this.EventLog.Source, this.EventLog.Log);

and then simply use it:

this.EventLog.WriteEntry("My Eventlog message.", EventLogEntryType.Information);

it’s actually pretty simple.

Leave a Comment