C#如何调查我的服务意外停止的原因

Cat*_*lMF 1 c# exception

我的服务在随机的时间内完美地工作,然后停止.

我正在捕捉我能看到的所有异常.我已经在服务创建中添加了一个try/catch来尝试获得某种错误,但我什么都没得到.服务刚刚停止.

        try
        {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] { new my_service() };
                ServiceBase.Run(ServicesToRun);
        }
        catch (Exception e)
        {
            logEvent(e);   // Log any errors to a .txt file.
        }
Run Code Online (Sandbox Code Playgroud)

事件查看器显示以下内容:

Faulting application name: my_service.exe, version: 0.9.2.1, time stamp: 0x50717154
Faulting module name: ntdll.dll, version: 6.1.7601.17514, time stamp: 0x4ce7b96e
Exception code: 0xc0000005
Fault offset: 0x00055f99
Faulting process id: 0x%9
Faulting application start time: 0x%10
Faulting application path: %11
Faulting module path: %12
Report Id: %13
Run Code Online (Sandbox Code Playgroud)

mba*_*emy 7

我们无法猜出您提供的信息出了什么问题.但是你可以尝试使用技巧来捕获每个未捕获的异常,并记录它.

以下是我在项目中的表现:

首先,在我的Main()中,我接受以下事件:

AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler;
Run Code Online (Sandbox Code Playgroud)

我定义了Handler,它只是将堆栈跟踪记录到文件中并将其打印到控制台(尽管它与您无关,因为您将应用程序作为服务运行):

private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e){
        try{
            Logger.Append(Severity.CRITICAL, "Unhandlable error : "+e.ExceptionObject); 


        }catch{}
        Console.WriteLine("Unhandlable error : "+e.ExceptionObject);
        Environment.Exit(10);
    }
Run Code Online (Sandbox Code Playgroud)