Created
February 1, 2016 14:54
-
-
Save treytomes/92d812c401cfe28bfc3c to your computer and use it in GitHub Desktop.
Example of how to write to the Windows Event Log.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
using System.Reflection; | |
public class EventLogger | |
{ | |
#region Constants | |
private const string DEFAULT_TYPE = "Application"; | |
#endregion | |
#region Constructors | |
public EventLogger(string type, string source) | |
{ | |
LogType = type; | |
LogSource = source; | |
if (!EventLog.SourceExists(LogSource)) | |
{ | |
EventLog.CreateEventSource(LogSource, LogType); | |
} | |
} | |
public EventLogger(string source) | |
: this(DEFAULT_TYPE, source) | |
{ | |
} | |
public EventLogger() | |
: this(DEFAULT_TYPE, Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyTitleAttribute>().Title) | |
{ | |
} | |
#endregion | |
#region Properties | |
public string LogType { get; private set; } | |
public string LogSource { get; private set; } | |
#endregion | |
#region Methods | |
public void Log(EventLogEntryType level, string message) | |
{ | |
EventLog.WriteEntry(LogSource, message, level); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment