STW*_*STW 8 .net debugging exception first-chance-exception
这可能是不现实的,但是是否有可能通知组件在其进程中发生的所有第一次机会异常?
我们有一些第三方(由我们签约)的组件除了吃异常之外没有做任何事情,而且商业关系的政治使整个考验成为皇家的痛苦.
我们也意识到我们的一些代码执行令人失望的行为,即让异常消失在深渊中,而不是使用我们的集中式异常记录器.
我假设我们的应用程序必须作为调试应用程序的子进程启动才能实现效果,但我认为它值得问:)
您可以使用.net概要分析API来获取各种状态的异常通知,这些是可用的方法:
ExceptionThrown
ExceptionSearchFunctionEnter
ExceptionSearchFunctionLeave
ExceptionSearchFilterEnter
ExceptionSearchFilterLeave
ExceptionSearchCatcherFound
ExceptionOSHandlerEnter
ExceptionOSHandlerLeave
ExceptionUnwindFunctionEnter
ExceptionUnwindFunctionLeave
ExceptionUnwindFinallyEnter
ExceptionUnwindFinallyLeave
ExceptionCatcherEnter
ExceptionCatcherLeave
ExceptionCLRCatcherFound
ExceptionCLRCatcherExecute
Run Code Online (Sandbox Code Playgroud)
使用剖析api并不完全适合胆小的人; 看看http://msdn.microsoft.com/en-us/library/ms404386.aspx作为您研究的切入点和http://msdn.microsoft.com/en-us/library/bb384687.aspx特别是异常处理.
我不知道在您的托管代码中执行此操作的简单方法,例如
AppDomain.FirstChanceException += new EventHandler...
Run Code Online (Sandbox Code Playgroud)
事件或类似事件.
编辑:一个可能更好的选择是使用unamanaged调试API.
基本上,您可以使用ICorDebug :: SetManagedHandler设置ICorManagedCallback/ICorManagedCallback2回调,并在发生异常时获取回调.
我在这方面没有足够的经验来了解分析api的优点/缺点.
我刚看了一下使用ICorDebug API 的mdgb示例,它似乎从异常中获得足够的通知(要快速查看发生了什么事件,请在corapi/Debugger.cs中的HandleEvent方法中设置断点:406)
Net 4.0实际上已经添加了该AppDomain.FirstChanceException
事件。它在执行任何 catch 块之前触发。
这篇MSDN 文章有一些示例。
基本上你只需添加一个像这样的事件处理程序:
AppDomain.CurrentDomain.FirstChanceException +=
(object source, FirstChanceExceptionEventArgs e) =>
{
Console.WriteLine("FirstChanceException event raised in {0}: {1}",
AppDomain.CurrentDomain.FriendlyName, e.Exception.Message);
};
Run Code Online (Sandbox Code Playgroud)