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 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
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
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
Showing messages 1 through 1 of 1.
2003-09-27 08:08:24 anonymous2 [View]