Advanced Testing and Debugging of an ASP.NET 2.0 Application - How to create custom trace messages
(Page 3 of 4 )
In some cases, you may want to add your own messages to the trace information that’s generated by the Trace feature. This can help you track the sequence in which the methods of a form are executed or the changes in the data as the methods are executed. Although you can also do this type of tracking by stepping through the methods of a form with the debugger, the trace information gives you a static listing of your messages.
Note, however, that you can also create this type of listing using tracepoints as described earlier in this chapter. The advantage to using tracepoints is that you can generate trace information without adding code to your application. In addition, this output is generated only when you run an application with debugging. In contrast, you have to add program code to add custom trace messages, and the trace output is generated whenever the Trace feature is enabled. Because of that, you may not want to create custom trace messages. But I’ve included it here in case you do.
To add messages to the trace information, you use the Write or Warn method of the TraceContext object. This is summarized in figure 4-16. The only difference between these two methods is that messages created with the Warn method appear in red. Notice that to refer to the TraceContext object, you use the Trace property of the page.
When you code a Write or Warn method, you can include both a category and a message or just a message. If you include just a message, the category column in the trace output is left blank. If you include a category, however, it appears as shown in this figure. In most cases, you’ll include a category because it makes it easy to see the sequence in which the methods were executed.
If you want to determine whether tracing is enabled before executing a Write or Warn method, you can use the IsEnabled property of the TraceContext object as shown in the example in this figure. Normally, though, you won’t check the IsEnabled property because trace statements are executed only if tracing is enabled.
Common members of the TraceContext class
Property | Description |
IsEnabled | True if tracing is enabled for the page. |
Method | Description |
Write(message) | Writes a message to the trace output. |
Write(category, message) | Writes a message to the trace output with the specified category. |
Warn(message) | Writes a message in red type to the trace output. |
Warn(category, message) | Writes a message in red type to the trace output with the specified category. |
Code that writes a custom trace message
if (Trace.IsEnabled)
Trace.Write("Page_Load", "Binding products drop-down list.");
A portion of a trace that includes a custom message

Figure 4-16. How to create custom trace messages
Description
- You can use the TraceContext object to write your own messages to the trace output. The TraceContext object is available through the Trace property of a page.
- Use the Write method to write a basic text message. Use the Warn method to write a message in red type.
- Trace messages are written only if tracing is enabled for the page. To determine whether tracing is enabled, you use the IsEnabled property of the TraceContext object.
Next: How to write information directly to the HTTP output stream >>
More ASP.NET Articles
More By Murach Publishing
|
This article is excerpted from chapter four of the book Murach's ASP.NET 2.0 Web Programming with VB 2005, written by Joel Murach and Anne Boehm (Murach, 2006; ISBN: 1-890774-32-4). Check it out today at your favorite bookstore. Buy this book now.
|
|