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

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

Jade Penguin Web Development
(760) 240-7667
2330 0 Box
Apple Valley, CA
Capital Web Solutions
(916) 972-1210
Sacramento, CA
Isp Solutions
(909) 823-2326
16989 Valley Blvd
Fontana, CA
Morgan Multi Media Inc
(408) 984-4600
4675 Stevens Creek Blvd
Santa Clara, CA
Peoplenet International Corp
(408) 988-1888
5201 Great America Pkwy Ste 239
Santa Clara, CA
Mountain View Computers
(530) 587-2213
15894 Wellington Way
Truckee, CA
Spectrum Marketing Communications
(949) 389-9016
Aliso Viejo, CA
Thinking2
(805) 495-8213
223 E Thousand Oaks Blvd
Thousand Oaks, CA
Hyperarts Web Design
(510) 339-6084
201 4th St
Oakland, CA
Roth Suzie Design
(310) 829-0180
2932 Nebraska Ave
Santa Monica, CA

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

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

DATAGENIX

1-888-458-2033
5225 Canyon Crest Drive Suite 355
Riverside, CA
http://www.datagenix.com

Related Local Events
West Coast Expos. Computer Fair (Ponoma Exhibition)
Dates: 7/12/2008 - 7/13/2008
Location: Fairplex Exposition Complex (Building 7)
Pomona CA
View Details

West Coast Expos. Computer Fair (Ponoma Exhibition)
Dates: 6/7/2008 - 6/8/2008
Location: Fairplex Exposition Complex (Building 6)
Pomona CA
View Details
Rate Article
     
Articles Insider

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Advertising Engineering Home Services Retail & Consumer Services
Business Services Entertainment Industrial Goods & Services Software
Career Family Insurance Technology
Cars Financial Services Internet Telecommunications
Computer Hardware Food & Beverage Legal Transportation & Logistics
Construction Health Pets Travel
Education Home Electronics Real Estate Wedding