【问题标题】:Error while writing to event log, prevents windows service from starting?写入事件日志时出错,阻止 Windows 服务启动?
【发布时间】:2011-05-14 07:02:19
【问题描述】:

我正在使用以下代码在我的 Windows 服务应用程序中创建自定义事件日志

public ServiceConstructor()
{
  InitializeComponent();
  if (!EventLog.SourceExists("WinService"))
  {
    EventLog.CreateEventSource("WinService", "WinServiceLog");
    eventLog1.Source = "WinService";
    eventLog1.Log = "WinServiceLog";
  }
}
protected override void OnStart(string[] args)
{
 eventLog1.WriteEntry("Started");
}

安装 service.msi 后,当我启动服务时,它启动然后停止。然后我在 EventViewer windows 日志部分发现以下错误:

服务无法启动。 System.ArgumentException:在写入之前未设置源属性 到事件日志。

at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) at System.Diagnostics.EventLog.WriteEntry(String message) at WinService.Service.OnStart(String[] args) at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

【问题讨论】:

    标签: c# windows-services event-log


    【解决方案1】:

    尝试以下方法:

    EventLog.CreateEventSource("WinService", "Application");
    eventLog1.Log = "Application";

    在 OnStart 中也加入以下内容:

    eventLog1.Log="Application"
    eventLog1.Source = "WinService";

    【讨论】:

      【解决方案2】:

      如果源已经存在,看起来你没有初始化 eventLog1.Source。

      建议你将初始化代码移到 OnStart 并移出构造函数。

      并将这两行移出 if 语句:

      eventLog1.Source = "WinService";
      eventLog1.Log = "WinServiceLog";
      

      【讨论】:

        猜你喜欢
        • 2023-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多