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.

2 Comments

Comments have been disabled for this content.