Categories
Programming

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
((ISupportInitialize)(eventLog)).BeginInit();
if (!EventLog.SourceExists(eventLog.Source))
{
    EventLog.CreateEventSource(eventLog.Source, eventLog.Log);
}

((ISupportInitialize)(eventLog)).EndInit();

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