How to Test and Debug an ASP.NET Application—How to create custom trace messages
|Visual C# Tutorials|
© 2006 Mike Murach & Assoc.
How to create custom trace messages
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.
Figure 4-16 How to create custom trace messages
Common members of the TraceContext class
| ||True if tracing is enabled for the page.|
| ||Writes a message to the trace output.|
| ||Writes a message to the trace output with the specified category.|
| ||Writes a message in red type to the trace output.|
| ||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.");
• 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.