如何:记录关于服务的信息

更新:2007 年 11 月

默认情况下,所有 Windows 服务项目都具有与应用程序事件日志进行交互并向其中写入信息和异常的功能。使用 AutoLog 属性指出是否希望应用程序具有此功能。默认情况下,用 Windows 服务项目模板创建的所有服务的记录都是打开的。可以使用静态形式的 EventLog 类将服务信息写入日志,而无需创建 EventLog 组件的实例或手动注册源。

当记录打开时,服务的安装程序自动在安装了服务的计算机上的 Application 日志中,将项目中的每项服务注册为一个有效的事件源。该服务在服务每次启动、停止、暂停、继续、安装和卸载时记录信息。它还记录发生的所有失败。当使用默认行为时,不需写任何代码以便将项写入日志;服务自动为您处理此任务。

如果要向事件日志而不是 Application 日志中写入,则必须将 AutoLog 属性设置为 false,在服务代码内创建自定义事件日志,并将服务注册为该日志的有效项源。有关更多信息,请参见如何:创建和移除自定义事件日志。然后必须写代码,以在感兴趣的操作发生时将项记入日志。

f6567h1s.alert_note(zh-cn,VS.90).gif说明:

如果使用一个自定义事件日志并配置服务应用程序写入该日志,则在代码中设置服务的 ServiceName 属性之前,一定不要尝试访问该事件日志。事件日志需要此属性的值将服务注册为一个有效的事件源。

为服务启用默认事件记录

  • 将组件的 AutoLog 属性设置为 true。

    f6567h1s.alert_note(zh-cn,VS.90).gif说明:

    默认情况下,此属性设置为 true。不需要显式设置此属性,除非正在生成更复杂的处理,如计算一个条件然后根据该条件的结果设置 AutoLog 属性。

为服务禁用事件记录

  • 将组件的 AutoLog 属性设置为 false。

    Me.AutoLog = False
    
         this.AutoLog = false;
    
         this.set_AutoLog(false);
    

将记录设置为自定义日志

  1. AutoLog 属性设置为 false。

    f6567h1s.alert_note(zh-cn,VS.90).gif说明:

    为了使用自定义日志,必须将 AutoLog 设置为 false。

  2. 在 Windows 服务应用程序中设置 EventLog 组件的一个实例。有关更多信息,请参见如何:创建 EventLog 组件实例

  3. 通过调用 CreateEventSource 方法,并指定源字符串和要创建的日志文件的名称,创建一个自定义日志。

  4. EventLog 组件实例上的 Source 属性设置为在第 3 步中创建的源字符串。

  5. 通过访问 EventLog 组件实例上的 WriteEntry 方法来编写项。

    下面的代码演示如何将记录设置为自定义日志。

    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.");
        }
    
     public UserService2()
        {
            // Turn off autologging
            this.set_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.set_Source("MySource");
        }
    
    
    ...
    
    
    
        protected void OnStart(System.String[] args)
        {
            // write an entry to the log
            eventLog1.WriteEntry("In OnStart.");
        }
    

请参见

任务

如何:创建和移除自定义事件日志

如何:创建 EventLog 组件实例

概念

Windows 服务应用程序介绍