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

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

CPA Technology, LLC
610-862-1998
101 W. Elm Street
Conshohocken, PA
The MediGroup, Ltd.
610-666-1955 x12
1308 Egypt Road
Oaks, PA
Deacom, Inc.
610-971-2278
950 West Valley Road, Suite 3000
Wayne, PA
Quintiq Inc.
610-964-8111
565 E Swedesford Rd #303
Wayne, PA
KasTech Consulting, Inc.
215-702-8155
116 N. Bellevue Avenue
Langhorne, PA
Cardy Multimedia
(610) 222-9626
257 Center Point Ln
Lansdale, PA
Roberts Webforge Inc
(215) 393-4757
Lansdale, PA
Data Expressway Graphics
(610) 807-9000
Bethlehem, PA
Swellnet Solutions
(215) 235-3005
443 W Girard Ave
Philadelphia, PA
Professional Web Concepts
(610) 519-9321
Bryn Mawr, PA

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

CPA Technology, LLC

Sage software, consulting & implementation, training (Sage Authorized Training Center)

610-862-1998
101 W. Elm Street
Conshohocken, PA
www.cpatechnology.net

CPA Technology is a leader in providing Sage software solutions to the construction and real estate industries in Eastern Pennsylvania, New Jersey, and Delaware.

www.cpatechnology.net

Related Local Events
Hot Java at the Watercooler
Dates: 12/23/2009 - 1/23/2010
Location: Odyssey Travel
Skippack, PA
View Details

IT Roundtable
Dates: 11/10/2009 - 11/10/2009
Location: D&E Communications
Brownstown, PA
View Details

Fraser Advanced Information Systems Mixer
Dates: 10/29/2009 - 10/29/2009
Location:
Bethlehem, PA
View Details

PREP 2009: 22nd International Symposium, Exhibit and Workshops
Dates: 7/19/2009 - 7/22/2009
Location: Loews Hotel
Philadelphia, PA
View Details