Methods and techniques for debugging asp.net applications

Source: Internet
Author: User
Tags config execution header sort
Asp.net| Program | Tips people who used to use ASP to develop Web applications must know how troublesome it is to debug Web applications. In ASP, debugging is painful and typically includes values that use the Response.Write () method to output variables. So ask yourself: how many times have you forgotten to delete a debug statement before the application is in the program?

With. NET Framework components have changed radically. In. NET, you can use visual Studio. NET to track the execution of the entire WEB application, or to use the Trace class in the System.Web.TraceContext namespace. This article demonstrates how to use the Trace class to assist with your debugging efforts.

Using the Trace class

Asp. NET contains a trace class that helps track the flow of information in your application. Instead of debugging with a response object, you can now use the Trace class to print out debugging information.

To demonstrate its use, we first build a asp.net Web application and place a button and a ListBox control on the default WebForm1 (as shown in Figure 1). Populates the ListBox control with three items and sets its AutoPostBack property to True.


Figure 1. Populating the default WebForm1
For this article, I want to track the execution flow of the application. First, the trace is activated, and the page directive requires that the trace attribute be set to true (switch to view HTML source mode), as shown in Figure 2.


Figure 2. Activate tracing
Next, I insert the trace statement in the form's Load event so that I know if postback has occurred. The postback event is one of the most confusing features in ASP.net, and it often leads to the failure of the first ASP.net developers.

Private Sub Page_Load (ByVal sender as System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
' Place the user code for the initialization page here
Trace.Write ("Page Loaded")
If not IsPostBack Then
Trace.Write ("Not in a postback")
' Perform some action when the postback occurs
Else
Trace.Write ("In a postback")
' Perform some action
End If
End Sub
I also want to know whether the postback occurred when the listbox was selected:

Private Sub listbox1_selectedindexchanged (ByVal sender as _
System.Object, _
ByVal e As System.EventArgs) Handles _
Listbox1.selectedindexchanged
Trace.Write ("Listbox postback")
End Sub
When the above ASP.net application executes, the following output results are displayed (Figure 3):


Figure 3. Show Trace Information
You can see that the string "Page loaded" and "not in a postback" are visible when you first load the WebForm1. If you click on the WebForm1 button, you can see the record shown in Figure 4. Similarly, if you click on a ListBox, a "ListBox postback" string is also displayed.


Figure 4. Check trace information
The trace page contains the following sections (no information is shown in Figure 3):

The paragraph describes the request details describing the request-related information, such as the dialog ID, encoding, and time of the request. The trace information contains the details of the currently running application. The trace information is displayed in this section. The control tree displays the information about the controls on a page and the size of the ViewState hidden fields. The cookie collection displays the cookie for the page and its value settings. The Header collection displays HTTP header information, such as the content length and user agent. The form collection displays the names of controls on a page and their values. Server variables display server-side environment variables.
Note that our tracking information is displayed below the trace Information section. If you want to turn off tracing, simply set the properties of trace in the page directive to false. There is no need to remove trace instructions from the application, and now turning off debugging is simply setting a Boolean value.

Turning on/off tracing simply modifies the value of the trace attribute in the page directive. You can also use the Trace class program to turn off tracing. The members of the Trace class are as follows:

The attribute description isenabled indicates whether the trace for the current request is activated. TraceMode Set trace mode: SortByCategory or SortByTime.
Method description warn the trace information to red. Write writes trace information.
To programmatically turn off tracing, you can use the following statement in the WEBFORM1 Load event:

Trace.IsEnabled = False
In our case, the tracking information is not shown in a very significant purpose, so it is buried by other tracking information. The Warn () method of the Trace class can print trace information to red. So it's not writing code like this:

Trace.Write ("Page Loaded")
But:

Trace.Warn ("Page Loaded")
Figure 5 shows the debugging information shown in red by the Warn () method.


Figure 5. Use the Warn () method to display trace information in red
Sort the trace information

Putting multiple trace statements in one application can sometimes seem cluttered. If your tracking information can be grouped into different categories, then tracking is easier. The Trace class allows us to categorize and sort trace information based on species.

The following example shows how to group trace 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 for the initialization page here
Trace.Warn ("Page_Load", "Page Loaded")
If not IsPostBack Then
' Perform some action
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 you perform this example, the following debugging information is displayed, grouped by category (Figure 6):


Figure 6. Sort by Category
Let's analyze the above code:

Trace.tracemode = Tracemode.sortbycategory
The TraceMode property sets the pattern for tracking support:

· SortByCategory: Sorts trace information according to type.

· SortByTime: The tracking information is displayed according to the order of execution.

Because we chose to sort by category, Figure 7 shows that the information is sorted by category.

Trace.Warn ("Page_Load", "Page Loaded")
The Warn property displays the message as red, noting that this is an overloaded method. In the example, we passed two parameters to it. The first input category (Category), and the second parameter is for obtaining a message.

In addition to using the Trace class to set trace mode, you can also use page directives to specify the tracking mode:

<%@ Page language= "vb" Trace= "true" tracemode= "SortByCategory" autoeventwireup= "false" codebehind= "WebForm1.aspx.vb" inherits= "Webapplication1.webform1"%>


Application Tracking

The last section discusses page tracking, which tracks the flow of information within a page. Asp. NET supports application-level tracing, and application-level tracing is set in the Web.config file, under the trace section:

<trace enabled= "false" requestlimit= "ten" pageoutput= "false" tracemode= "SortByTime" localonly= "true"/>
To activate application-level tracing, set the following value:

Attribute value Description Enabledtrue activates or disables application-level tracing. RequestLimit10 sets the maximum number of requests that are tracked. Pageoutput false to display trace information at the end of the page. TRACEMODE SortByTime tracking information sorting method. Localonly True Sets the ability to view tracking browsers on a non-local computer.
After the application is loaded, the tracking information is not displayed on the page. To view the tracking information, we need to use the Trace viewer (Trace.axd):


Figure 7. Application-Level tracing
Figure 7 shows the tracking information for the last six requests of the application. To view the details of each request, click the "View Details" link for each row.

Note that if trace is set to true in the Web.config file and it is set to false in the page directive, tracing will be disabled.

Summarize

Asp. NET makes it much easier to debug Web applications. Now that you know how to use tracing, try it and see how much it has improved your efficiency!


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.