且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#中捕获的异常是在线程池存在的

更新时间:2023-11-13 17:40:10

如果我理解正确(如果传递,导致你的例外是线程池线程内发生的结论堆栈跟踪,这将有助于),然后就换​​你EventLogMonitor的代码在try / catch块。

If I understand you correctly (it would help if you pass the stacktrace that leads you to the conclusion that the exception is happening inside a threadpool thread), then just wrap your code of EventLogMonitor in a try/catch block.

例如:

void EventLogHandler(object sender, EventArgs args)
{
   try
   {
      // Your original code.
   }
   catch (Exception ex)
   {
      // Log or Write "ex" to the console. Set a breakpoint, whatever.

      throw;
   }
}



更新

:以后你更新它看起来好像异常的确不是从你的处理程序中提出,但它甚至被称为EventLog类里了。

UPDATE: after your update it looks as if the exception is indeed not raised from inside your handler, but before it is even called inside the EventLog class.

您可以尝试注册与处理程序 AppDomain.UnhandledException 事件,做你的日志/在那里处理。请注意,这不会让你抑制或改变或包装异常,而只是某个记录它用于诊断目的。

You could try registering a handler with the AppDomain.UnhandledException event and do your logging/handling in there. Note that this will not allow you to suppress or "change" or wrap the exception, but merely to log it somewhere for diagnostic purposes.

如果你只是想检查除了一次(或上一次),你应该尝试使用SOS扩展的!PrintException 在WinDbg中的命令。

If you just want to inspect the exception once (or on occasion), you should try using the SOS-extension's !PrintException command in WinDBG.

更新2 :在进一步调查之后我发现它,而奇怪的是,异常堆满了所有。您的堆栈跟踪建议你使用.NET 3.5(或更早,而不是4),并期待在反射EventLog类,你可以看到,整个处理 EventWrittenHandler ,包括似乎导致异常的前置码,被包裹在一个大的try / catch语句(例外)/ catch块。滑稽。

UPDATE 2: after further investigation I find it rather strange that the exception bubbles up all. Your stacktrace suggests you're using .NET 3.5 (or earlier, but not 4.) and looking at the EventLog class in Reflector you can see that the whole handling of the EventWrittenHandler, including the preamble code that seems to cause the exception, is wrapped in one big "try/catch(Exception)/catch" block. Funny.