Wednesday, November 13, 2024

ASP.NET: How to Create Custom Trace Messages

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

The Trace feature is an ASP.NET feature that displays some useful information that you can’t get by using the debugger. Because the debugger works so well, you probably won’t need to use the Trace feature very much, but you should at least be aware of the information that it can provide…

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.

Common members of the TraceContext class

Property

IsEnabled – True if tracing is enabled for the page.

Method

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

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.

Add to Del.icio.us | Digg | Yahoo! My Web | Furl

Bookmark Murdok:

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.

Related Articles

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

Location based geofenced device advertising (all states).