Posted By

Saikou on 12/03/07


Tagged


Versions (?)

Exception Logging to the Event Log


 / Published in: Visual Basic
 

A helper class which adds the ability to log exceptions to the Windows event log, including the class and method in which they occurred, to a class that derives from it. The event source name defaults to the application title, but can be overridden by passing a string to the constructor (i.e. MyBase.New("MyApp") in the derived class).

e.g.

Public Class Test Inherits ExceptionLogger

Public Sub Test()
    Try
        Throw New ApplicationException("Test exception", New Exception("Test inner exception"))
    Catch ex As Exception
        LogException(ex)
    End Try
End Sub

End Class

  1. Public MustInherit Class ExceptionLogger
  2. Protected mstrEventSource As String
  3.  
  4. Public Sub WriteToEventLog(ByVal strMessage As String, ByVal enmType As EventLogEntryType)
  5. Try
  6. If (Not EventLog.SourceExists(mstrEventSource)) Then
  7. EventLog.CreateEventSource(mstrEventSource, "Application")
  8. End If
  9.  
  10. EventLog.WriteEntry(mstrEventSource, strMessage, enmType)
  11. Catch ex As Exception
  12. End Try
  13. End Sub
  14.  
  15. Public Sub LogException(ByVal objException As Exception, Optional ByVal strClassName As String = "", Optional ByVal strMethodName As String = "")
  16. Dim objStackFrame As StackFrame = New System.Diagnostics.StackFrame(1)
  17. Dim intLineNumber As Integer = objStackFrame.GetFileLineNumber()
  18. Dim strFile As String = objStackFrame.GetFileName
  19.  
  20. ' If no class/method information was passed in, get the information from the call stack
  21. If (strClassName = "" And strMethodName = "") Then
  22. Dim objMethod As System.Reflection.MethodBase = objStackFrame.GetMethod()
  23. strClassName = objMethod.ReflectedType.FullName
  24. strMethodName = objMethod.Name
  25. End If
  26.  
  27. Dim strMessage As String = "Exception Details:" & ControlChars.CrLf
  28. If (strClassName <> "") Then
  29. strMessage = strMessage & "Class:" & ControlChars.Tab & strClassName & ControlChars.CrLf
  30. End If
  31. If (strMethodName <> "") Then
  32. strMessage = strMessage & "Method:" & ControlChars.Tab & strMethodName & ControlChars.CrLf
  33. End If
  34. If (intLineNumber <> 0) Then
  35. strMessage = strMessage & "Line:" & ControlChars.Tab & intLineNumber & ControlChars.CrLf
  36. End If
  37. If (strFile <> "") Then
  38. strMessage = strMessage & "File:" & ControlChars.Tab & strFile & ControlChars.CrLf
  39. End If
  40. strMessage = strMessage & "Details:" & ControlChars.Tab & objException.Message & ControlChars.CrLf
  41.  
  42. ' Walk through all innerExceptions, adding their message to the combined message
  43. If (Not objException.InnerException Is Nothing) Then
  44. strMessage = strMessage & ControlChars.CrLf & "Inner exceptions:" & ControlChars.CrLf
  45. Dim objExceptionWalker As Exception = objException
  46. While (Not objExceptionWalker.InnerException Is Nothing)
  47. objExceptionWalker = objExceptionWalker.InnerException
  48. strMessage = strMessage & ControlChars.CrLf & "Message:" & ControlChars.CrLf
  49. strMessage = strMessage & objExceptionWalker.Message & ControlChars.CrLf
  50. End While
  51. End If
  52.  
  53. WriteToEventLog(strMessage, EventLogEntryType.Error)
  54. End Sub
  55.  
  56. Public Sub New()
  57. mstrEventSource = My.Application.Info.Title
  58. End Sub
  59.  
  60. Public Sub New(ByVal strEventSource As String)
  61. mstrEventSource = strEventSource
  62. End Sub
  63. End Class

Report this snippet  

You need to login to post a comment.