【问题标题】:Windows Service log writing on system shutdown / restart系统关闭/重启时写入 Windows 服务日志
【发布时间】:2015-11-26 15:58:03
【问题描述】:

问题是当主机重新启动(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);
}
  • 服务启动时,“服务已启动”。被写入日志。

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

附:使用 .Net 4.5

【问题讨论】:

  • 我不是 100% 确定,因为我不是服务人员,但我不认为 OnShutdown 总是涵盖重新启动。尝试使用SessionEnded 方法而不是shown here
  • @Equalsk 感谢您的帮助,但我忘了输入问题文本,它不仅仅是重新启动......关机也不起作用......我现在已经编辑了......标题确实说关机/重启
  • 嗯...测试...看起来可能是由于它在哪个帐户下运行...social.msdn.microsoft.com/Forums/vstudio/en-US/…
  • 不,没有任何区别......仍然没有使用(域)管理员帐户写入日志,作为系统登录或作为服务登录......没有写入任何内容。呸呸呸

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


【解决方案1】:

这可能是因为Windows Event Log 服务在您的服务之前关闭。 您可以通过使用ServiceInstaller 使您的服务依赖于Windows Event Log 来解决它。

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

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

【讨论】:

  • 感谢您的建议。我已经使用了 ServiceInstaller,所以我只是在 ServiceDependedOn 中添加了“EventLog”,在我的开发机器上安装了服务,关闭了窗口......并且没有任何内容写入事件日志:S......文档暗示这都是关于所需的服务依赖项的服务启动...没有关于关闭等。
  • 只是确保... EventLog 是 Windows 事件日志服务的名称吗?还是通用名称?还是应该是我的事件日志实例的名称?
  • 应该是。您是否检查了服务属性上的依赖项选项卡以查看是否存在 Windows 事件日志?
  • 应该是...应该是什么?...我使用可视化编辑器手动放置了EventLog。
  • 啊,谢谢,刚刚使用计算机管理检查了服务条目,它说,就像您的图像一样,Windows 事件日志作为依赖项......唉,仍然无法正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多