Anyone who has developed a Web application using ASP before must know how troublesome it is to debug a Web application. In ASP, debugging is painful and usually involves printing the value of a variable using the Response.Write() method. So ask yourself: How many times have you forgotten to remove debugging statements before deploying your application?
This situation has completely changed with the advent of .NET Framework components. In .NET, you can trace the execution of an entire Web application using the debugger in Visual Studio .NET, or by using the Trace class in the System.Web.TraceContext namespace. This article demonstrates how to use the Trace class to assist your debugging work.
Using the Trace class
ASP.NET includes a Trace class that helps track application information flow. Instead of using the Response object for debugging, you can now use the Trace class to print out debugging information.
To demonstrate its use, we first build an ASP.NET Web application and place a Button and a ListBox control on the default WebForm1 (as shown in Figure 1). Fill the ListBox control with three items and set its AutoPostBack property to True.
![](https://images.downcodes.com/u/info_img/2009-06/30/257671.jpg) Figure 1. Populating the default WebForm1 |
For this article, I want to trace the execution flow of my application. First, activate tracking. The page instruction is required to contain the Trace attribute, and its value is set to True (switch to view HTML source mode), as shown in Figure 2.
![](https://images.downcodes.com/u/info_img/2009-06/30/257672.jpg) Figure 2. Activation tracking |
Next, I insert a Trace statement in the Form's load event so I know if Postback occurred. The PostBack event is one of the most confusing features in ASP.NET, and it often leads to failure for developers who are new to ASP.NET.
Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load 'Place the user code to initialize the page here Trace.Write("Page loaded") If Not IsPostBack Then Trace.Write("Not in a postback") 'Perform some operations when postback occursElse Trace.Write("In a postback") 'Perform some operationsEnd If End Sub |
I'd also like to know if postback occurs when a ListBox item is selected:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As _ System.Object, _ ByVal e As System.EventArgs) Handles _ ListBox1.SelectedIndexChanged Trace.Write("Listbox postback") End Sub |
When the above ASP.NET application executes, the following output will be displayed (shown in Figure 3):
![](https://images.downcodes.com/u/info_img/2009-06/30/257673.jpg) Figure 3. Displaying tracking information |
You can notice that when WebForm1 is loaded for the first time, you can see the strings "Page loaded" and "Not in a postback". If you click the button on WebForm1, you can see the record shown in Figure 4. Similarly, if the ListBox is clicked, the "Listbox postback" string will also be displayed.
![](https://images.downcodes.com/u/info_img/2009-06/30/257674.jpg) Figure 4. Check trace information |
The tracking page contains the following sections (not all information is shown in Figure 3):
Section | Description |
Request Details | Describes information related to the request, such as the conversation ID, encoding, and time of the request. |
Trace information | contains details of currently running applications. Tracking information is displayed in this section. |
The control tree | displays information about controls in a page and the size of Viewstate hidden fields. |
The Cookie collection | displays the cookies set by the page and its values. |
The header collection | displays HTTP header information such as content-length and user-agent. |
The Forms collection | displays the names of controls on a page and their values. |
Server Variables | displays server-side environment variables. |
Note that our tracking information appears below the "Tracking Information Section." If you want to turn off tracing, simply set the Trace attribute in the page directive to False. There is no need to remove trace instructions from your application, turning off debugging is now as simple as setting a boolean value.
Turning tracing on/off is simply a matter of modifying the value of the Trace attribute in the page directive. You can also turn off tracing programmatically using the Trace class. The members of the Trace class are as follows:
Property | description |
IsEnabled | indicates whether to activate tracking of the current request. |
TraceMode | sets the tracing mode: sortByCategory or sortByTime. |
Method | Description |
Warn | displays trace information in red. |
Write | trace information. |
To turn off tracking programmatically, use the following statement in the load event of WebForm1:
In our case, the tracking information is not displayed prominently, so it is buried by other tracking information. The Warn() method of the Trace class can print trace information in red. So instead of writing the code like this:
Trace.Write("Page loaded") |
Instead:
Trace.Warn("Page loaded") |
Figure 5 shows the debugging information displayed in red for the Warn() method.
![](https://images.downcodes.com/u/info_img/2009-06/30/257675.jpg) Figure 5. Using the Warn() method to display trace information in red |
Sort tracking information
Putting multiple tracing statements in an application can sometimes become cluttered. Tracking is easier if your tracking information can be divided into different categories. The Trace class allows us to classify and sort trace information based on category.
The following example demonstrates how to group tracking information by category:
Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Trace.TraceMode = TraceMode.SortByCategory 'Place the user code for initializing the page here Trace.Warn("Page_Load", "Page loaded") If Not IsPostBack Then 'Perform some operations Trace.Warn("Page_Load", "Not in a postback") Else Trace.Warn("Page_Load", "In a postback") End If End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender _ As System.Object, ByVal e As _ System.EventArgs) Handles _ ListBox1.SelectedIndexChanged Trace.Warn("ListBox", "Listbox postback") End Sub |
When executing this example, the following debugging information will be displayed, grouped by category (shown in Figure 6):
![](https://images.downcodes.com/u/info_img/2009-06/30/257676.jpg) Figure 6. Sort by category |
Let's analyze the above code:
Trace.TraceMode = TraceMode.SortByCategory |
The TraceMode property sets the modes supported by tracing:
· SortByCategory: Sort tracking information according to type.
· SortByTime: Display trace information according to execution order.
Because we selected the sorting by category mode, Figure 7 shows the information sorted by category.
Trace.Warn("Page_Load", "Page loaded") |
The Warn attribute displays the message in red. Note that this is an overloaded method. In the example, we pass it two parameters. The first input category (Category), the second parameter is to get the message (Message).
In addition to setting the trace mode using the Trace class, you can also specify the trace mode using page directives:
<%@ Page Language="vb" Trace="true" TraceMode="SortByCategory" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %> |
Application Trace
The final section discusses page tracking, which tracks the flow of execution information within a page. ASP.NET also supports application-level tracing. Application-level tracing is set in the web.config file, under the trace section:
To activate application-level tracing, set the following values:
Property | Value | Description |
Enabled | True | Activates or disables application-level tracing. |
requestLimit | 10 | sets the maximum number of requests to track. |
pageOutput | False | displays tracking information at the end of the page. |
traceMode | sortByTime | trace information sorting method. |
localOnly | True | sets the ability to view browser traces on non-local computers. |
When the application is loaded, the tracking information is not displayed on the page. In order to view trace information, we need to use the trace viewer (trace.axd):
![](https://images.downcodes.com/u/info_img/2009-06/30/257677.jpg) Figure 7. Application-level tracing |
Figure 7 shows the trace information for the application's last six requests. To view details about each request, click the "View Details" link for each row.
Note that if trace is set to true in the web.config file and it is set to false in the page directive, tracing will be disabled.
Summarize
ASP.NET makes debugging web applications much easier. Now that you know how to use tracking, give it a try and see how much it improves your productivity!