MrMistreater

Logging exceptions to event log

May 23rd, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.67 KB | None | 0 0
  1. public static void LogException(string ErrorDescription)
  2. {
  3.     // The name of our log in the event logs
  4.     string Log = “AspNetError”;
  5.  
  6.     // Check to see fi the log for AspNetError exists on the machine
  7.     //          If not, create it
  8.  
  9.     if ((!(EventLog.SourceExists(Log))))
  10.     {
  11.         EventLog.CreateEventSource(Log, Log);
  12.     }
  13.  
  14.     // Now insert your exception information into the AspNetError event log
  15.     EventLog logEntry = new EventLog();
  16.     logEntry.Source = Log;
  17.     logEntry.WriteEntry(ErrorDescription, EventLogEntryType.Error);
  18. }
  19.  
  20. // Usage
  21. try
  22. {
  23.     ThrowException();
  24. }
  25. catch (Exception exp)
  26. {
  27.     LogException(exp.ToString());
  28.     throw;  // or whatever you want to do with it
  29. }
Advertisement
Add Comment
Please, Sign In to add comment