How to: Log Messages When the Application Starts or Shuts Down
You can use the My.Application.Log and My.Log objects to log information about events that occur in your application. This example shows how to use the My.Application.Log.WriteEntry method with the Startup and Shutdown events to write tracing information.
To access the application's event-handler code
Have a project selected in Solution Explorer. On the Project menu, choose Properties.
Click the Application tab.
Click the View Application Events button to open the Code Editor.
This opens the ApplicationEvents.vb file.
To log messages when the application starts
Have the ApplicationEvents.vb file open in the Code Editor. On the General menu, choose MyApplication Events.
On the Declarations menu, choose Startup.
The application raises the Startup event before the main application runs.
Add the My.Application.Log.WriteEntry method to the Startup event handler.
My.Application.Log.WriteEntry("Application started at " & _ My.Computer.Clock.GmtTime.ToString)
To log messages when the application shuts down
Have the ApplicationEvents.vb file open in the Code Editor. On the General menu, choose MyApplication Events.
On the Declarations menu, choose Shutdown.
The application raises the Shutdown event after the main application runs, but before it shuts down.
Add the My.Application.Log.WriteEntry method to the Shutdown event handler.
My.Application.Log.WriteEntry("Application shut down at " & _ My.Computer.Clock.GmtTime.ToString)
Example
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_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
See Also
Tasks
How to: Handle Application Events (Visual Basic)
Concepts
Working with Application Logs in Visual Basic