【问题标题】:How to create a custom event log using C#如何使用 C# 创建自定义事件日志
【发布时间】:2012-02-03 23:02:40
【问题描述】:

我创建了一个 Windows 服务。我创建了一个事件日志。

public Service1()
{
        InitializeComponent();
        this.ServiceName = ConfigurationManager.AppSettings.Get("ServiceName");

        string sourceName = ConfigurationManager.AppSettings.Get("ServiceName");
        string logName = ConfigurationManager.AppSettings["EventLogName"];
        try
        {
            if (!System.Diagnostics.EventLog.Exists(sourceName))
                System.Diagnostics.EventLog.CreateEventSource(sourceName, logName);
            eventLog.Source = sourceName;
            eventLog.Log = logName;
        }
        catch
        {
            eventLog.Source = "Application";
        }
    }

在初始化期间,服务已安装,但未创建日志。日志条目在系统的Application 日志中。

我错过了什么?

我使用进程安装程序来安装

 public ProjectInstaller()
 {
        InitializeComponent();
        this.Installers.Add(GetServiceInstaller());
        this.Installers.Add(GetServiceProcessInstaller());
 }

 private ServiceInstaller GetServiceInstaller()
 {
        serviceInstaller.ServiceName = GetConfigurationValue("ServiceName");
        serviceInstaller.Description = GetConfigurationValue("Description");
        serviceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
        return serviceInstaller;
 }

 private ServiceProcessInstaller GetServiceProcessInstaller()
 {
        serviceProcessinstaller.Account = ServiceAccount.LocalSystem;
        return serviceProcessinstaller;
 }

如何创建事件日志?

【问题讨论】:

标签: c# .net windows-services event-log


【解决方案1】:

将您的代码更改为以下内容:

if (!System.Diagnostics.EventLog.SourceExists(source: sourceName))
{
    System.Diagnostics.EventLog.CreateEventSource(source: sourceName, logName: logName);
}

请注意as per Microsoft's KB,事件日志名称的前 8 个字符必须与计算机上的所有其他事件日志不同(因此,如果用户的计算机已经有一个名为 "Application" 的日志,那么您无法创建新的 @ 987654325@ 命名为 "Applicat1""ApplicationFoobar",因为它们与内置的 Application 事件日志共享相同的 8 个字符。

【讨论】:

  • 我试过你的代码。但它没有创建事件日志并且无法启动服务。服务显示服务无法启动。 System.ArgumentException:源“SyncronizationService”未在日志“SyncronizationLog”中注册。 (注册在日志'Application'中。) " Source和Log属性必须匹配,或者你可以将Log设置为空字符串,它会自动匹配到Source属性。错误信息
  • 请注意 logName 必须有唯一的第一个字符,请查看此链接 msdn.microsoft.com/en-us/library/y7x55536%28v=vs.90%29.aspx
【解决方案2】:

先尝试将 AutoLog 设置为 false。

    this.AutoLog = false;

    if (!System.Diagnostics.EventLog.SourceExists(sourceName)) 
    {        
        System.Diagnostics.EventLog.CreateEventSource(sourceName, logName);
    }

    eventLog.Source = "MySource";

在这个 MSDN walkthrough 中,他们似乎完全省略了这一步。但是,在这个 MSDN“How to:”中,他们声明:

如果您想写入应用程序日志以外的事件日志,则必须将 AutoLog 属性设置为 false,在您的服务代码中创建您自己的自定义事件日志,并将您的服务注册为该事件的有效条目源记录。

将 AutoLog 设置为 false 后,我的自定义日志和事件按预期显示。

【讨论】:

    【解决方案3】:

    ServiceName 和 Source 必须是不同的名称。

    服务名称

    this.serviceInstaller1.ServiceName = "MaliyeWMService";
    

    来源

    if (!System.Diagnostics.EventLog.SourceExists("MaliyeMailService"))
    {
      System.Diagnostics.EventLog.CreateEventSource("MaliyeMailService", "MaliyeMailServiceLog");
    
    }
    OlayLog.Source = "MaliyeMailService";
    

    【讨论】:

    • 我只花了 5 个小时...... Tnx MS 我阅读了整个文档并没有提到这个小东西............ @Mustafa Unal 解决方案为我工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    • 2020-02-27
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多