usman shaheen
usman shaheen

Reputation: 3766

How to Trace all local variables when an exception occurs

any generic way to trace/log values of all local variables when an exception occurs in a method? (in C# 3)

Upvotes: 12

Views: 6186

Answers (4)

usman shaheen
usman shaheen

Reputation: 3766

Answer: Using PostSharp (Policy Injection), XTraceMethodBoundary attribute, override OnException . this logs all the method input and return parameters types and values. I modified PostSharp to add a simple method to log parameters. not perfect but good enough

private static void TraceMethodArguments(MethodExecutionEventArgs eventArgs)
{
    object[] parameters = eventArgs.GetReadOnlyArgumentArray();

    if (parameters != null)
    {
        string paramValue = null;
        foreach (object p in parameters)
        {
            Type _type = p.GetType();
            if (_type == typeof(string) || _type == typeof(int) || _type == typeof(double) || _type == typeof(decimal))
            {
                paramValue = (string)p;
            }
            else if (_type == typeof(XmlDocument))
            {
                paramValue = ((XmlDocument)p).OuterXml;
            }
            else
            { //try to serialize
                try
                {
                    XmlSerializer _serializer = new XmlSerializer(p.GetType());
                    StringWriter _strWriter = new StringWriter();

                    _serializer.Serialize(_strWriter, p);
                    paramValue = _strWriter.ToString();
                }
                catch
                {
                    paramValue = "Unable to Serialize Parameter";
                }
            }
            Trace.TraceInformation("[" + Process.GetCurrentProcess().Id + "-" + Thread.CurrentThread.ManagedThreadId.ToString() + "]" + " Parameter: " + paramValue);
        }
    }
}

Upvotes: 10

1800 INFORMATION
1800 INFORMATION

Reputation: 135285

Use MiniDumpWriteDump to create a memory dump of the process at the point of the exception. You would have to P/Invoke it.

Upvotes: 2

Brian Rasmussen
Brian Rasmussen

Reputation: 116401

You could do a memory dump of the process since that captures both the heaps and the stacks, but since you tagged the question as logging, I assume that is not what you're looking for.

However, you can reduce the need for this information by making sure your methods are always small and to the point. That will yield much more useful stack traces and it will limit the number of locals to inspect.

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1500555

You can't, basically. Reflection lets you get at instance (and static) variables but if you want to log local variables, you'll have to do so explicitly. It's possible that you could do a bit better using the profiling API, but that would be a pretty extreme step.

Upvotes: 7

Related Questions