Skadoosh
Skadoosh

Reputation: 2623

How to get a dump of all local variables?

How can I get a dump of all local & session variables when an exception occurs? I was thinking of writing some sort of reflection based function that would interrogate the calling function & create a dump of variables & values.

Is there an existing library that I can use?

After speaking to a colleague, I was pointed to AOP or Aspect Oriented Programming. Here is what I understand, using AOP, one would simple decorate the methods & classes with certain attributes. The AOP framework then injects code in or around these classes & methods. There are two separate kinds of framework, one that injects code & then compiles the assembly & the second simply uses reflection & traps the call which you have decorated and wraps whatever code around the method at runtime.

I hope all that makes sense. I will be doing more research on this & post my approach.

Upvotes: 23

Views: 12828

Answers (3)

Pankaj
Pankaj

Reputation: 10095

You should not use Exception handling in Try Catch form. Rather, it should be

  1. Page Level Error
  2. Application Level error

Suppose You have a Presentation Layer and a Business Logic Layer/DataAccess Layer.

Upon facing the error in say Business Logic, it will move directly to Glogal.asax.cs file under Application_Error Event without going back to the calling function. Here you can log the error message like below....

HttpContext.Current.Server.GetLastError().InnerException.StackTrace
HttpContext.Current.Server.GetLastError().InnerException.Message
HttpContext.Current.Server.GetLastError().InnerException.Source
HttpContext.Current.Server.GetLastError().InnerException.TargetSite.DeclaringType.FullName
HttpContext.Current.Server.GetLastError().InnerException.TargetSite.DeclaringType.Name
HttpContext.Current.Server.GetLastError().InnerException.TargetSite.DeclaringType.Namespace

In case of page level error, Priority is the Page OnError Override and finally the Application Level error event. here also you can log errors.

I will prefer Application_error handler because If you have 20 modules and a situation come when you need to create baseclass for each module. It is not good to make code redundancy.

Now in the Web Config you can write code to redirect the user on some default page like below.

<customErrors defaultRedirect="ErrorPage.htm" mode="On">
   <error statusCode="404" redirect="ErrorPageNotFound.htm"/>
</customErrors>

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460058

I'm not sure if this is what you're looking for. But if you're in a catch-block you can get all fields and properties of this class in the following way:

try
{
    double d = 1 / 0;
}
catch (Exception ex)
{
    var trace = new System.Diagnostics.StackTrace();
    var frame = trace.GetFrame(1);
    var methodName = frame.GetMethod().Name;
    var properties = this.GetType().GetProperties();
    var fields = this.GetType().GetFields(); // public fields
    // for example:
    foreach (var prop in properties)
    {
        var value = prop.GetValue(this, null);
    }
    foreach (var field in fields)
    {
        var value = field.GetValue(this);
    }
    foreach (string key in Session) 
    {
        var value = Session[key];
    }
}

I've showed how to get the method name where the exception occured only for the sake of completeness.

With BindingFlags you can specify constraints, for example that you only want properties of this class and not from inherited:

Using GetProperties() with BindingFlags.DeclaredOnly in .NET Reflection

Of course the above should give you only a starting-point how to do it manually and you should encapsulate all into classes. I've never used it myself so it's untested.

Upvotes: 15

Gregory A Beamer
Gregory A Beamer

Reputation: 17010

This is a question asked ad nauseum on Stack Overflow, although phrased differently. In one thread, the answer was to use PostSharp. As others have suggested dumping the stack trace, you can do that. The easiest would be to manually dump the local variables. This can either be to Trace or you can create your own custom exception handler.

Upvotes: 0

Related Questions