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

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

InBios International
(206) 344-5821
562 First Avenue S., Ste. 600
Seattle, WA
MITS
(206) 789-8313
701 5th Ave.,
Seattle, WA
TA Netgables Pvt Ltd
1 206 623 0910
216 First Avenue South,
Seattle,, WA
Control Systems PNW, Inc.
(206) 344-5929
900 4th Ave.,
Seattle, WA
Benaroya Research Institute at Virginia Mason
(206) 341-1313
1201 9th Ave.
Seattle, WA
Advanced Computer Solutions Ltd.
(206) 624-7854
500 Union St.,
Seattle, WA
Envision Telephony, Inc.
(206) 621-9384
520 Pike St.,
Seattle, WA
International Business Machines
(800) 426-4968
1200 5th Ave.,
Seattle, WA
Information Miner
(206) 652-3425
601 Union St
Seattle, WA
Onvia, Inc.
(206) 282-5170
509 Olive Way
Seattle, WA

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

InBios International

(206) 344-5821
562 First Avenue S., Ste. 600
Seattle, WA
http://www.inbios.com/

Related Local Events
Leads Group: Wired
Dates: 11/26/2009 - 11/26/2009
Location: Mud Bay Coffee
Olympia, WA
View Details

Leads Group: Wired
Dates: 12/3/2009 - 12/3/2009
Location: Mud Bay Coffee
Olympia, WA
View Details

Leads Group: Wired
Dates: 12/13/2009 - 12/13/2009
Location: Mud Bay Coffee
Olympia, WA
View Details

Leads Group: Wired
Dates: 12/24/2009 - 12/24/2009
Location: Mud Bay Coffee
Olympia, WA
View Details

Leads Group: Wired
Dates: 12/31/2009 - 12/31/2009
Location: Mud Bay Coffee
Olympia, WA
View Details