gpt4 book ai didi

c# - CurrentDomain.UnhandledException 未在 Windows 服务中抛出

转载 作者:行者123 更新时间:2023-11-30 16:16:16 24 4
gpt4 key购买 nike

我已经设置了 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 方法中记录它,但我什么也没得到。

为什么没有捕获异常?

最佳答案

可能是你的抛出代码是在use set AppDomain.CurrentDomain.UnhandledException之前或者因为你把它放在了OnStart方法

第二个选项是因为它是ThreadException

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

  1. How can I set up .NET UnhandledException handling in a Windows service?
  2. Global exception handler for windows services?

关于c# - CurrentDomain.UnhandledException 未在 Windows 服务中抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18591382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com