Trace Points in Visual Studio 2008

This is a little piece of functionality I stumbled across in Visual Studio 2008.

You can set up a “Trace Point”, which is like a breakpoint that doesn’t break, it logs a trace message to the debug trace listener.

Here’s how to set one up:

1: Set a breakpoint on the line where you want to log the trace

CropperCapture[5] 

2: Right click the breakpoint, and select “When Hit…”

CropperCapture[1] 

3: In the dialog, enter the message

Variables can be logged using “{}”, as specified in the dialog instructions.

CropperCapture[6]

 

Now the code shows a “diamond” instead of circle for the trace point:

CropperCapture[7]

 

4: Finally, run the code and see the trace messages in the Debug Output (in the Output window in Visual Studio)

CropperCapture[8]

 

This is really useful when you have some complicated looping or recursion that you need to debug; stepping through takes a lot of time and is confusing, you lose the forest for the trees.  But if you can spool out traces you can see exactly what has happened after the code is done running, and run it multiple times with different input to see how it changes.

A few notes:

  • The trace message is logged before the line is evaluated – so in the sample code, I am tracing the value of “depth” before 1 is added to it.  If you want to get the value after execution of the line, set the trace to the line below it.
  • These trace points are only as stable as breakpoints, as they are stored in your VS user options file.  If the code structure is modified outside of your editor (such as by another developer, then updated through source control), the positions can be thrown off and the traces will not work.  If you need stable tracing, use System.Diagnostics.Trace.Write in an explicit call in your code; trace points are really only for specific debugging efforts.
Published Thursday, January 29, 2009 9:24 PM by gstarbuck

Comments

# re: Trace Points in Visual Studio 2008@ Monday, October 03, 2011 3:48 PM

Nice gem Guy.  How's it been going since you left Stericycle?

-Kevin

by Kevin Earley

# re: Trace Points in Visual Studio 2008@ Friday, February 01, 2013 8:43 AM

i love cougars which is why i love to watch Desperate Housewives and

also Cougar Town“

by Ketchum