Skip to content

Instantly share code, notes, and snippets.

@treytomes
Created February 1, 2016 14:54
Show Gist options
  • Save treytomes/92d812c401cfe28bfc3c to your computer and use it in GitHub Desktop.
Save treytomes/92d812c401cfe28bfc3c to your computer and use it in GitHub Desktop.
Example of how to write to the Windows Event Log.
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