Steve Fenton

Logging to Windows Event Log

This article is really just one of those snippets of code that you get bored or re-writing again and again! So I have left it here for my future self.

var eventLog = new System.Diagnostics.EventLog();
eventLog.Source = "My Awesome Windows Service";
eventLog.Log = "Application";

// Make sure it knows about our event source
if (!EventLog.SourceExists(eventLog.Source))
    EventLog.CreateEventSource(eventLog.Source, eventLog.Log);


// You can now write to the event log
eventLog.WriteEntry("Error message goes here", EventLogEntryType.Error);

Written by Steve Fenton on