ASP. NET usage tracking

Source: Internet
Author: User

Asp.net introduces a new feature that allows you to write debugging statements directly in your code. This removes the need to deploy your application on a production server. 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 in a given application. When Asp.net processes a page request, this information can help 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 pages and to a trace viewer.

2. Configure Asp.net's tracking mode

To be able to use the tracking function, you must enable it on the page or throughout the application.

2.1 page-level Configuration

To enable the tracing function at the page level, configure 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 document.

Table 1: trace options

Attribute Description
Enabled If the tracing function is true in the application, otherwise, false
Pageoutput If the trail information is displayed on the application page and in the trail window, it is true; otherwise, it is 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 any attribute is used in this example, not all attributes must be configured. 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 is configured with 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 a separate output window.

3.1 detailed request information

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: detailed request information

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) connection and form last (the 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.

Tracking information is very important and I will elaborate on it in the next section.

3.3 Control tree

The Control tree information uses a tree structure to display any elements on the Asp.net page. This allows programmers to clarify the subordination between controls and help them determine the scope and permissions. Each element displays the following information: Control ID (identifier), type (type), render size bytes (size), and number of viewstate bytes.


Figure 3: Control tree

3.4 cookies

The cookie set information lists any cookies related to this Asp.net web application. The displayed information includes: name, value, and size ).

3.5 header Message Set

The header message set contains any HTTP header messages sent to the Asp.net webpage. Each item will display name and value ).

3.6 form Message Set

This type of information is displayed only when the Asp.net webpage contains a form that has been submitted to the server. It contains two very important information.

(1) The viewstate of the webpage, that is, the State summary of any controls in the form.

(2) Name and value of any control ).

4. Write tracing messages

Asp.net includes a trace object (similar to a response, request, or context object), which allows you to write debugging statements that appear when a trace of a page or application is enabled.

Asp.net uses the tracecontext class to store request information, its control hierarchy, and tracking information. The trace information includes some lifecycle stages of the page request, along with 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 the Asp.net page. The latter is available when you want to include trace statements in custom server controls or outside pages (such as the Global. asax document.

The tracecontext class interface is very simple. There is only one constructor, two attributes, and two methods. Of course, there are more 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 the call to the write or warn method, you can use this category to sort the tracing statements.

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, this parameter can be used 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. This category can be used 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. This category can be used 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 time when the Asp.net page starts to run, along with the time it takes to run the page. This information is 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 repeated execution. This kind of error is often difficult to find in the traditional ASP, and with this part of tracking information, these errors become obvious.

The proper use of the application-level tracing function will greatly reduce the time and effort it takes to debug web applications. If you can enable the tracing function in your program and set the pageoutput attribute of the <trace> element to false in Web. config, you can use the application. In this way, the majority of programmers can get tracking information (the client users cannot see this information), which is conducive to determining the root cause of the error.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.