系统关闭/重启时写入Windows服务日志

时间:2015-11-26 15:58:04

标签: c# .net windows service windows-services

问题是当主机重启(Windows重启)或关机(Windows关机)时,没有任何内容写入日志。

我在Windows服务中有以下内容:

protected override void OnStart(string[] args)
{
    eventLogUtility.WriteToEventLog("Service has Started.", EventLogEntryType.Information);
}

protected override void OnStop()
{
    eventLogUtility.WriteToEventLog("Service has Stopped.", EventLogEntryType.Information);
}

/// <summary>When implemented in a derived class, executes when the system is shutting down.
/// Specifies what should occur immediately prior to the system shutting down.</summary>
protected override void OnShutdown()
{
    eventLogUtility.WriteToEventLog("Service has Shutdown.", EventLogEntryType.Information);
}
  • 服务启动时,“服务已启动”。被写入日志。

  • 当我停止服务时,“服务已停止”。被写入日志。

P.S。使用.Net 4.5

1 个答案:

答案 0 :(得分:2)

这可能是因为Windows Event Log服务停止服务之前。 您可以使用ServiceInstaller使服务取决于Windows Event Log,从而解决此问题。

 ...
 ServiceInstaller serviceInstaller = new ServiceInstaller()
 serviceInstaller.ServicesDependedOn = new string [] { "EventLog" };
 ...

或使用可视化编辑器添加“ EventLog ”:

enter image description here

相关问题