Sdílet prostřednictvím


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. You must then write code to record entries to the log whenever an action you're interested in occurs.

Poznámka

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

  • Set the AutoLog property for your component to true.

    Poznámka

    By default, this property is set to true. You do not need to set this explicitly unless you are building more complex processing, such as evaluating a condition and then setting the AutoLog property based on the result of that condition.

To disable event logging for your service

  • Set the AutoLog property for your component to false.

    Me.AutoLog = False
    
            this.AutoLog = false;
    

To set up logging to a custom log

  1. Set the AutoLog property to false.

    Poznámka

    You must set AutoLog to false in order to use a custom log.

  2. Set up an instance of an EventLog component in your Windows Service application.

  3. 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.

  4. Set the Source property on the EventLog component instance to the source string you created in step 3.

  5. Write your entries by accessing the WriteEntry method on the EventLog component instance.

    The following code shows how to set up logging to a custom log.

    Poznámka

    In this code example, an instance of an EventLog component is named eventLog1 (EventLog1 in Visual Basic). If you created an instance with another name in step 2, change the code accordingly.

    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
    
        public UserService2()
        {
            // Turn off autologging
            this.AutoLog = false;
            // create an event source, specifying the name of a log that
            // does not currently exist to create a new, custom log
            if (!System.Diagnostics.EventLog.SourceExists("MySource")) 
            {        
                    System.Diagnostics.EventLog.CreateEventSource(
                        "MySource","MyLog");
            }
            // configure the event log instance to use this source name
            eventLog1.Source = "MySource";
        }
    
    
    ...
    
    
    
        protected override void OnStart(string[] args)
        {
            // write an entry to the log
            eventLog1.WriteEntry("In OnStart.");
        }
    

See Also

Concepts

Introduction to Windows Service Applications