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

前端 未结 11 1429
南方客
南方客 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: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

提交回复
热议问题