如何:在 Visual Basic 中記錄例外狀況How to: Log Exceptions in Visual Basic

您可以使用 My.Application.LogMy.Log 物件來記錄應用程式中發生之例外狀況的相關資訊。You can use the My.Application.Log and My.Log objects to log information about exceptions that occur in your application. 下列範例示範如何使用 My.Application.Log.WriteException 方法,以記錄您明確攔截到的例外狀況和未處理的例外狀況。These examples show how to use the My.Application.Log.WriteException method to log exceptions that you catch explicitly and exceptions that are unhandled.

如需記錄追蹤資訊,請使用 My.Application.Log.WriteEntry 方法。For logging tracing information, use the My.Application.Log.WriteEntry method. 如需詳細資訊,請參閱<WriteEntryFor more information, see WriteEntry

記錄已處理的例外狀況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. 使用 Try...Catch 區塊來攔截例外狀況。Use a Try...Catch block to catch the exception.

    Try
    Catch ex As Exception
    End Try
    
  3. 將可產生例外狀況的程式碼放在 Try 區塊中。Put the code that could generate an exception in the Try block.

    取消 DimMsgBox 行的註解,造成 NullReferenceException 例外狀況。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. Catch 區塊中,使用 My.Application.Log.WriteException 方法來寫入例外狀況資訊。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 & ".")
    

    下列範例顯示用於記錄已處理例外狀況的完整程式碼。The following 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
    

記錄未處理的例外狀況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.

    這會開啟 ApplicationEvents.vb 檔案。This opens the ApplicationEvents.vb file.

  4. 在 [程式碼編輯器] 中開啟 ApplicationEvents.vb 檔案。Have the ApplicationEvents.vb file open in the Code Editor. 在 [一般] **** 功能表上,選擇 [MyApplication 事件] ****。On the General menu, choose MyApplication Events.

  5. 在 [宣告]**** 功能表上,選擇 [未處理的例外狀況]****。On the Declarations menu, choose UnhandledException.

    應用程式在主應用程式執行之前,引發 UnhandledException 事件。The application raises the UnhandledException event before the main application runs.

  6. My.Application.Log.WriteException 方法加入 UnhandledException 事件處理常式。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)
    

    下列範例顯示用於記錄未處理例外狀況的完整程式碼。The following example shows the complete code for logging an unhandled exception.

    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