C# Windows服务未在autostart上写入事件日志

C# Windows服务未在autostart上写入事件日志,c#,.net,windows-10,windows-services,event-log,C#,.net,Windows 10,Windows Services,Event Log,我有这个问题。。。当我手动启动/停止windows服务时,它会按预期工作,我会获取日志条目。但是,如果关闭并重新启动计算机,则服务状态为正在运行,但在计算机关闭时未记录停止消息,也未在打开时记录启动消息。操作系统是Windows10 在这个问题上我快发疯了。该代码是用C语言编写的最基本的windows服务,用于在OnStart和OnClose方法上写入日志。没别的了 我还尝试过在文件系统上编写代码,结果也一样。手动启动/停止时成功;但在自动启动或系统关闭时,它会失败 我已经没有主意了 这是我正在

我有这个问题。。。当我手动启动/停止windows服务时,它会按预期工作,我会获取日志条目。但是,如果关闭并重新启动计算机,则服务状态为正在运行,但在计算机关闭时未记录停止消息,也未在打开时记录启动消息。操作系统是Windows10

在这个问题上我快发疯了。该代码是用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方法(ResumeSupspend)

2020-02-05 11:17:50:调用OnPowerEvent方法(ResumeAutomatic)

2020-02-05 11:18:12:调用了OnSessionChange方法(SessionLogon)


您可以覆盖windows服务提供的以下事件,并检查它是否正在记录到文件系统

OnShutdown(),OnPowerEvent()

例:


嗨,普拉文,谢谢你的回复。我添加了一些源代码。OnShutdown不会记录。OnPowerEvent或OnSessionChange do日志。但是仍然无法控制OnStart方法来保存日志或在出现问题时发出警告。我也无法访问某些文件来完成工作:/n您如何登录到文件系统?
        /// <summary>
        /// Will be called when system goes to shutdown mode. 
        /// </summary>
        protected override void OnShutdown()
        {
           //Log  
            base.OnShutdown();
        }
        CanPauseAndContinue = true;
        CanHandleSessionChangeEvent = true;
        CanHandlePowerEvent = true;
        CanShutdown = true;