是否有可能在.net中可以捕捉到任何方法的所有未处理的异常在一个类的向上传递之前调用堆栈?有可能、堆栈、异常、方法

2023-09-04 01:34:09 作者:若相惜、永不弃

问题:

我想可以捕捉到任何方法的任何异常的一类,这样我可以记录类的特定数据异常的日志记录才能通过堆栈。我知道,我可以把一个try-catch在类的所有方法,但也有许多方法,它似乎应该有一个更有效的方式。

的例子是什么我目前做的:

 公共类ClassA的
{
    私人诠释X;
    私人诠释Ÿ;

    公共无效方法1()
    {
        尝试
        {
           //有些code
        }
        赶上(例外前)
        {
            ex.Data.Add(×,x)的;
            ex.Data.Add(Y,y)基
            扔;
        }
    }

    公共无效方法2()
    {
        尝试
        {
            //有些code
        }
        赶上(例外前)
        {
            ex.Data.Add(×,x)的;
            ex.Data.Add(Y,y)基
            扔;
        }
    }
}
 

是我想要做的例子:

 公共类ClassB的:IUnhandledErrorHandler
{
    私人诠释X;
    私人诠释Ÿ;

    公共无效方法1()
    {
        //有些code
    }

    公共无效方法2()
    {
        //有些code
    }

    无效IUnhandledErrorHandler.OnError(例外前)
    {
        ex.Data.Add(×,x)的;
        ex.Data.Add(Y,y)基
        扔;
    }
}

公共接口IUnhandledErrorHandler
{
    无效的OnError(例外前);
}
 

注意: 这个类是一个WCF项目中的服务,并实现了的ServiceContract。我曾尝试加入的ErrorHandler到服务的ChannelDispatcher。然而,当错误达到的ErrorHandler它已超出了发生错误的类的范围,所以我不能访问类的细节。

解决方法:

 公共类ClassC
{
    公共ClassC()
    {
        AppDomain.CurrentDomain.FirstChanceException + =的OnError;
    }

    私人诠释X;
    私人诠释Ÿ;

    公共无效方法1()
    {
        //有些code
    }

    公共无效方法2()
    {
        //有些code
    }

    私人无效的OnError(对象发件人,System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs E)
    {
        e.Exception.Data [×] = X;
        e.Exception.Data [Y] = Y;
    }
}
 
.NET程序崩溃了怎么抓 Dump 我总结了三种方案

解决方案

如果您运行在.NET 4中,你可以使用FirstChanceException事件从AppDomain中。

Problem:

I would like to catch any exceptions from any method in a class so that I may record class specific data to the exception for logging before it is passed up the stack. I know that I can put a try-catch in every method of the class, but there are many methods and It seems there should be a more efficient way.

Example of what I am currently doing:

public class ClassA
{
    private int x;
    private int y;

    public void Method1()
    {
        try
        {
           //Some code
        }
        catch(Exception ex)
        {
            ex.Data.Add("x", x);
            ex.Data.Add("y", y);
            throw;
        }
    }

    public void Method2()
    {
        try
        {
            //Some code
        }
        catch (Exception ex)
        {
            ex.Data.Add("x", x);
            ex.Data.Add("y", y);
            throw;
        }
    }
}

Example of what I would like to do:

public class ClassB : IUnhandledErrorHandler
{
    private int x;
    private int y;

    public void Method1()
    {
        //Some code
    }

    public void Method2()
    {
        //Some code
    }

    void IUnhandledErrorHandler.OnError(Exception ex)
    {
        ex.Data.Add("x", x);
        ex.Data.Add("y", y);
        throw;
    }
}

public interface IUnhandledErrorHandler
{
    void OnError(Exception ex);
}

Note: This class is a service in a WCF project and implements a ServiceContract. I have tried adding an ErrorHandler to the service's ChannelDispatcher. However, when the error reaches the ErrorHandler it is already beyond the scope of the class where the error occurred, so I cannot access the class details.

Solution:

public class ClassC
{
    public ClassC()
    {
        AppDomain.CurrentDomain.FirstChanceException += OnError;
    }

    private int x;
    private int y;

    public void Method1()
    {
        //Some code
    }

    public void Method2()
    {
        //Some code
    }

    private void OnError(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
    {
        e.Exception.Data["x"] = x;
        e.Exception.Data["y"] = y;
    }
}

解决方案

If you run on .NET 4, you might use the FirstChanceException event from the AppDomain.