C# 当服务器重新启动c停止windows服务时如何记录日志#

C# 当服务器重新启动c停止windows服务时如何记录日志#,c#,.net,crash,windows-services,topshelf,C#,.net,Crash,Windows Services,Topshelf,我想处理windows服务何时被任务管理器或系统重新启动终止的问题 我尝试在服务计时器的计时器内使用下面的处理程序 public void Start() { AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; _myTimer= new Timer(10000); _myTimer.Elapsed += OnElapsed;

我想处理windows服务何时被任务管理器或系统重新启动终止的问题

我尝试在服务计时器的计时器内使用下面的处理程序

  public void Start()
    {
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
        _myTimer= new Timer(10000);
        _myTimer.Elapsed += OnElapsed;
        _myTimer.AutoReset = false;
        _myTimer.Start();
    }

  private void OnElapsed(object sender, ElapsedEventArgs e)
     {
                try
                {
                   _myTimer.Stop();
                   //Work
                }
                finally
                {
                    _myTimer.Start();
                }
     }
甚至在我使用Topshelf for windows服务时,我也在控制台的Main()方法中加入了相同的内容

 static void Main(string[] args)
    {
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
        var exitCode = HostFactory.Run(hostConfigurator =>
        {
            hostConfigurator.Service<MyService>(serviceConfigurator =>
            {
                serviceConfigurator.ConstructUsing(() => new MyService());
                serviceConfigurator.WhenStarted(MyService => MyService.Start());
                serviceConfigurator.WhenStopped(MyService=> MyService.Stop());
            });

            hostConfigurator.RunAsLocalSystem();

            hostConfigurator.SetDisplayName("MyService");
            hostConfigurator.SetDescription("MyService");
            hostConfigurator.SetServiceName("MyService");
            hostConfigurator.UseLog4Net();
        });
}


private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            File.AppendAllText(@"C:\Crash.txt", "Main Termination");
        }
static void Main(字符串[]args)
{
AppDomain.CurrentDomain.UnhandledException+=CurrentDomain\u UnhandledException;
var exitCode=HostFactory.Run(hostConfigurator=>
{
hostConfigurator.Service(serviceConfigurator=>
{
serviceConfigurator.ConstructUsing(()=>newmyservice());
开始时(MyService=>MyService.Start());
serviceConfigurator.WhenStopped(MyService=>MyService.Stop());
});
hostConfigurator.RunAsLocalSystem();
hostConfigurator.SetDisplayName(“MyService”);
hostConfigurator.SetDescription(“MyService”);
hostConfigurator.SetServiceName(“MyService”);
hostConfigurator.UseLog4Net();
});
}
私有静态void CurrentDomain_UnhandledException(对象发送方,UnhandledExceptionEventArgs e)
{
AppendAllText(@“C:\Crash.txt”,“主终止”);
}
在任何一种情况下,当我从任务管理器或系统重新启动中终止windows服务进程时,都不会调用处理程序。即使是TopShelf也没有记录“UnhandledServiceException”退出代码

我在谷歌上搜索了所有可能的方法,但没有找到运气。谁能帮我一下吗


谢谢

如果您只想记录,windows事件日志是否还不够?它已经记录了服务关闭等情况,或者可以打开。这不是我的期望@Ryios。我必须更新数据库中的inprogress work status,以便在服务重新启动后可以恢复它。我认为没有事件/处理程序允许您通过任务管理器检测进程何时被终止。但在请求重新启动系统时有一种方法(查看文档中的
SystemEvents.SessionEnding
)。然而,如果我是你,我会在运行时将进程工作状态记录在tmp日志文件中。因此,如果进程因任何原因被终止,然后恢复,您可以读取此文件并知道下一步要做什么。MSDN说“控制台应用程序不会引发SessionEnding事件”。仅当消息泵正在运行时才会引发此事件。在Windows服务中,除非使用隐藏表单或消息泵已手动启动,否则不会引发此事件。有关显示如何在Windows服务中使用隐藏窗体处理系统事件的代码示例,请参阅SystemEvents类。