Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get values of parameters in stack trace

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

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

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?

like image 476
Neil N Avatar asked May 24 '13 20:05

Neil N


1 Answers

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);
    }
}
like image 167
Sergey Kalinichenko Avatar answered Sep 27 '22 20:09

Sergey Kalinichenko