获取方法的值在C#中引发异常

时间:2014-10-16 08:04:51

标签: c# exception logging exception-handling aop

我在一个日志项目上工作。我想保存数据库中方法的参数值。我怎样才能获得这些值。我想得到参数值"测试" FirstChanceException事件中的方法。

class Program
{
    private static void Main(string[] args)
    {
        AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;

        Test(100);
    }

    private static void Test(int i)
    {
        throw new OverflowException();
    }

    private static void CurrentDomain_FirstChanceException(object sender,
        System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
    {
        //I Want To Get Parameter Value of Test Method
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用PostSharp

执行此操作
[Serializable] 
public class ExceptionPolicyAttribute : OnExceptionAspect 
{ 
    public override void OnException(MethodExecutionArgs args) 
    { 
        Guid guid = Guid.NewGuid(); 

        Trace.TraceError("Exception {0} handled by ExceptionPolicyAttribute: {1}", 
            guid, args.Exception.ToString()); 

        throw new InternalException( 
            string.Format("An internal exception has occurred. Use the id {0} " + 
            "for further reference to this issue.", guid)); 
    } 
}

参数MethodExecutionArgs包含传递给失败方法调用的参数值。