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

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

Apple Sales.com dba Mactown
(937) 435-0134
8975 Kingsridge Drive
Dayton, OH
Vtech Graphics
(937) 291-9620
Dayton, OH
Implications
(937) 427-1200
3220 Beaver Vu Dr
Dayton, OH
Ilead Llc
(440) 846-2346
20376 Kelsey Ln
Strongsville, OH
Black Technology Corp
(937) 233-0200
7391 Rustic Woods Dr
Huber Heights, OH
Purple Dingo Inc
(740) 633-1766
15 S 5th St
Martins Ferry, OH
Boudog Web Technologies
(614) 430-8211
667 Lakeview Plaza Blvd
Worthington, OH
Oleary Rob
(614) 228-2101
800 Cross Pointe Rd
Columbus, OH
2029 Web Design.Com
(614) 459-3589
1550 Old Henderson Rd
Columbus, OH
Noreign Studios
(614) 889-9410
5900 Cromdale Dr
Dublin, OH

 

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

Apple Sales.com dba Mactown

(937) 435-0134
8975 Kingsridge Drive
Dayton, OH

Related Local Event
Forge Fair 2010
Dates: 4/20/2010 - 4/22/2010
Location: Cincinnati Convention Center
Cincinnati, OH
View Details