【发布时间】:2020-02-05 09:22:52
【问题描述】:
我遇到了这个问题...当我手动启动/停止 Windows 服务时,它按预期工作,我得到了日志条目。但是,如果我关闭并重新启动机器,服务状态正在运行,但它没有在计算机关闭时记录停止消息,也没有在打开时记录启动消息。操作系统是 Windows 10。
我在这个问题上疯了。该代码是用 C# 编写的最基本的 windows 服务写入日志的 OnStart 和 OnClose 方法。仅此而已。
我也尝试在文件系统上写入,结果相同。手动启动/停止时成功;但在自动启动或系统关闭时会失败。
我的想法已经用完了。
这是我正在运行的代码:
public partial class Service1 : ServiceBase
{
private bool _loggedLogPath = false;
public Service1()
{
InitializeComponent();
this.AutoLog = true;
this.CanHandlePowerEvent = true;
this.CanHandleSessionChangeEvent = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.CanStop = true;
this.CanStop = true;
}
private void LogToFile(string message)
{
try
{
string path = Path.Combine(Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData), "TestService.txt");
using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine(String.Concat(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ": ", message));
writer.Flush();
}
if (!this._loggedLogPath)
{
this.EventLog.WriteEntry(String.Concat("The log path is: ", path));
this._loggedLogPath = true;
}
}
catch
{
// Ignore exceptions to avoid crashing the service.
}
}
protected override void OnContinue()
{
this.EventLog.WriteEntry("OnContinue method Called.");
LogToFile("OnContinue method called.");
base.OnContinue();
}
protected override void OnPause()
{
this.EventLog.WriteEntry("OnPause method called");
LogToFile("OnPause method called.");
base.OnPause();
}
protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
{
this.EventLog.WriteEntry("OnPowerEvent method called");
LogToFile(String.Format("OnPowerEvent method called ({0}).", powerStatus.ToString()));
return base.OnPowerEvent(powerStatus);
}
protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
this.EventLog.WriteEntry(String.Format("OnSessionChange method called ({0}).", changeDescription.Reason.ToString()));
LogToFile(String.Format("OnSessionChange method called ({0}).", changeDescription.Reason.ToString()));
base.OnSessionChange(changeDescription);
}
protected override void OnShutdown()
{
this.EventLog.WriteEntry("OnShutdown method called.");
LogToFile("OnShutdown method called.");
base.OnShutdown();
}
protected override void OnStart(string[] args)
{
this.EventLog.WriteEntry("OnStart method called.");
LogToFile("OnStart method called.");
base.OnStart(args);
}
protected override void OnStop()
{
this.EventLog.WriteEntry("OnStop method called.");
LogToFile("OnStop method called.");
base.OnStop();
}
}
记录到文件系统的消息是:
2020-02-05 11:17:10:调用 OnSessionChange 方法 (RemoteConnect)。
2020-02-05 11:17:13:调用 OnSessionChange 方法 (SessionLogoff)。
2020-02-05 11:17:14:调用 OnSessionChange 方法 (ConsoleDisconnect)。
2020-02-05 11:17:14:调用 OnSessionChange 方法 (RemoteDisconnect)。
2020-02-05 11:17:15:调用 OnSessionChange 方法 (ConsoleConnect)。
2020-02-05 11:17:15:调用 OnPowerEvent 方法(暂停)。
2020-02-05 11:17:50:调用 OnPowerEvent 方法 (ResumeSuspend)。
2020-02-05 11:17:50:调用 OnPowerEvent 方法 (ResumeAutomatic)。
2020-02-05 11:18:12:调用 OnSessionChange 方法 (SessionLogon)。
【问题讨论】:
标签: c# .net windows-10 windows-services event-log