.NET - 第一次机会异常监听器密集调试?监听器、密集、异常、机会

2023-09-03 02:01:34 作者:不丧

这可能是不现实的,但它有可能使一个组件的所有第一机会异常存在的在其过程中得到通知?

This is probably unrealistic, but would it be possible to enable a component to be notified of all first chance exceptions occuring in its process?

我们有一些第三方(由我们签约),这不能做任何事情,但吃excepitions和业务关系的政治使整个磨难皇家疼痛的组件。

We have some third-party (contracted by us) components which fail to do anything but eat excepitions and the politics of the business relationship make the whole ordeal a royal pain.

我们也都知道,我们的一些code正在执行让异常消失的深渊,而不是用我们的集中式例外记录令人失望的作用。

We also are aware that some of our code is performing the disappointing action of letting exceptions vanish into the abyss rather than using our centralized exception logger.

我想我们的应用程序都必须开始作为一个调试应用程序来实现的效果的一个子进程,但我想这是值得一问:)

I assume our application would have to be started as a child process of a debugging application to achieve the effect, but I figure it's worth asking :)

推荐答案

.NET 4.0实际上已经增加了的 AppDomain.FirstChanceException 事件。它发射的任何catch块被执行之前。

Net 4.0 has actually added the AppDomain.FirstChanceException event. It fires before any catch block is executed.

这 MSDN文章有一些例子。

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

Basically you just add an event handler like this:

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