Those of you who have developed web applications using ASP before know how cumbersome debugging your application is. In ASP, debugging in a painful process that usually involves using the
Response.Write() method to output the values of variables. Ask yourself this: how many times have you forgotten to remove the debugging statements prior to deploying a web application?
Things have changed drastically with the launch of the .NET framework. In .NET, you can use the debugger in Visual Studio .NET to trace through the execution of your web application, or you can use the
Trace class in the
System.Web.TraceContext namespace. This article shows you how to use the
Trace class to aid in your debugging efforts.
ASP.NET includes the
Trace class to help trace the flow of an application. Instead of using the
Response object for debugging, you can now use the
Trace class to print out debugging information.
To illustrate this, let's create an ASP.NET web application and drag-and-drop a
Button and a
ListBox control onto the default
WebForm1 (see Figure 1). Populate the
ListBox control with three items and set the
AutoPostBack property to
|Figure 1. Populating the default
For this article, I would like to trace the execution flow of the application. First, to activate the trace, the page directive needs to have a
Trace attribute with its value set to
true (switch to View HTML Source mode), as shown in Figure 2.
|Figure 2. Enabling tracing|
Next, I injected
Trace statements into the
Form load event so that I know whether a postback has occurred. The
PostBack event is one of the confusing aspects of ASP.NET that often trips up a beginning ASP.NET developer.
Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load 'Put user code to initialize the page here Trace.Write("Page loaded") If Not IsPostBack Then Trace.Write("Not in a postback") ' do something when a postback occurs Else Trace.Write("In a postback") ' do something... End If End Sub
I am also interested to know if a
ListBox postback has occurred 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 ASP.NET application is executed, the following output is shown (see Figure 3).
|Figure 3. Displaying the trace information(click for larger image)|
As you can see, when
WebForm1 is loaded for the first time, you should see the strings "Page loaded" and "Not in a postback". If you click on the
Button control on
WebForm1, you should see the entry shown in Figure 4. Similarly, when the
ListBox is clicked, the string "Listbox postback" will also be displayed.
|Figure 4. Examining the trace information|
The trace page contains the following sections (not all are shown in Figure 3):
|Request Details||Describes information pertaining to the request, e.g. |
|Trace Information||Contains detailed information about the application currently running. Trace information is displayed in this section.|
|Control Tree||Displays information about controls used in a page and the size of the |
|Cookies Collection||Displays the cookie set by the page and its value.|
|Headers Collection||Displays HTTP header information, such as content length and user agent.|
|Forms Collection||Displays the name of controls in a page and their values.|
|Server variables||Displays the environment variables on the server side.|
Notice that our trace message is written under the
Trace Information section. To turn off tracing, simply set the
Trace attribute in the page directive to
False. There is no need to remove the trace instructions in our application; turning off debugging is now simply a matter of setting a Boolean value!
Turning tracing on and off is just a matter of modifying the value of the
Trace attribute in the page directive. You can also programmatically turn off tracing using the
Trace class contains the following members:
| ||Indicates whether tracing is enabled for the current request.|
| ||Sets the trace mode: |
| ||Writes the trace information in red.|
| ||Writes the trace information.|
To turn off tracing programmatically, use the following statement in the
WebForm1 load event:
Trace.IsEnabled = false
In our example, the trace information displayed is not obvious, as it is buried within other trace information. The
Warn() method of the
Trace class causes the trace information to be printed in red.
So instead of writing:
you could write:
Figure 5 shows the
Warn() method displaying debugging information in red.
|Figure 5. Using the
Inserting multiple trace statements into an application can sometimes be messy. It would be useful if the trace information could be classified into different categories to make tracing easier. The
Trace class allows us to categorize and sort the trace information based on categories.
The following example shows how to group categories of trace information:
Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Trace.TraceMode = TraceMode.SortByCategory 'Put user code to initialize the page here Trace.Warn("Page_Load", "Page loaded") If Not IsPostBack Then ' do something... 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 executed, the following debugging information is displayed and grouped according to category (see Figure 6).
|Figure 6. Sorting by category|
Let's dissect the above code:
Trace.TraceMode = TraceMode.SortByCategory
TraceMode property sets the modes supported by the trace:
SortByCategory: trace information is sorted by category.
SortByTime: trace information is displayed in the sequence of execution.
Since we are sorting the trace mode by category, notice that Figure 7 shows that the messages are sorted by category.
Trace.Warn("Page_Load", "Page loaded")
Warn property displays the message in red. Note that this method is overloaded. In this case, we pass in two arguments. The first goes into the
Category and the second is for the
Besides using the
Trace class to set the trace mode, you can also use the
Page directive to set the trace mode:
<%@ Page Language="vb" Trace="true" TraceMode="SortByCategory" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>
This last section discusses page tracing, which traces the flow of execution within a page. ASP.NET also supports tracing at the application level. Application-level tracing is set in the web.config file, under the
<trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true" />
To enable an application-level trace, set the following values:
| || ||Enables or disables application-level tracing.|
| || ||Sets the maximum number of requests to trace.|
| || ||Displays the trace at the end of the page.|
| || ||Trace information sort order.|
| || ||Sets the ability to see trace viewer on a non-local machine.|
When the application is loaded, the trace information does not appear on the page. To view the trace information, we need to use the trace viewer (
|Figure 7. Application-level tracing|
Figure 7 shows the trace information of the last six requests to the application. To view the detailed information of each request, click on the View Details link of each row.
Note that if the trace is set to
true in the web.config file and set to
false in the page directive, tracing is disabled.
ASP.NET has made it much easier to debug your web application. Now that you have learned how to use tracing, try it yourself and see how it improves your efficiency!
Wei-Meng Lee (Microsoft MVP) http://weimenglee.blogspot.com is a technologist and founder of Developer Learning Solutions http://www.developerlearningsolutions.com, a technology company specializing in hands-on training on the latest Microsoft technologies.
Return to ONDotnet.com
Copyright © 2009 O'Reilly Media, Inc.