【Abstract】 the tracking function of ASP. NET is much better than that of ASP. It is helpful to judge Web applications through tracking information. Program The root cause of the error. This article explains in detail the tracking mode of ASP. NET through examples.
[Keyword] ASP. NET; trace information; trace
1. Introduction
The most common debugging method of ASP is to use response. Write to set breakpoints and display the current values of some variables on the web page. However, this method has many problems.
(1) The program contains response. Write statements, which may affect debugging and page layout.
(2) After the debugging is complete, delete these statements with pain points. When deleting redundant response. Write statements, you must be careful to prevent the necessary response. Write statements from being deleted by mistake. If there are hundreds of lines in the program, you can imagine the heavy workload.
ASP. NET allows you to directlyCodeTo deploy applications to production servers without removing them from the application. This function is called tracking. It allows you to write variables or structures on the page, determine whether the assertions meet certain conditions, or simply trace through the execution path of the page or application. To collect and display these messages and other tracing information, you must enable page or application tracing. When tracing is enabled, two things will happen:
(1) ASP. NET adds a series of diagnostic information tables to the output page. This information is also sent to the tracing Viewer application (only when tracing for the application is enabled ).
(2) ASP. NET displays custom diagnostic messages in the trace information table of append performance data. The specified diagnostic information and tracing message are appended to the page output sent to the requesting browser. Alternatively, you can view this information in a separate trace. axd, which displays the trace information for each page of a given application. When ASP. NET processes page requests, this information helps identify errors or unwanted results.
The trace statement is processed and displayed only after the trail is enabled. You can control whether to display a trail to a page, to a trace viewer, or to both a page and a trace viewer.
2. ASP. NET tracking mode configuration
To be able to use the tracing function, you must enable it within the page or the entire application.
2.1 page-level Configuration
To enable the tracing function at the page level, you must set the trace attribute in the @ page command. As follows:
**************************************** *************************************** <% @ Page Language = "VB" trace = "true" codebehind = "webform1.aspx. VB" inherits = "webapplication1.webform1" %> **************************************** *************************************** |
Figure 1: Tracking Information |
If the trace property value is true, the trace information is displayed at the bottom of the page when the page is displayed. The tracemode attribute can be used to adjust the display order of the information. The optional tracemode values are sortbytime and sortcategory. The default value is time.
2.2 application-level Configuration
The application-level tracing function has multiple options. You can add an XML Element <trace> to <system. Web> In the config. Web file.
Table 1: trace options
Attribute |
Description |
Enabled |
If the tracking function is true in the application, otherwise false |
Pageoutput |
True if the trace information is displayed on the application page and in the trace window; otherwise, false. Note: This attribute does not affect the enabled tracking feature page. |
Requestlimit |
The maximum number of trace requests that the server can store. 10 by default |
Tracemode |
Displays the trace information in a certain order. Sort by sortbytime or by user-defined sortbycategory (in alphabetical order ). The default value is chronological. |
Localonly |
If it is true, the trace. axd window can only be displayed on the web server host; otherwise, it is false. The default value is true. |
**************************************** *************************************** <Configuration> <System. Web> <Trace enabled = "true" pageoutput = "false" requestlimit = "20" tracemode = "sortbytime" localonly = "true"/> ... </System. Web> </Configuration> **************************************** *************************************** |
Although all attributes are used in this example, not all attributes must be set. Page-level Configuration overwrites application-level configuration. For example, if the tracing function is disabled at the application level, but this function is enabled at the page level, the tracing information will still be displayed on the page.
The requstlimit attribute sets the number of requests to be recorded in the trace log, which can avoid excessive logs. If the localonly attribute is set to true, only the trace information is displayed on the server. This allows only the server to track applications, but other users cannot see the tracing information.
3. ASP. NET trace mode output
The trace output is generated by the tracecontext object and is displayed at the bottom of the ASP. NET page in several parts, as shown in 1. The following is a description of these parts. Because there are many trace information, it is impossible to display it in a window. Therefore, each type of information is displayed in an independent output window.
3.1 request details
This section contains six items, as shown in the following table:
Table 2: Request Information
Information item |
Description |
Session ID |
Unique Identifier of the session on the server |
Time of request |
Request generation time |
Request Encoding |
The request encoding method, such as Unicode. |
Request type |
Get or POST requests |
Status |
Request status code |
Response Encoding |
The encoding method of the response, such as Unicode. |
Figure 2: request details |
3.2 tracking information
This type of information contains the tracing information or warning output by the application or ASP. NET engine during the tracing process. By default, the ASP. net engine outputs Information at the beginning and end of each practice, such as prerender and saveviewstate. The output information includes: category, message, and form first) and form last (the time interval between the last output trace information items ). The display order of these information items is determined by the tracemode attribute of @ page or the tracemode attribute value of the tracecontext object.
The tracking information is very important and I will elaborate in the next section.
3.3 Control tree
The Control tree Information Displays all elements on the ASP. NET page in a tree structure. This helps programmers to clarify the subordination between controls and determine the scope and ownership. Each element displays the following information: Control ID (identifier), type (type), render size bytes (size), and number of viewstate bytes.
3.4 cookies
The cookie set information lists all cookies related to the ASP. NET web application. The displayed information includes: name, value, and size ).
3.5 header Message Set
The header message set contains all HTTP header messages sent to ASP. NET webpages. Each item displays name and value ).
3.6 form Message Set
This type of information is displayed only when an ASP. NET webpage contains a form that has been submitted to the server. It contains two important information.
(1) The viewstate of the webpage, that is, the State summary of all controls in the form.
(2) Name and value of all controls ).
4. Write tracing messages
ASP. NET includes a trace object (similar to a response, request, or context object). This object allows you to write debugging statements that appear when a page or entire application is enabled for tracing.
ASP. NET uses the tracecontext class to store request information, its control hierarchy, and tracking information. The trace information includes some lifecycle stages of page requests and any custom statements included in the selection. The tracecontext class can be used through the page. Trace attribute or the control. Context attribute. The former is available when developing ASP. NET pages. The latter is available when you want to include trace statements in custom server controls or in addition to pages (such as the Global. asax file.
The tracecontext class interface is very simple. There is only one constructor, two attributes, and two methods. Of course, there are also some attributes and methods inherited from the object class. The trace attribute in the page object is an instance of the tracecontext class. The tracecontext class provides two methods: Write and warn, which allow the statement to be written to the tracking record. Each method is overloaded and allows you to specify the trace type, text message, and optional error information. The only difference between the two methods is that the warn method displays its text in red.
[Note] When you specify a category in calling the write or warn method, you can use this category to sort the tracing statement.
Every method will be reloaded, and each method has three versions. If only one string parameter is included when calling warn or write, ASP. NET treats this as a message. If two parameters are included, the first parameter is treated as a category. During programming, you can use this parameter to sort the messages displayed in the trace information table when tracing is enabled. The third parameter is of the exception type and contains the error message of this request.
4.1 write custom tracking messages to tracking records on the page
(1) In the code declaration block or code hiding class on the page, use the trace property to call one of the tracecontext methods;
(2) Specify the optional Category parameter for the trace statement. You can use this category to sort the displayed trace statements;
(3) Specify the message parameter for the trace statement. This can be a string or method;
(4) Specify the optional errorinfo parameter, which contains any error information on the page;
The following tracecontext. Warn method example defines the category as render, And the tracing message is "Zhang Zhiyuan is using warn tracking ".
[C #] Trace. Warn ("render", "Zhang Zhiyuan is using warn tracking. "); [Visual Basic] Trace. Warn ("render", "Zhang Zhiyuan is using warn tracking. ") |
The following screen illustrates the custom trace statements displayed on the trace information table when page tracing is enabled. The warn method is used to generate the first message displayed in red text. Its category is render, and the message is "Zhang Zhiyuan is using warn tracking ". The write method is used to generate the second custom message with the same category, but the message is "Zhang Zhiyuan is using Write tracking ".
Figure 4: Custom tracking records |
4.2 write custom tracking messages to tracking records in the Custom Server Control
(1) In the server control code, use the context attribute to call one of the tracecontext methods;
(2) Specify the optional Category parameter for the trace statement. You can use this category to sort the displayed trace statements;
(3) Specify the message parameter for the trace statement;
(4) Specify the optional errorinfo parameter, which contains any error information on the page;
The following example uses the warn method to write custom statements to the trace records of the server control. The category is tracing class, and the message is "Zhang Zhiyuan is tracking ".
[C #] Context. Trace. Write ("Keep class", "Zhang Zhiyuan is tracking. "); [Visual Basic] Context. Trace. Write ("Keep class", "Zhang Zhiyuan is tracking. ") |
4.3 Only Tracking message write records when tracing is enabled
In some cases, the statement must be passed to the write or warn method only when tracing is enabled. The tracecontext object has a Boolean attribute (isenabled) that allows conditional calls to these methods.
Create an if statement to check whether tracing is enabled for the page or application to which the Code belongs. then create a condition statement that is executed when the trace. isenabled attribute returns true.
The following example confirms the Enable page tracing and then writes the relevant information from the database to the trace information table using the write method.
**************************************** *************************************** Private sub page_load (byval sender as system. Object, byval e as system. eventargs) handles mybase. Load If trace. isenabled then Trace. Write ("Prod", "Zhang Zhiyuan tracking ") End if End sub **************************************** *************************************** |
5. Summary
Through the above introduction, we can get the tracking information. How can we use them? This depends on the specific situation. Most tracking information (such as cookies, header information, and server variables) is also available in traditional ASP. But they are not included in the tracking viewer like ASP. NET.
The most powerful tracking function of ASP. NET is the tracking information (here refers to the information (Message) of the tracking output )). With this information, you can understand the start time of the ASP. NET page and the time it takes to run the page. This information is very important for discovering performance bottlenecks in applications. At the same time, this information also helps solve the problem that some code cannot run normally, and this is because the code is not executed in the desired sequence or is executed repeatedly. This kind of error is often difficult to find in the traditional ASP, and with this part of tracking information, these errors become obvious.
using the application-level tracking function appropriately will greatly reduce the time and effort spent debugging web applications. For example, you can enable the tracking function in the program and set the pageoutput attribute of the element to false in Web. config. Then, some users can use the application. In this way, the majority of program enthusiasts can get tracking information (client users cannot see this information), which is conducive to determining the root cause of the error.