Skip to content

Instantly share code, notes, and snippets.

@Dynyx
Created June 4, 2012 13:03
Show Gist options
  • Save Dynyx/2868234 to your computer and use it in GitHub Desktop.
Save Dynyx/2868234 to your computer and use it in GitHub Desktop.
Log exceptions to the event log
public static void LogException(string ErrorDescription)
{
// The name of our log in the event logs
string Log = “AspNetError”;
// Check to see fi the log for AspNetError exists on the machine
// If not, create it
if ((!(EventLog.SourceExists(Log))))
{
EventLog.CreateEventSource(Log, Log);
}
// Now insert your exception information into the AspNetError event log
EventLog logEntry = new EventLog();
logEntry.Source = Log;
logEntry.WriteEntry(ErrorDescription, EventLogEntryType.Error);
}
// Usage
try
{
ThrowException();
}
catch (Exception exp)
{
LogException(exp.ToString());
throw; // or whatever you want to do with it
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment