获取堆栈跟踪中的参数值 [英] Get values of parameters in stack trace

查看:34
本文介绍了获取堆栈跟踪中的参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法重现我们在错误日志中看到的一些错误.

I am having trouble reproducing a few errors we are seeing in our error log.

如果我知道特定方法在抛出异常时使用的是哪个记录 ID,事情就会变得容易得多.

It could be made a lot easier if I knew which record ID a specific method was using when it threw an exception.

我们所有未处理的异常都由我们的全局异常处理程序处理,该处理程序将异常的所有详细信息以及 HTTP 请求的所有详细信息放入日志表中.

All of our unhandled exceptions get handled by our global exception handler, which puts all the details of the exception, as well as all the details of the HTTP request, into a log table.

有没有办法捕获抛出异常的方法的所有参数的值?或者更好的是,堆栈跟踪中的所有值?

Is there a way to capture the values of all the parameters for the method that threw an exception? Or even better, all the values up the stack trace?

推荐答案

不幸的是,这是不可能的:在处理程序中捕获异常时,所有带有方法参数的堆栈帧都消失了.一旦控件离开您的函数,您将无法再访问其参数值.

Unfortunately, this is not possible: at the time when you catch the exception in the handler, all the stack frames with the method parameters are gone. Once the control leaves your function, you can no longer access its parameter values.

既然你知道崩溃发生的具体函数,你可以在那里设置一个异常处理程序来收集所有感兴趣的参数,并重新抛出一个包装的异常.诊断完成后,您可以将代码恢复正常:

Since you know the specific function where the crash happens, you could set up an exception handler there to collect all the parameters of interest, and re-throw a wrapped exception. Once the diagnostics is complete, you could revert the code back to normal:

void SuspiciousFunction(string name, long count) {
    try {
        // The code of your function goes here
    } catch (Exception e) {
        var args = new Dictionary<string,object> {
            { "name" , name  }
        ,   { "count", count }
        };
        throw new MySpecialException(e, args);
    }
}

这篇关于获取堆栈跟踪中的参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆