Debugging ASP.NET
Pages: 1, 2
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.
The Trace class contains the following members:
| Property | Description |
|---|---|
IsEnabled | Indicates whether tracing is enabled for the current request. |
TraceMode | Sets the trace mode: sortByCategory or sortByTime. |
| Methods | Description |
|---|---|
Warn | Writes the trace information in red. |
Write | 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:
Trace.Write("Page loaded")
you could write:
Trace.Warn("Page loaded")
Figure 5 shows the Warn() method displaying debugging information in red.
![]() |
Figure 5. Using the Warn() method to display trace information in red |
Sorting the Trace Information
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
The 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")
The 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 Message.
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" %>
Application Tracing
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 section:
<trace enabled="false"
requestLimit="10"
pageOutput="false"
traceMode="SortByTime"
localOnly="true"
/>
To enable an application-level trace, set the following values:
| Attribute | Value | Description |
|---|---|---|
Enabled | True | Enables or disables application-level tracing. |
requestLimit | 10 | Sets the maximum number of requests to trace. |
pageOutput | False | Displays the trace at the end of the page. |
traceMode | sortByTime | Trace information sort order. |
localOnly | True | 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 (trace.axd):
![]() |
| 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.
Summary
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
-
http://www.codon4.com
2003-09-27 08:08:24 anonymous2 [View]




