In a .net Exception how to get a stacktrace with argument values

前端 未结 11 1392
南方客
南方客 2020-12-01 01:58

I am trying to add an unhandled exception handler in .net (c#) that should be as helpfull for the \'user\' as possible. The end users are mostly programers so they just need

相关标签:
11条回答
  • 2020-12-01 02:27

    it is theoretically possible to do what you want by taking advantage of the Portable Executable (PE) file format to get the variable types and offsets, but I ran into a [documentation] wall trying to do this a couple of years ago. Good luck!

    0 讨论(0)
  • 2020-12-01 02:33

    I don't think System.Diagnostics.StackFrame supplies argument information (other than the method signature).

    You could instrument the troublesome calls with trace logging via AOP, or even use its exception interception features to conditionally log without having to litter your code. Have a look around http://www.postsharp.org/.

    0 讨论(0)
  • 2020-12-01 02:35

    Since the end users are developers, you can provide them with a version that enables logging of all the key values/arguments that are passed. And provide a facility so that they can turn on/off logging.

    0 讨论(0)
  • 2020-12-01 02:41

    If you could do what you are looking for, you would be defeating an integral part of .NET security.

    The best option in the case, it to attach to a debugger or profiler (either of them can access those values). The former can be attached, the latter need to be active before the program starts.

    0 讨论(0)
  • 2020-12-01 02:42

    Unfortunately you can't get the actual values of parameters from the callstack except with debugging tools actually attached to the application. However by using the StackTrace and StackFrame objects in System.Diagnostics you can walk the call stack and read out all of the methods invoked and the parameter names and types. You would do this like:

    System.Diagnostics.StackTrace callStack = new System.Diagnostics.StackTrace();
    System.Diagnostics.StackFrame frame = null;
    System.Reflection.MethodBase calledMethod = null;
    System.Reflection.ParameterInfo [] passedParams = null;
    for (int x = 0; x < callStack.FrameCount; x++)
    {
        frame = callStack.GetFrame(x);
        calledMethod = frame.GetMethod();
        passedParams = calledMethod.GetParameters();
        foreach (System.Reflection.ParameterInfo param in passedParams)
            System.Console.WriteLine(param.ToString()); 
    }
    

    If you need actual values then you're going to need to take minidumps and analyse them i'm afraid. Information on getting dump information can be found at:

    http://www.debuginfo.com/tools/clrdump.html

    0 讨论(0)
  • 2020-12-01 02:42

    I don't believe there is a built-in mechanism. Retrieving each frame of the stack trace, while allowing you to determine the method in the trace, only gives you the reflected type information on that method. No parameter information. I think this is why some exceptions, notably, ArgumentException, et. al. provide a mechanism to specify the value of the argument involved in the exception since there's no easy way to get it.

    0 讨论(0)
提交回复
热议问题