简体   繁体   English

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

[英]Windows Service log writing on system shutdown / restart

The issue is that when host machine is restarted (Windows restart) or shutdown (Windows shutdown) nothing is being written to the log. 问题是当主机重启(Windows重启)或关机(Windows关机)时,没有任何内容写入日志。

I have the following in a Windows Service: 我在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);
}
  • When the service starts, "Service has Started." 服务启动时,“服务已启动”。 is written to the log. 写入日志。

  • When the service is stopped by me, "Service has Stopped." 当我停止服务时,“服务已停止”。 is written to the log. 写入日志。

PS Using .Net 4.5 PS使用.Net 4.5

It's probably because Windows Event Log service shutdowns before your service. 这可能是因为Windows事件日志服务在您的服务之前关闭。 You can solve it by making your Service depends on Windows Event Log using ServiceInstaller . 您可以通过使用ServiceInstaller使您的服务依赖于Windows Event Log来解决此问题。

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

or with the visual editor by adding " EventLog " : 或通过添加“ EventLog ”与可视化编辑器:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM