Kieran Benton
Kieran Benton

Reputation: 8890

Reflecting local variables

I'm trying to find a way to automate some exception logging code to add to the stack information already available.

Is there any way to use reflection to retrieve the values of all variables on the stack (locals and parameters) - I sincerely doubt the names of the variables are available, but in many cases it would be useful to see the values.

Upvotes: 0

Views: 632

Answers (2)

Jason Haley
Jason Haley

Reputation: 3800

You might check out John Robbins' SUPERASSERT (SUPERASSERT Goes .Net), his book gives a great walkthrough of one way to do what you are after (plus a WHOLE lot more).

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062855

Not really. For this level of digging you'd probably need something like WinDbg.

If a specific variable is of interest, you can add it to the exception yourself (although even this introduces issues with duplicate keys, re-entrancy, etc):

    string dir = ...todo...
    try
    {
        // some code
    }
    catch (Exception ex)
    {
        ex.Data.Add("dir", dir);
        throw;
    }

Upvotes: 2

Related Questions