【问题标题】:CurrentDomain.UnhandledException is not being thrown in Windows ServiceCurrentDomain.UnhandledException 未在 Windows 服务中引发
【发布时间】:2013-09-06 15:14:16
【问题描述】:

我已设置 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 方法中记录此内容,但我一无所获。

为什么没有捕获到异常?

【问题讨论】:

  • 在这里发布更多代码。

标签: c# unhandled-exception


【解决方案1】:

可能是你的抛出代码在使用之前设置AppDomain.CurrentDomain.UnhandledException或者因为你把它放在OnStart方法中

第二个选项是因为它是ThreadException

上述问题已在以下主题中讨论过:

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

【讨论】:

  • 宾果游戏。我把它放在 OnStart 方法中。谢谢。
  • ThreadException 特定于 Windows 窗体,不适用于 Windows 服务 (stackoverflow.com/a/2014684/8479)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-29
  • 1970-01-01
  • 1970-01-01
  • 2021-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多