How to: Log Exceptions in Visual Basic

You can use the My.Application.Log and My.Log objects to log information about exceptions that occur in your application. These examples show how to use the My.Application.Log.WriteException method to log exceptions that you catch explicitly and exceptions that are unhandled.

For logging tracing information, use the My.Application.Log.WriteEntry method. For more information, see WriteEntry Method (My.Application.Log and My.Log).

To log a handled exception

  1. Create the method that will generate the exception information.

    Public Sub ExceptionLogTest(ByVal fileName As String)
    End Sub
    
  2. Use a Try...Catch block to catch the exception.

    Try 
    Catch ex As Exception
    End Try
    
  3. Put the code that could generate an exception in the Try block.

    Uncomment the Dim and MsgBox lines to cause a NullReferenceException exception.

    ' Code that might generate an exception goes here. 
    ' For example: 
    '    Dim x As Object 
    '    MsgBox(x.ToString)
    
  4. In the Catch block, use the My.Application.Log.WriteException method to write the exception information.

    My.Application.Log.WriteException(ex, _
        TraceEventType.Error, _
        "Exception in ExceptionLogTest " & _
        "with argument " & fileName & ".")
    

To log an unhandled exception

  1. Have a project selected in Solution Explorer. On the Project menu, choose Properties.

  2. Click the Application tab.

  3. Click the View Application Events button to open the Code Editor.

    This opens the ApplicationEvents.vb file.

  4. Have the ApplicationEvents.vb file open in the Code Editor. On the General menu, choose MyApplication Events.

  5. On the Declarations menu, choose UnhandledException.

    The application raises the UnhandledException event before the main application runs.

  6. Add the My.Application.Log.WriteException method to the UnhandledException event handler.

    My.Application.Log.WriteException(e.Exception, _
        TraceEventType.Critical, _
        "Application shut down at " & _
        My.Computer.Clock.GmtTime.ToString)
    

Example

This example shows the complete code for logging a handled exception.

Public Sub ExceptionLogTest(ByVal fileName As String)
    Try 
        ' Code that might generate an exception goes here. 
        ' For example: 
        '    Dim x As Object 
        '    MsgBox(x.ToString) 
    Catch ex As Exception
        My.Application.Log.WriteException(ex, _
            TraceEventType.Error, _
            "Exception in ExceptionLogTest " & _
            "with argument " & fileName & ".")
    End Try 
End Sub

The next example shows the complete code for logging an unhandled exception. You can use the Project Designer to access the application events in the Code Editor. For more information, see How to: Handle Application Events (Visual Basic).

Private Sub MyApplication_UnhandledException( _
    ByVal sender As Object, _
    ByVal e As ApplicationServices.UnhandledExceptionEventArgs _
) Handles Me.UnhandledException
    My.Application.Log.WriteException(e.Exception, _
        TraceEventType.Critical, _
        "Application shut down at " & _
        My.Computer.Clock.GmtTime.ToString)
End Sub

See Also

Tasks

How to: Write Log Messages

Walkthrough: Determining Where My.Application.Log Writes Information

Walkthrough: Changing Where My.Application.Log Writes Information

Concepts

Working with Application Logs in Visual Basic

Reference

My.Application.Log Object

My.Log Object

WriteEntry Method (My.Application.Log and My.Log)

WriteException Method (My.Application.Log and My.Log)