Asp.net| detailed "Digest" ASP. NET has a much better tracking function than ASP, and it helps to determine the source of errors in Web applications by tracking information. In this paper, the tracking mode of ASP.net is explained in detail through examples.
The most common debugging method for traditional ASP is to use Response.Write to set breakpoints and to display the values of certain variables on a Web page. However, there are many problems with this approach.
(1) in the program is full of Response.Write statements, these statements may affect debugging, but also affect the layout of the page.
(2) After the commissioning is over, but also painful to delete these statements. And in the removal of redundant Response.Write should be careful to prevent the wrong deletion of the necessary Response.Write statements. If the program has hundreds of lines, the workload can be imagined.
Asp. NET introduces new features that allow you to write debug statements directly in your code, which eliminates the need to remove applications from your application when you deploy them to production servers. This feature, called tracing, allows you to write variables or structures in a page, assert whether a condition is met, or simply trace through the page or application's execution path. To collect and display these messages and other trace information, you must enable tracing for the page or application. When tracing is enabled, two things will happen:
(1) asp.net a series of diagnostic information tables appended to the page output immediately thereafter. The information is also sent to the trace viewer application (only when tracing is enabled for the application).
(2) asp.net displays a custom free clinic break message in the trace information table of the appended performance data. The specified diagnostics and trace messages are appended to the page output sent to the requesting browser. Alternatively, you can view the information in a separate trace viewer (Trace.axd), which displays the trace information for each page in a given application. When ASP.net processing page requests, this information can help you identify errors or do not want to get results.
Trace statements are processed and displayed only after tracing is enabled. You can control whether a trace is displayed on a page, displayed to a trace viewer, or displayed both on the page and in the trace viewer.
2, ASP. NET's tracking mode configuration
In order to be able to use the tracing feature, it is necessary to enable it within the scope of the page or the entire application.
2.1 Page-level configuration
To enable tracing at the page level, you set the trace attribute in the @page directive. As shown below:
Figure 1: Trace Information
If trace has a property value of true, trace information is displayed at the bottom of the page when the page is displayed. Using the TraceMode property allows you to adjust the order in which the information is displayed. TraceMode optional values are: in chronological order (SortByTime) and by category (Sortcategory), where the default value is in chronological order.
2.2 Application-level configuration
There are several options for application-level tracing, which can be set by adding an XML element under <system.web> in the Config.web file <trace>.
Table 1: Tracking options
Property Description enabled If the trace function is true in the application, it is falsepageoutput if trace information appears on the application's page and in the trace window, false. Note: This property will not affect the start of the Trace feature page requestlimit describes the maximum number of trace requests that the server can hold. The default is 10 TraceMode named to display trace information in some order. by SortByTime (chronological) or by user-defined sortbycategory (alphabetic order). defaults to chronological order. Localonly If True, the trace window (trace.axd) can only be displayed on the Web server's host, or false. The default is true.
*******************************************************************************
<configuration>
<system.web>
<trace enabled= "true" pageoutput= "false" requestlimit= "Tracemode=" SortByTime "true"/>
...
</system.web>
</configuration>
*******************************************************************************
Although all properties are used in the example, not every property must be set. and the page-level configuration overrides the application-level configuration. For example, when tracing is disabled at the application level, but it is enabled at the page level, the trace information is still displayed on the page.
The Requstlimit property sets the number of requests that will be recorded in the trace log, which prevents the log from being too large. If the Localonly property is set to True, trace information is only seen on the server. This allows only the server side to track the application while other users do not see trace information.
3, ASP. NET's Trace mode output
The trace output is generated by the TraceContext object and is displayed in several sections at the bottom of the ASP.net page, as shown in Figure 1. Here are a few sections and descriptions of them. Because of the many tracking information, it is not possible to display it in a single window, so each type of information is displayed by a separate Output window.
3.1 Requests for more information
This section contains six pieces of information, as shown in the following table:
Table 2: Request Information
The information item describes the unique identifier of the session ID conversation on the server the time requested by request encoding the encoding of the request, such as Unicoderequest Typeget or post requests the status code of the status request response the encoding of the encoding response, such as Unicode
Figure 2: Detailed information for the request
3.2 Tracking Information
This type of information contains trace information or warnings from the application or ASP.net engine output during the tracing process. By default, ASP. NET engine will output information at the start and end of each practice, such as PreRender, SaveViewState. The output information includes: Category (category), message (messages), Form first (the time interval at which the page starts to run), and the time interval of the trace information item that was last printed. The display order of these information items is determined by the TraceMode property of the @page or the TraceMode property value of the TraceContext object.
The Tracking Information section is important and I'll elaborate on it in the next section.
3.3 Control Tree
The control tree information displays all the elements in the ASP.net page in a tree structure, enabling programmers to sort out the dependencies between controls, helping to determine scope and ownership. Each element displays information such as control Id (identifier), type (type), Render size Bytes (size), and number of viewstate bytes.
Figure 3: The control tree
3.4 Cookies Set
The cookies set information lists all the cookies associated with the ASP.net Web application. The information displayed includes: Name, value (value), and size.
3.5-Header Message Set
The header message set contains all the HTTP header messages that are uploaded to the ASP.net Web page, and each item will display the name (first name) and value (values).
3.6 Form Message Sets
Such information is displayed only if a form is included in the ASP.net Web page and the form has been submitted to the server. It contains two very important information.
(1) The ViewState of the page, which is the status summary for all controls in the form.
(2) the name (name) and value (values) of all controls.
4. Write Trace message
Asp. NET includes a Trace object (similar to a response, request, or context object) that allows you to write debug statements that occur when you enable tracing for a page or an entire application.
Asp. NET uses the TraceContext class to store information about the request, its control hierarchy, and trace information. Trace information includes certain lifecycle stages of a page request and any custom statements that are included in the selection. You can use the TraceContext class by using the Page.trace property or the Control.context property. 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 when you want to include trace statements outside of a page, such as a Global.asax file.
The interface for the TraceContext class is simple, with only one constructor, two properties, and two methods. There are, of course, properties and methods inherited from the object class. The trace attribute in the Page object is an instance of a TraceContext class. The TraceContext class provides two methods: Write and warn, which allow statements to be written to trace records. Each method is overloaded and allows you to specify trace categories, text messages, and optional error messages. The only difference between the two methods is that the Warn method displays its text in red.
Note When you specify a category in a call to a write or Warn method, you can use that category to sort the trace statements.
Each method is overloaded, and there are three versions of each method. If you include only one string parameter when calling warn or write, asp.net treats this as a message. If you include two parameters, treat the first as a category, and you can programmatically use this parameter to sort the messages that are displayed in the Trace information table when tracing is enabled. The third parameter is the exception type, containing the error message for the request.
4.1 Write custom trace messages to the trace in the page
(1) In the page code declaration block or code-behind class, use the Trace property to invoke one of the TraceContext methods;
(2) Specify an optional category parameter for the trace statement. You can use this category to sort the trace statements that are displayed;
(3) Specify the message parameter for the trace statement. This can be a string or a method;
(4) Specify an optional errorinfo parameter that contains information about any errors in the page;
The following TraceContext.Warn Method example defines the category as render, and the trace message is "Professor Zhang Zhiyuan using warn tracking."
[C #]
Trace.Warn ("Render", "Professor Zhang Zhiyuan is using Warn tracking.) ");
[Visual Basic]
Trace.Warn ("Render", "Professor Zhang Zhiyuan is using Warn tracking.) ")
The following screenshot illustrates the custom trace statements that are rendered to the trace information table when tracing is enabled for pages. The Warn method is used to generate the first message displayed in red text with a category of Render, with the message "Professor Zhang Zhiyuan using warn tracking." The Write method is used to generate a second custom message with the same category, but the message is "Professor Zhang Zhiyuan is using write tracing."
Figure 4: Custom Trace Records
4.2 Writing a custom trace message to a trace in a custom server control
(1) In the server control code, use the context property to invoke one of the TraceContext methods;
(2) Specify an optional category parameter for the trace statement. You can use this category to sort the trace statements that are displayed;
(3) Specify the message parameter for the trace statement;
(4) Specify an optional errorinfo parameter that contains information about any errors in the page;
The following example uses the Warn method to write a custom statement to a trace record of a server control. The category is Zzy class, and the message is "Professor Zhang Zhiyuan on track."
[C #]
Context.Trace.Write ("ZZY Class", "Professor Zhang Zhiyuan in trace.") ");
[Visual Basic]
Context.Trace.Write ("ZZY Class", "Professor Zhang Zhiyuan in trace.") ")
4.3 Trace message Write record only when tracing is enabled
In some cases, the statement needs to be passed to a write or Warn method only if tracing is enabled. The TraceContext object has a Boolean attribute (IsEnabled) that allows you to invoke these methods conditionally.
Creates an if statement that checks whether tracing is enabled for the page or application to which the code belongs, and then creates a conditional statement that executes when the Trace.IsEnabled property returns True.
The following example confirms that tracing is enabled for a page, and then uses the Write method to write information about it from the database to the trace information table.
*******************************************************************************
Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
If trace.isenabled Then
Trace.Write ("Prod", "Professor Zhang Zhiyuan in trace")
End If
End Sub
*******************************************************************************
5. Summary
Through the above introduction, has been able to obtain tracking information, then how to use them? This depends on the specific circumstances. Most of the tracking information (such as cookies, headers, and server variables) is also available in traditional ASP. But they're not like asp.net in the trace viewer.
The most powerful tracking feature of ASP.net is the tracking information (message) in the Trace Information section of the trace output. With this part of the information, you can see when the ASP.net page starts and how long it takes to run the page. This information is important for discovering performance bottlenecks in the application. At the same time, this information can also help solve some of the code does not work correctly, and this is because the code is not in the desired order of execution, or repeated execution. This error is often difficult to discover in traditional ASP, and with this part of the trace information, these errors become obvious.
Proper use of application-level tracing will significantly reduce the time and effort spent debugging Web applications. If you can enable tracing in your program and set the <trace> element's Pageoutput property to False in Web.config, you can then allow some users to use the application. In this way, in the user's use process, the vast number of program enthusiasts can get tracking information (client users do not see this information), which helps to determine the root cause of the error.
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.