Friday, September 20, 2024

How to Use the Trace Feature in ASP.NET

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 enable the Trace feature

To use the Trace feature, you must first enable tracing. To do that, you add a Trace attribute to the Page directive for the page, and you assign a value of True to this attribute. Then, when you run the page, trace information will be displayed at the end of the page output, as shown in figure 4-15.

When you enable the Trace feature, it is enabled only for the current page, which is usually what you want. To enable tracing for another page, you must modify the Page directive for that page too. Once this feature has been enabled for a page, ASP.NET adds trace output to the page whenever the page is requested.

How to interpret Trace output

In figure 4-15, you can see the start of the output for the Cart page after the user added an item to the shopping cart. After the request details, the trace information provides a list of trace messages that are generated as the application executes. Here, ASP.NET automatically adds Begin and End messages when major page events such as PreInit, Init, and InitComplete occur. If you scroll down to see all of these trace messages, you can see the variety of events that are raised during the life cycle of a page.

After the trace messages, you’ll find information about the controls used by the page, the items in the session state object, the cookies that were included with the HTTP request, the HTTP request headers, and the server variables. In this figure, for example, you can see the session state and cookies data for the Cart page of the Shopping Cart application. In this case, an item named Cart has been added to the session state object. And a cookie named ASP.NET_SessionId is used to keep track of the user’s session ID so the user’s session state object can be retrieved.

The beginning of the trace output for the Cart page

The session and cookies information for the Cart page

A Page directive that enables tracing for the Cart page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cart.aspx.cs" Inherits="Cart" Trace="True" %>

Description

    The ASP.NET Trace feature traces the execution of a page and displays trace information and other information at the bottom of that page.

    To activate the trace feature for a page, you add a Trace attribute to the Page directive at the top of the aspx file for the page and set its value to True as shown above.

    The trace information is divided into several tables that provide specific types of trace information. For example, the Trace Information table provides information about how the page request was processed, and the Session State table provides information about the items currently stored in session state.

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

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

Crafting a successful digital strategy for your business in 2023 rokture.