How To: Obtain Method Name Programmatically For Tracing

I am not a fan of hard-coding method names in exception or trace messages.  Here is a utility method to allow access to method name at runtime:

public static void TraceContext(string messageFormat)
{
    Trace.WriteLine(string.Format(messageFormat, 
        new System.Diagnostics.StackFrame(1).GetMethod().Name));
}

If I call the method above from inside another method:

protected void Application_AuthorizeRequest(object sender, EventArgs e)
{
    Tools.TraceContext("Inside of {0} event handler");
}

The resulting output is: 

"Inside of Application_AuthorizeRequest event handler"

Published Sunday, July 08, 2007 10:04 PM by Palermo4
Filed under: , , ,

Comments

# re: How To: Obtain Method Name Programmatically For Tracing

Monday, July 09, 2007 12:41 AM by Uwe

I would love to be able trace the actual parameter values, too, but never found a way of doing this.

Any chance?

# re: How To: Obtain Method Name Programmatically For Tracing

Monday, July 09, 2007 2:48 AM by Palermo4

Uwe,

To my knowledge, there is not a way to get the value of the parameter from ParameterInfo.

# re: How To: Obtain Method Name Programmatically For Tracing

Monday, July 09, 2007 3:13 AM by RoyOsherove

also:

MethodBase.GetCurrentMethod().Name also works.

# re: How To: Obtain Method Name Programmatically For Tracing

Monday, July 09, 2007 3:52 PM by Will Ballard

Forcing a stack to build to get a method name is a pretty hefty runtime -- you planning on using that solution in high traffic paths, or off to the side.

Take a peek in Reflector at System.Diagnostics.StackTrace.CaptureStackTrace

# re: How To: Obtain Method Name Programmatically For Tracing

Monday, July 09, 2007 6:40 PM by Palermo4

Roy,

MethodBase.GetCurrentMethod() is a great tip/trick.  I wish it let me get the method that executed the current method like my implementation supports.  Oh well!

# re: How To: Obtain Method Name Programmatically For Tracing

Monday, July 09, 2007 6:47 PM by Palermo4

Will,

I am not quite sure why you pointed me to Diagnostics.StackTrace.CaptureStackTrace?  In looking at the constructor for StackFrame, I did not see the overhead or call to the internal method you are referencing.  From my investigation, there is little overhead in using what I suggested.

# re: How To: Obtain Method Name Programmatically For Tracing

Friday, October 05, 2007 9:00 PM by Todd

Anybody know how to get a method name when you're not in that method?  I know, it sounds strange, but here's what I'm looking for...

There are many examples of "on the fly" delegate creation classes to simplify the creation of multiple delegates, i.e. to allow the creation of delegates at runtime (using reflection.)  Usually you pass in the Control as an object and the method name as a string, and all works great.  The problem is refactoring...  Since your method name is a string, if you refactor the method, your delegate creation will get broken, and you won't know until runtime.  If we could just pass in the name of the method captured at runtime, refactoring would no longer be an issue.  Something like MethodBase.GetMethod(MyControl.MyMethod).Name?

Any ideas?

# re: How To: Obtain Method Name Programmatically For Tracing

Monday, December 10, 2007 2:42 PM by kannan

U can use this to get the current method name

System.Reflection.MethodBase.GetCurrentMethod().Name.ToString()

# re: How To: Obtain Method Name Programmatically For Tracing

Friday, February 20, 2009 5:14 AM by ecilaro

Create a Delegate for that method

ex

private void Button1_Click( object sender, EventArgs e )

{

//....

}

///....

MessageBox.Show( new EventHandler( Button1_Click ).Method.Name );

           MessageBox.Show( MethodBase.GetCurrentMethod().Name );

Leave a Comment

(required) 
(required) 
(optional)
(required)