Skip to content

Instantly share code, notes, and snippets.

@ChrisMoney
Created April 20, 2017 18:31
Show Gist options
  • Save ChrisMoney/ff8cd8dfdd8fb3af02c271997ce61393 to your computer and use it in GitHub Desktop.
Save ChrisMoney/ff8cd8dfdd8fb3af02c271997ce61393 to your computer and use it in GitHub Desktop.
Log Error
// define function
public static void WriteEventLog(string Error)
{
// Create the source, if it does not already exist.
if (!System.Diagnostics.EventLog.SourceExists("FEC Events"))
{
//An event log source should not be created and immediately used.
//There is a latency time to enable the source, it should be created
//prior to executing the application that uses the source.
//Execute this sample a second time to use the new source.
System.Diagnostics.EventLog.CreateEventSource("FEC Events", "FEC Events");
// The source is created. Exit the application to allow it to be registered.
return;
}
// Create an EventLog instance and assign its source.
System.Diagnostics.EventLog myLog = new System.Diagnostics.EventLog();
myLog.Source = "FEC Events";
// Write an informational entry to the event log.
myLog.WriteEntry(Error, System.Diagnostics.EventLogEntryType.Error);
}
// Call function
try { // code here }
catch (Exception ex)
{
WriteEventLog(ex.Message);
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment