您可以使用 My.Application.Log
和 My.Log
物件來記錄應用程式中發生的事件相關信息。 此範例示範如何使用 My.Application.Log.WriteEntry
方法搭配 Startup
和 Shutdown
事件來寫入追蹤資訊。
存取應用程式的事件處理程式程式代碼
在 [方案總管] 中選取專案。 在 [ 專案] 功能表上,選擇 [ 屬性]。
按一下 [應用程式] 索引標籤。
按兩下 [ 檢視應用程式事件 ] 按鈕以開啟程式代碼編輯器。
這會開啟ApplicationEvents.vb檔案。
在應用程式啟動時記錄訊息
在程式代碼編輯器中開啟ApplicationEvents.vb檔案。 在 [ 一般] 功能表上,選擇 [MyApplication 事件]。
在 [ 宣告] 功能表上,選擇 [ 啟動]。
應用程式會在主要應用程式執行之前引發 Startup 事件。
將
My.Application.Log.WriteEntry
方法新增至Startup
事件處理程式。My.Application.Log.WriteEntry("Application started at " & My.Computer.Clock.GmtTime.ToString)
在應用程式關閉時記錄訊息
在程式代碼編輯器中開啟ApplicationEvents.vb檔案。 在 [ 一般] 功能表上,選擇 [MyApplication 事件]。
在 [ 宣告] 功能表上,選擇 [ 關機]。
應用程式會在主要應用程式開始運行後引發 Shutdown 事件,但在關閉之前。
將
My.Application.Log.WriteEntry
方法新增至Shutdown
事件處理程式。My.Application.Log.WriteEntry("Application shut down at " & My.Computer.Clock.GmtTime.ToString)
範例
您可以使用 [項目設計工具 ] 來存取程式代碼編輯器中的應用程式事件。 如需詳細資訊,請參閱 應用程式頁面、項目設計工具 (Visual Basic) 。
Private Sub MyApplication_Startup(
ByVal sender As Object,
ByVal e As ApplicationServices.StartupEventArgs
) Handles Me.Startup
My.Application.Log.WriteEntry("Application started at " &
My.Computer.Clock.GmtTime.ToString)
End Sub
Private Sub MyApplication_Shutdown(
ByVal sender As Object,
ByVal e As System.EventArgs
) Handles Me.Shutdown
My.Application.Log.WriteEntry("Application shut down at " &
My.Computer.Clock.GmtTime.ToString)
End Sub