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"

10 Comments

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

    Any chance?

  • Uwe,

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

  • also:
    MethodBase.GetCurrentMethod().Name also works.

  • 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

  • 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!

  • 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?

  • U can use this to get the current method name

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

  • 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 );





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

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

  • 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.

Comments have been disabled for this content.