Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows服务中未引发CurrentDomain.UnhandledException_C#_Unhandled Exception - Fatal编程技术网

C# Windows服务中未引发CurrentDomain.UnhandledException

C# Windows服务中未引发CurrentDomain.UnhandledException,c#,unhandled-exception,C#,Unhandled Exception,我已经设置了UnhandledException,以便在服务崩溃之前记录任何异常 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(unhandledException); 我正在通过抛出未处理的异常来测试这一点 throw new Exception("TestExcepion"); private void unhandledException(object sender, U

我已经设置了UnhandledException,以便在服务崩溃之前记录任何异常

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(unhandledException);
我正在通过抛出未处理的异常来测试这一点

throw new Exception("TestExcepion");

private void unhandledException(object sender, UnhandledExceptionEventArgs e)
{
    // Logs to Log4Net but its not reaching here.
    Log.Info("Unhandled Exception: " + (e.ExceptionObject as Exception).ToString());
}
我正试图用Log4Net在我的
unhandledException
方法中记录这个,但我什么也没有得到


为什么没有捕获异常?

可能是因为您的抛出代码在使用set
AppDomain.CurrentDomain.UnhandledException
之前,或者是因为您将其放入了
OnStart
方法中

第二个选项是因为它是
ThreadException

之前在以下线程中讨论了上述问题:


  • 在这里发布更多代码。宾果。我把它放在OnStart方法中。谢谢。
    ThreadException
    特定于Windows窗体,不适用于Windows服务()。