<p>I would like to be able to obtain all the parameter values from the stack frame in .NET. A bit like how you're able to see the values in the call stack when in the Visual Studio debugger. My approach has concentrated on using the <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.stackframe%28v=vs.71%29.aspx" rel="noreferrer">StackFrame class</a> and then to reflect over a <a href="http://msdn.microsoft.com/en-us/library/system.reflection.parameterinfo%28v=vs.71%29.aspx" rel="noreferrer">ParameterInfo</a> array. I've had success with reflection and properties, but this is proving a bit trickier.</p>

<p>Is there an approach for achieving this?</p>

<p>The code so far looks like this:</p>

<pre><code>class Program
{
    static void Main(string[] args)
    {
        A a = new A();
        a.Go(1);
    }
}

public class A
{
    internal void Go(int x)
    {
        B b = new B();
        b.Go(4);
    }
}

public class B
{
    internal void Go(int y)
    {
        Console.WriteLine(GetStackTrace());

    }
    public static string GetStackTrace()
    {
        StringBuilder sb = new StringBuilder();
        StackTrace st = new StackTrace(true);
        StackFrame[] frames = st.GetFrames();

        foreach (StackFrame frame in frames)
        {
            MethodBase method = frame.GetMethod();

            sb.AppendFormat("{0} - {1}",method.DeclaringType, method.Name);
            ParameterInfo[] paramaters = method.GetParameters();
            foreach (ParameterInfo paramater in paramaters)
            {
                sb.AppendFormat("{0}: {1}", paramater.Name, paramater.ToString());
            }
            sb.AppendLine();
        }
        return sb.ToString();
    }
}
</code></pre>

<h2>The output looks like this:</h2>

<pre><code>SfApp.B - GetStackTrace
SfApp.B - Go
y: Int32 y
SfApp.A - Go
x: Int32 x
SfApp.Program - Main
args: System.String[] args
</code></pre>

<h2>I'd like it to look more like this:</h2>

<pre><code>SfApp.B - GetStackTrace
SfApp.B - Go
y: 4
SfApp.A - Go
x: 1
SfApp.Program - Main
</code></pre>

<hr/>

<p>Just for a bit of context, my plan was to try and use this when I throw my own exceptions. I'll look at your suggestions in more detail and see if I can see it fitting.</p>

[{'answer_id': 75297, 'author': 'Lars Truijens', 'author_id': 1242, 'author_profile': 'https://Stackoverflow.com/users/1242', 'pm_score': 4, 'selected': True, 'text': '<p>It seems it can\'t be done that way. It will only provide meta information about the method and its parameters. Not the actual value at the time of the callstack. </p>\n\n<p>Some suggest deriving your classes from <a href="http://msdn.microsoft.com/en-us/library/system.contextboundobject.aspx" rel="noreferrer">ContextBoundObject</a> and use <a href="http://msdn.microsoft.com/en-us/library/system.runtime.remoting.messaging.imessagesink.aspx" rel="noreferrer">IMessageSink</a> to be notified off all method calls and the values of the parameters. This is normally used for <a href="http://en.wikipedia.org/wiki/.NET_Remoting" rel="noreferrer">.NET Remoting</a>.</p>\n\n<p>Another suggestion might be to write a debugger. This is how the IDE gets its information. Microsoft has <a href="http://blogs.msdn.com/jmstall/archive/2004/09/30/236281.aspx" rel="noreferrer">Mdbg</a> of which you can get the source code. Or write a <a href="http://msdn.microsoft.com/en-us/magazine/cc300553.aspx" rel="noreferrer">CLR profiler</a>.</p>\n'}, {'answer_id': 70553027, 'author': 'Marco Luzzara', 'author_id': 5587393, 'author_profile': 'https://Stackoverflow.com/users/5587393', 'pm_score': 1, 'selected': False, 'text': '<p>I am quite sure this is possible somehow, but I am not competent enough to give you the answer you wish. I am suggesting a different approach, less flexible but still useful if you have a stack trace in advance and you would like to see the arguments passed to each frame.</p>\n<p>Typically you would scatter log messages at the start of each method present in your stack trace, but that is pretty cumbersome and not always possible: what if you call methods defined in an external library?</p>\n<hr />\n<p>While I was searching for an inspiration on how to tackle this problem, I found a library called <a href="https://github.com/pardeike/Harmony" rel="nofollow noreferrer">Harmony</a>, that allows you to patch methods at runtime. Basically a method can be decorated with a prefix, a postfix and/or a finalizer. It is all very well explained in the documentation, but the idea you could use in order to provide details about methods in a stack trace is to create a <a href="https://harmony.pardeike.net/articles/patching-prefix.html" rel="nofollow noreferrer">prefix</a> that prints the parameters of each frame.</p>\n<p>Unfortunately, with Harmony only, I do not think it is possible to do this for the methods in the current <code>StackTrace</code>, as you would like to do, even using a postfix/finalizer. Nonetheless it can be done before the target method is called.</p>\n<hr />\n<p>I have created a very simple library called <code>DebugLogger</code> that internally uses Harmony and that simplifies the procedure. <a href="https://github.com/marco-luzzara/DebugLogger" rel="nofollow noreferrer">Here</a> is the repository, but you can also find it on Nuget:</p>\n<pre><code>dotnet add package DebugLogger --version 1.0.0\n</code></pre>\n<p>It still has many limitations, in part because of Harmony, there are just a bunch of tests and is not ready to be considered a library, but it is a good starting point in my opinion. I have prepared a <a href="https://dotnetfiddle.net/79dHkJ" rel="nofollow noreferrer">Fiddle</a> showing the initialization of <code>DebugLogger</code> and a couple of dummy classes to make the demo &quot;meaningful&quot;.</p>\n'}]
['https://Stackoverflow.com/questions/75076', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2422/']