How to Use Tracepoints

    September 11, 2006

This article is an excerpt from the book: Murach’s ASP.NET 2.0 Web Programming with C# 2005.

In addition to breakpoints, Visual Studio 2005 provides a new feature called tracepoints. A tracepoint is a special type of breakpoint that performs an action when it’s encountered. Figure 4-10 shows how tracepoints work.

To set a tracepoint, you use the When Breakpoint Is Hit dialog box to indicate what you want to do when the tracepoint is encountered or “hit.” In most cases, you’ll use the Print a Message option to display a message in the Output window. As indicated in this dialog box, the message can include variable values and other expressions as well as special keywords.

For example, the message shown here will include the value of the SelectedValue property of the ddlProducts control. You can see the output from this tracepoint in the Output window in this figure. Here, the first tracepoint message was displayed the first time the page was requested. The second message was displayed when a product was selected from the drop-down list. And the third message was displayed when a quantity was entered and the Add to Cart button was clicked.

Notice that the Output window is also used to display Visual Studio messages like the first, second, and fifth messages shown in this figure. Because of that, this window is displayed automatically when you run an application. If you ever close it and want to reopen it without running the application, however, you can do that using the View > Output command.

To run a macro when a tracepoint is encountered, you select the Run a Macro option. Then, the drop-down list becomes available and you can select the macro you want to run from this list.

By default, program execution continues after the tracepoint action is performed. If that’s not what you want, you can remove the check mark from the Continue Execution option. Then, the program will enter break mode when the tracepoint action is complete.

After you set a tracepoint on a statement, the statement will be highlighted and a breakpoint indicator will appear in the margin. If program execution will continue after the tracepoint action is performed, the indicator will appear as a large diamond. If the program will enter break mode, however, the same indicator is used as for a standard breakpoint.

The Order page with a tracepoint and the dialog box used to set it

Output from the tracepoint in the Output window


    A tracepoint is a special type of breakpoint that lets you perform an action. When ASP.NET encounters a tracepoint, it performs the specified action and then continues execution if the Continue Execution option is selected or enters break mode if it isn’t.

    You typically use tracepoints to print messages to the Output window. A message can include text, values, and special keywords. You can also use tracepoints to run macros.

    To set a tracepoint, right-click on the statement where you want it set and choose Breakpoint > Insert Tracepoint. Then, complete the When Breakpoint Is Hit dialog box and click OK. You can also convert an existing breakpoint to a tracepoint by rightclicking on its indicator and choosing When Hit.

    If program execution will continue after the tracepoint action is performed, the tracepoint will be marked with a large diamond as shown above. Otherwise, it will be marked just like any other breakpoint.

Add to | Digg | Yahoo! My Web | Furl

Bookmark WebProNews:

Joel Murach has been writing and editing for more than 10 years. During that time, he sharpened his programming skills as a contract programmer in San Francisco and his instructional skills as a trainer for HarperCollins Publishing. He always brings a vision to his projects that leads to improved effectiveness for his readers.