How to: Log Information About Services
By default, all Windows Service projects have the ability to interact with the Application event log and write information and exceptions to it. You use the AutoLog property to indicate whether you want this functionality in your application. By default, logging is turned on for any service you create with the Windows Service project template. You can use a static form of the EventLog class to write service information to a log without having to create an instance of an EventLog component or manually register a source.
The installer for your service automatically registers each service in your project as a valid source of events with the Application log on the computer where the service is installed, when logging is turned on. The service logs information each time the service is started, stopped, paused, resumed, installed, or uninstalled. It also logs any failures that occur. You do not need to write any code to write entries to the log when using the default behavior; the service handles this for you automatically.
If you want to write to an event log other than the Application log, you must set the AutoLog property to false, create your own custom event log within your services code, and register your service as a valid source of entries for that log. For more information, see How to: Create and Remove Custom Event Logs. You must then write code to record entries to the log whenever an action you're interested in occurs.
If you use a custom event log and configure your service application to write to it, you must not attempt to access the event log before setting the service's ServiceName property in your code. The event log needs this property's value to register your service as a valid source of events.
To enable default event logging for your service
To disable event logging for your service
To set up logging to a custom log
Set the AutoLog property to false.
You must set AutoLog to false in order to use a custom log.
Create a custom log by calling the CreateEventSource method and specifying the source string and the name of the log file you want to create.
The following code shows how to set up logging to a custom log.
Public Sub New() ' Turn off autologging Me.AutoLog = False ' Create a new event source and specify a log name that ' does not exist to create a custom log If Not System.Diagnostics.EventLog.SourceExists("MySource") Then System.Diagnostics.EventLog.CreateEventSource("MySource", _ "MyLog") End If ' Configure the event log instance to use this source name EventLog1.Source = "MySource" End Sub ... Protected Overrides Sub OnStart(ByVal args() As String) ' Write an entry to the log you've created. EventLog1.WriteEntry("In Onstart.") End Sub