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 8, 2007 10:04 PM by Palermo4
Filed under: , , ,

Comments

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

Monday, July 9, 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 9, 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 9, 2007 3:13 AM by RoyOsherove

also:

MethodBase.GetCurrentMethod().Name also works.

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

Monday, July 9, 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 9, 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

Friday, October 5, 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 );

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

Thursday, January 21, 2010 3:16 PM by Sam

You could use a delegate to get the name of one method from another method.

string methodName=(new Func<MyReturnType>(MyMethod)).Method.Name;

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

Thursday, February 9, 2012 6:04 AM by Darren

This would be great if it worked but this technique of getting the current method cannot be relied on because the compiler may 'inline' a particular method so the top of your stack is not actually the method you expect (it's an ancestor).

Search for ".net inlining" for a full explanation.

Leave a Comment

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