Created
June 4, 2012 13:03
-
-
Save Dynyx/2868234 to your computer and use it in GitHub Desktop.
Log exceptions to the 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
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