Logging local variables in the method

Topics: Building and extending application blocks, Exception Handling Application Block
Jul 8, 2008 at 9:57 AM
Hi!
I want add functionality to ExceptionPolicy.HandleException so in catch it would read local variables value and log them, like this:

pulic void myMethod()
{
    SomeTypeName var1=new SomeTypeName();
.    ....
    catch(Exception ex)
    {
            bool rethrow = ExceptionPolicy.HandleException(ex, "Default Exception Policy",this,"SomeTypeName");

            if (rethrow)

            {

                 throw;

            }
    }
}

As you see I give a reference to current object - this - and name or names of types I am interested in to log in and then ExceptionPolicy.HandleException would use reflection or something else and read these values and add them to ex.Data directory, so my problem is how I can access these values, I have tried with 
foreach (FieldInfo var in t.GetFields) or
foreach (LocalVariableInfo var in ex.TargetSite.GetMethodBody().LocalVariables),
but yhe only informations available are stuff like the type and name.. you know stuff I already know as developer of the application!
I have tried last two days without result and really hope some of these Microsoft guys can help.

Regards
Shahram 

 

 

Jul 8, 2008 at 1:44 PM
Hi,

You're probably looking for PropertyInfo.GetValue and FieldInfo.GetValue; there are several uses of those in EntLib. You will need to be careful with indexers, write-only properties and access to non-public properties in partial trust.

Fernando

Jul 8, 2008 at 5:50 PM
Hi,
Thanks for the answer.
I am aware of this methods but non of them are usable to access value of local variables in a method which caused the exception/contains the catch clause
 Like example above, SomeTypeName var1=new SomeTypeName() in myMethod().

Regards

Jul 8, 2008 at 6:16 PM

I'm sorry, I misunderstood you because of the FieldInfo code snippet.

I don't think you can do this in general, particularly if your binaries are optimized. There was a similar question some time ago in the msdn forums, it might provide you additional insight http://forums.msdn.microsoft.com/en/csharpgeneral/thread/0b094906-daf2-4091-bbfe-d977b75eba53/.

You may also find these posts useful http://blogs.msdn.com/jmstall/archive/2005/03/20/399287.aspx and http://iridescence.no/Posts/GettingtheCurrentStackTrace.aspx.

Fernando


shahram wrote:
Hi,
Thanks for the answer.
I am aware of this methods but non of them are usable to access value of local variables in a method which caused the exception/contains the catch clause
 Like example above, SomeTypeName var1=new SomeTypeName() in myMethod().

Regards




Jul 9, 2008 at 8:30 AM
Thank you for the answer, so in short local variable values are not accessible by reflection in .Net. Well in that case the only way is to pass references to exception handling method I guess.

/shahram
Jul 9, 2008 at 3:13 PM
Well, you could store your context information in the Exception.Data property before invoking the ExceptionPolicy.

Fernando

shahram wrote:
Thank you for the answer, so in short local variable values are not accessible by reflection in .Net. Well in that case the only way is to pass references to exception handling method I guess.

/shahram


Jul 9, 2008 at 5:56 PM
Edited Jul 9, 2008 at 5:58 PM
Yes,that is exactly what I do now, but it is a sham it is not possible by reflection i .Net.
Aug 13, 2008 at 9:40 AM
Hi

I am attempting the exact same thing, logging method parameter values when an exception is raised.  Storing context information in the Exception.Data property, as suggested by Shahram, seems to be a good option.  I am however having difficulty logging my exception when I add entries to the Exception.Data collection.

My Exception Handling Application Block (EHAP) has a policy named GeneralPolicy, which contains a Logging Handler and a Replace Handler.  The Logging Application Block will in turn write log entries to a text file, making use of the default Flat File Trace Listener and Text Formatter.

When I request the EHAP to handle an exception, the exception is written to the log file, replaced and later on shown to the user.
            catch (Exception ex)
            {
                Boolean rethrow = ExceptionPolicy.HandleException(ex, "GeneralPolicy");
                if (rethrow)
                    throw;
            }

My problem is that when I add items to the Exception.Data collection, my exception is not recorded in the log file at all.  It is however replaced and later on displayed.
            catch (Exception ex)
            {
                ex.Data.Add("BankItemID", BankItemID);
                ex.Data.Add("Principal", Principal);

                Boolean rethrow = ExceptionPolicy.HandleException(ex, "GeneralPolicy");
                if (rethrow)
                    throw;
            }

Any idea what I might be doing wrong?

Thank you!
Aug 13, 2008 at 3:29 PM
You can check at EntLib's instrumentation to troubleshoot this issue; both EHAB's and the Logging Block's instrumentation should help you.

You may also want to try adding a breakpoint on the logging handler to determine if it's a problem at the exception handling level or if it's happening down into the logging block.

Fernando
Sep 5, 2008 at 9:29 AM
Thanks for the reply Fernando.

I have been spending little time on this project.  I have had a hard time debugging the enterprise library.  I created a new test project this morning and was able to log exceptions even when I add elements to the Exception.Data collection.  The only changes to the Enterprise Library that I made was to uninstall and re-install (due to other reasons).  I'm not sure whether this is a contributing factor.

If I encounter this problem again I'll get to the bottom of it and document my findings here.

Thanks
Sep 5, 2008 at 1:03 PM
Thanks for the update.

Fernando