Web applications developed using ASPProgramYou must know how troublesome it is to debug web applications. In ASP, debugging is painful and usually includes the value of the variable output using the response. Write () method. Excuse me: How many times have you forgotten to delete the debugging statement before deploying the application? With the emergence of. NET Framework components, this situation has undergone a thorough change. In. net, you can use the debugging program in Visual Studio. NET to track the execution of the entire web application, or use the trace class in the system. Web. tracecontext namespace. This article demonstrates how to use the trace class to assist your debugging. Use trace class ASP. NET contains a trace class that helps track application information flows. Instead of using the response object for debugging, you can now use the trace class to print debugging information. To demonstrate its usage, we first create an ASP. NET web application and place a button and a ListBox control (1) on the default webform1 ). Fill the ListBox control with three items and set its autopostback attribute to true. Figure 1. Fill in the default webform1 |
For this article, I want to track the execution flow of the application. First, activate the trace. The page command must contain the trace attribute and its value is set to true (switch to view the HTML source mode), as shown in figure 2.
Figure 2. Activate a trail |
Next, I insert the trace statement in the form load event so that I can know whether PostBack has occurred. PostBack events are the most confusing feature in ASP. NET, which often leads to the failure of developers who use ASP. NET.
Private sub page_load (byval sender as system. Object ,_ Byval e as system. eventargs )_ Handles mybase. Load 'Users who place the initialization page hereCode Trace. Write ("Page loaded ") If not ispostback then Trace. Write ("not in a PostBack ") 'Perform some operations when a PostBack occurs. Else Trace. Write ("in a PostBack ") 'Perform some operations End if End sub |
I also want to know whether PostBack occurs when the ListBox data item is selected:
Private sub listboxincluselectedindexchanged (byval sender _ System. Object ,_ Byval e as system. eventargs) handles _ Listbox1.selectedindexchanged Trace. Write ("ListBox PostBack ") End sub |
When the preceding ASP. NET application is executed, the following output is displayed (Figure 3 ):
Figure 3. Show trace information |
You can find that when loading webform1 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 you click ListBox, the "ListBox PostBack" string is displayed.
Figure 4. Check tracking information |
The trail page contains the following parts (not shown in figure 3 ):
Segment |
Description |
Request details |
Describes the request-related information, such as the conversation ID, encoding, and request time. |
Tracking Information |
Contains the details of the currently running application. The trace information is displayed in this section. |
Control tree |
Displays the control information and the size of the viewstate hidden field on a page. |
Cookie set |
Display the page and the cookie set by its value. |
Header set |
Displays HTTP header information, such as content length and user agent. |
Form set |
Displays the names and values of controls on a page. |
Server Variables |
Displays the environment variables on the server. |
Note that our tracking information is displayed under the "tracking information segment. To disable tracing, simply set the trace attribute in the page command to false. There is no need to delete the trace commands in the application. Now disabling debugging is just as easy as setting a Boolean value.
Opening or disabling a trail simply modifies the trace attribute value in the page directive. You can also use the trace class programming to disable tracing. The trace class has the following members:
Attribute |
Description |
Isenabled |
Indicates whether to activate the tracking of the current request. |
Tracemode |
Set the trail mode: sortbycategory or sortbytime. |
Method |
Description |
Warn |
The trace information is displayed in red. |
Write |
Write tracking information. |
To program to disable tracing, you can use the following statement in the load event of webform1:
In our example, the trace information is not explicitly displayed, so it is buried by other trace information. The warn () method of the trace class can print the trace information in red. Therefore, the Code is not written as follows:
Trace. Write ("Page loaded ") |
But:
Trace. Warn ("Page loaded ") |
Figure 5 shows the debugging information displayed in red in the warn () method.
Figure 5. Use the warn () method to display trace information in red |
Sort tracking information
Putting multiple tracing statements in one application may sometimes look messy. If your tracking information can be divided into different categories, it is easier to trace. The trace class allows us to classify and sort trace information based on categories.
The following example demonstrates how to group tracing 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 on the initialization 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 subPrivate sub listbox#selectedindexchanged (byval sender _ As system. Object, byval e _ System. eventargs) handles _ Listbox1.selectedindexchanged Trace. Warn ("ListBox", "ListBox PostBack ") End sub |
When this example is executed, the following debugging information is displayed, Which is grouped by category (figure 6 ):
Figure 6. sort by category |
Let's analyze the above Code:
Trace. tracemode = tracemode. sortbycategory |
The tracemode attribute sets the modes supported by the trail:
· Sortbycategory: sorts trace information by type.
· Sortbytime: displays the trace information based on the execution sequence.
Because we chose the sort by category mode, Figure 7 shows that the information is 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 this example, we pass two parameters to it. The first input category and the second parameter is for obtaining the message.
In addition to using the trace class to set the trace mode, you can also use the page command to specify the trace mode:
<% @ Page Language = "VB" trace = "true" tracemode = "sortbycategory" autoeventwireup = "false" codebehind = "webform1.aspx. VB" inherits = "webapplication1.webform1" %> |
Application Tracking
The last part discusses page tracing, which tracks the execution information flow on the 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:
Attribute |
Value |
Description |
Enabled |
True |
Activate or disable application-level tracing. |
Requestlimit |
10 |
Set the maximum number of requests to be tracked. |
Pageoutput |
False |
The tracing information is displayed at the end of the page. |
Tracemode |
Sortbytime |
Sorting method of tracing information. |
Localonly |
True |
Allows you to view the tracking browser on a non-local computer. |
When an application is loaded, the tracing information is not displayed on the page. To view tracing information, we need to use the tracing Viewer (trace. axd ):
Figure 7. application-level Tracing |
Figure 7 shows the tracing information for the last six requests of the application. To view the details of each request, click the "View Details" link in each row.
Note: If the trace is set to true in the web. config file and it is set to false in the page command, tracing will be disabled.
Summary
ASP. NET makes debugging Web applications much easier. Now you know how to use tracing. Try it and see how much it improves your efficiency!