Where were I? (StackTrace Class)

On my current project we wished to impersonate a WebService for test purposes and decided to record live messages using XmlSerialization. For the first few messages I manually implemented code to serialize to a file. This cut-and-paste reuse obviously had to be refactored but I was unhappy manually passing in the filename to every call of the new method since the files are named the same as the webmethod. Somehow the generic serialization code needed to know who called it, or to put it in Jerry’s terms it had to ask ‘Were were I?’. Jerry replied to my first ever blog post and finally I got a chance to use his suggestion:



https://dotnetworkaholic.com/PermaLink,guid,692ca362-89cb-4dad-985f-92aea92ca96b.aspx




http://www.codeproject.com/csharp/customtracelistener.asp

The crux is the StackTrace class which resides in the System.Diagnostics namespace. It is exactly as you might expect – very simple when you know it exists but I believe it would be hard to find using Google etc.

This is the code snippet I used after re-reading Jerry’s Code Project article:

   #region Get the calling method (“Where were I” – courtesy of Jerry Dennany)
   System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
   string methodName = “unknown”;
   if (st.FrameCount > 0)
   {
    System.Diagnostics.StackFrame sf = st.GetFrame(1);
    methodName       = sf.GetMethod().Name;
   }
   #endregion

Being a stickler for simplicity I read the MSDN help and reduced the code to:

   #region Get the calling method name (“Where were I” – courtesy of Jerry Dennany)
   System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, false);
   string methodName = st.GetFrame(0).GetMethod().Name;
   #endregion

I am not sure that my employer will be sending a Mr. Dennany a check anytime soon, but I appreciated the helpful article. This is a good reminder of why we ‘waste’ so much time with blogs.

 

 

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Where were I? (StackTrace Class)

  1. Ha!

    The "Where were I?" actually has a story behind it. CodeProject requires (or used to require) "editing" before an article was officially accepted. My version had the subtitle "Where was I?" Some ‘helpful’ editor changed it to "Where were I?", and we now get the version of the article as it exists. It always annoyed me, but I never really bothered to have it corrected.

    So, I’m glad that I could be of help!

  2. Paul Lockwood says:

    I found ‘Where were I’ to be quite amusing 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s