【问题标题】:Get Values Of Method On Throws Exceptions in C#获取 C# 中抛出异常的方法的值
【发布时间】:2014-10-19 00:21:00
【问题描述】:

我从事一个伐木项目。我想将方法​​的参数值保存在数据库中。我怎样才能得到这些值。我想在 FirstChanceException 事件中获取“Test”方法的参数值。

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
    }
}

【问题讨论】:

标签: c# exception logging exception-handling aop


【解决方案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 包含传递给失败方法调用的参数值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 2010-09-20
    • 2011-07-28
    • 1970-01-01
    相关资源
    最近更新 更多