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

Are you tired of hard-coding function names in trace messages? Have you ever used cut and paste to copy a trace statement and forgot to change the function name? There'll be no more wild goose chases because of bad function names when you determine the function name dynamically.

Local Companies

Developware, Inc.
1.877.280.2392
Bakery Square
Louisville, KY
Epicor Software Corporation
502-412-7666
9100 Shelbyville RD
Louisville, KY
Oasis Computer Solutions
502-429-6902
9000 Wessex Pl
Louisville, KY
Epicor Software
502-339-7748
8700 Westport RD
Louisville, KY
Information Systems Solutions Inc
502-245-7003
12730 Townepark Way
Louisville, KY
National Innovative Software Solutions LLC
502-495-0548
2108 Plantside DR
Louisville, KY
Allscripts Healthcare Solutions
502-412-1102
1302 Clear Springs Trce
Louisville, KY
Bellwether Software
502-426-5463
9900 Shelbyville Rd
Louisville, KY
Halo Group, Inc.
502-657-6468
9900 Corporate Campus Drive
Louisville, KY
Power Creative Inc
(502) 267-0772
Louisville, KY

provided by: 
Originally published at Internet.com


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.

Author: Jay Miller

Read article at Internet.com site

Featured Local Company

Developware, Inc.

1.877.280.2392
Bakery Square
Louisville, KY
www.developware.com

Related Local Event
SUR/FIN - Annual Technical Conference and Exhibit of Surface Finishing
Dates: 6/16/2009 - 6/18/2009
Location: Kentucky International Convention Center
Louisville, KY
View Details