【问题标题】:.NET - First chance exception listener for intensive debugging?.NET - 密集调试的第一次机会异常侦听器?
【发布时间】:2009-06-04 18:31:45
【问题描述】:

这可能是不现实的,但是否有可能使组件能够在其进程中发生的所有第一次机会异常时得到通知?

我们有一些第三方(由我们承包)组件,除了吃异常之外什么都做不了,商业关系的政治使整个磨难成为皇家痛苦。

我们也意识到我们的一些代码正在执行令人失望的操作,让异常消失在深渊中,而不是使用我们的集中式异常记录器。

我认为我们的应用程序必须作为调试应用程序的子进程启动才能达到效果,但我认为值得一问:)

【问题讨论】:

    标签: .net debugging exception first-chance-exception


    【解决方案1】:

    您可以使用 .net 分析 API 来获取各种状态下的异常通知,这些是可用的方法:

    ExceptionThrown
    ExceptionSearchFunctionEnter
    ExceptionSearchFunctionLeave
    ExceptionSearchFilterEnter
    ExceptionSearchFilterLeave
    ExceptionSearchCatcherFound
    ExceptionOSHandlerEnter
    ExceptionOSHandlerLeave
    ExceptionUnwindFunctionEnter
    ExceptionUnwindFunctionLeave
    ExceptionUnwindFinallyEnter
    ExceptionUnwindFinallyLeave
    ExceptionCatcherEnter
    ExceptionCatcherLeave
    ExceptionCLRCatcherFound
    ExceptionCLRCatcherExecute
    

    使用 profiling api 并不完全适合胆小的人;看看http://msdn.microsoft.com/en-us/library/ms404386.aspx 作为您研究的入口点,看看http://msdn.microsoft.com/en-us/library/bb384687.aspx 专门用于异常处理。

    我不知道在您的托管代码中执行此操作的简单方法,例如

    AppDomain.FirstChanceException += new EventHandler...
    

    事件或类似的。

    编辑:一个可能更好的选择是使用 unamanaged debugging API 代替。

    基本上,您可以使用ICorDebug::SetManagedHandler 设置ICorManagedCallback/ICorManagedCallback2 回调,并在发生异常时获取回调。

    我在这方面的经验不足,无法了解分析 api 的优点/缺点。

    我刚刚查看了使用 ICorDebug API 的 mdgb sample,它似乎从异常中获得了足够多的通知​​(要快速查看发生了什么事件,请在 corapi/Debugger.cs 的 HandleEvent 方法中设置断点:第406章)

    【讨论】:

    • 我没想到它会很容易,但至少我需要一个路来感受它。非常感谢!
    • 我在玩过 mdbg 之后编辑了我的答案,我相信 ICorDebug API 可能是更好的方法。
    【解决方案2】:

    Net 4.0 实际上已经添加了AppDomain.FirstChanceException 事件。它在任何 catch 块执行之前触发。

    这个MSDN article 有一些例子。

    基本上你只需像这样添加一个事件处理程序:

        AppDomain.CurrentDomain.FirstChanceException += 
            (object source, FirstChanceExceptionEventArgs e) =>
            {
                Console.WriteLine("FirstChanceException event raised in {0}: {1}",
                    AppDomain.CurrentDomain.FriendlyName, e.Exception.Message);
            };
    

    【讨论】:

    • 非常好的发现!非常感谢您解决这个老问题!
    猜你喜欢
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 2014-06-27
    • 1970-01-01
    • 2013-03-14
    相关资源
    最近更新 更多