.NET Tip: Debugging: Dynamically Determining the Name of the Current Function

Determining function names dynamically can simplify your debugging and trace logic as well as ensure that your trace messages match the actual code that generated them. All you have to do is wrap all your calls to Trace() with your own custom function. This utility function then can reference the stack frame to find the name of the function that called it. Here is the utility function:

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

This function takes the message to output to Trace() and replaces the '{0}' in the message string with the name of the function that called TraceContext(). That is really all there is to it. To test the function, I created a form and added a button. In the button's click event, I call TraceContext() and call another function that also calls TraceContext(). Here is the sample code for calling TraceContext():

private void button1_Click(object sender, EventArgs e)
{
   TraceContext("In event handler: {0}");
   DynamicNameTest();
}

private void DynamicNameTest()
{
   TraceContext("In function: {0}");
}

Here is the trace output when the button is clicked:

In event handler: button1_Click
In function: DynamicNameTest

You now can have confidence in your trace output because you do not have any hard-coded function names in your messages.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.



Comments

  • Alternative

    Posted by jmcilhinney on 09/11/2007 09:14am

    System.Reflection.MethodBase.GetCurrentMethod().Name will give you the name of the current method.

    Reply
  • Clear and precise explanation

    Posted by JonnyPoet on 09/09/2007 01:16pm

    Very clear description of how to do,makes debugging easier

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • VMware vCloud® Government Service provided by Carpathia® is an enterprise-class hybrid cloud service that delivers the tried and tested VMware capabilities widely used by government organizations today, with the added security and compliance assurance of FedRAMP authorization. The hybrid cloud is becoming more and more prevalent – in fact, nearly three-fourths of large enterprises expect to have hybrid deployments by 2015, according to a recent Gartner analyst report. Learn about the benefits of …

Most Popular Programming Stories

More for Developers

RSS Feeds