« AJAX Without ASP.NET AJAX | Main | How To: Return Emedded Resource Content as String »

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"

Posted on Sunday, July 8, 2007 at 07:28PM by Registered CommenterJ. Michael Palermo IV in , | CommentsPost a Comment | References1 Reference

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (1)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    I'm a big fan of tracing in .NET and use it in every project I work on since it's a great way to find

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.
Editor Permission Required
You must have editing permission for this entry in order to post comments.