Debug the method and skills of the ASP.NET application

xiaoxiao2021-03-05  23

Personnel who used ASP to develop a web application must know how troublesome to debug web applications. In ASP, debugging is painful, usually includes the value of the variable using the response.write () method. So, how many times have you forgotten to delete a debug statement before a deployment application? This happens with the occurrence of the .NET framework assembly. In .NET, you can use the debugger in Visual Studio .NET to track the execution of the entire web application, or use the TRACE class in the System.Web.TraceContext name space. This article demonstrates how to use Trace classes to assist your debugging work. Using the Trace class ASP.NET contains a Trace class that helps track the application information flow. Instead of debugging using the Response object, you can now print out debug information using the Trace class. To demonstrate its use, we first build an ASP.NET web application and place a Button and a ListBox control on the default WebForm1 (shown in Figure 1). Fill the ListBox control with three items and set it to true with its AutoPostBack properties.

Figure 1. Fill the default WebForm1

For this article, I hope to track the execution of the application. First, activate the tracking, the page instruction requires the TRACE attribute, which is set to true (switches to the View HTML source mode), as shown in Figure 2.

Figure 2. Activation tracking

Next, I insert the Trace statement in the Form's Load event so I know if postback has occurred. Postback event is the easiest confusion in ASP.NET, which often leads to the failure of the initiator of the initial ASP.NET.

Private Sub Page_Load (Byval Sender As System.Object, _byval e as system.eventargs _handles mybase.load 'Place the initialization page for the initialization page TRACE.WRITE ("Page Loaded") if not ispostback the TRACE.WRITE ("not In a postback ") 'Postback takes place to perform some operations Else Trace.Write (" in a postback ")' Perform some operation END IFEND SUB

I also want to know if postback occurs when the ListBox data item is selected:

Private sub placebox1_selectedIndexchanged (Byval Sender as _system.Object, _byval e as system.eventargs) Handles _ listbox1.selected IndexChanged Trace.write ("ListBox Postback") End Sub

When the above ASP.NET application is executed, the output result is displayed (shown in Figure 3):

Figure 3. Display tracking information

You can find that when you load WebForm1 for the first time, you can see the string "Page Loaded" and "Not in A Postback". If you click on the button on WebForm1, you can see the record shown in Figure 4. Similarly, if you click ListBox, a "ListBox Postback" string is also displayed.

Figure 4. Check the tracking information

The tracking page contains the following section (all information is displayed in Figure 3):

Segment Description Request Details Describe information related to requests, such as dialog ID, encoding, and request time. The tracking information contains details of the currently running application. The tracking information is displayed in this section. The control tree displays the information of the control in a page and the size of the ViewState hides the field. Cookie set the cookie set to display the page and its value. The header shows HTTP header information, such as content length and user agent. Form collection displays the name of the control in a page and their value. Server variables Display the environment variable of the server side. Note that our tracking information is displayed below the "Tracking Information Segment". If you want to close the track, you only need to simply set the properties of the TRACE in the page instruction to false. There is no need to delete tracking instructions in the application, and now close debugging is just as simple as setting up a Boolean value. Turning on / off track is simply modifying the value of the TRACE attribute in the page instruction. You can also use Trace class to turn off track. The member of the Trace class is as follows:

Property Description ISENABLED indicates whether tracking of the current request is activated. TraceMode Set Tracking Mode: Sortbycategory or SortbyTime.

Method Description WARN Displays the tracking information in red. Write write tracking information.

To program close track, you can use the following statement in WebForm1's Load event:

TRACE.ISenabled = FALSE

In our example, the tracking information is not displayed, so it is buried by other tracking information. The WARN () method of the TRACE class can print the tracking information to red. So don't write code like this:

Trace.write ("Page Loaded")

Instead:

Trace.warn ("Page Loaded")

Figure 5 shows the debugging information of the WARN () method with red display.

Figure 5. Use a WARN () method with red display tracking information

Sorting tracking information Sort multiple tracking statements in an application sometimes make messy. If your tracking information can be divided into different categories, then tracking is easier. The Trace class allows us to classify and sort the tracking information based on the species. The following example demonstrates how to group the tracking information by category:

Private Sub Page_Load (ByVal sender As System.Object, _ByVal e As System.EventArgs) _Handles MyBase.Load Trace.TraceMode = TraceMode.SortByCategory 'placed here Trace.Warn user code to initialize the page ( "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 IfEnd SubPrivate Sub ListBox1_SelectedIndexChanged (ByVal sender _As System. Object, byval e as _system.eventargs) Handles _ listbox1.selected "End Sub"

When performing this example, the following debugging information will be displayed, which is packet according to the category (shown in Figure 6):

Figure 6. Sort by category

Let us analyze the above code:

Trace.tracemode = tracemode.sortbycategory

The TraceMode property sets the pattern of tracking support: • Sortbycategory: Sort the tracking information based on the type. • SortbyTime: Displays the tracking information based on the execution order. Because we chose the sort mode according to the category, Figure 7 shows that the information is sorted by category. Trace.Warn ("Page_Load", "Page Loaded")

The WARN property is displayed in red, pay attention to this is an overload method. In the example, we pass two parameters. The first input category (category), the second parameter is to obtain a message (Message). In addition to using the Trace class setting tracking mode, you can also use the page instruction to specify the tracking mode:

<% @ Page language = "vb" trace = "true" tracemode = "sortbycategory" autoeventwireup = "false" codebehind = "Webform1.aspx.vb" inherits = "WebApplication1.webform1"%>%>%>

The last part of the application tracks discusses the page tracking, and it tracks the execution information flow within the page. ASP.NET also supports application-level tracking, the application level is set in the web.config file, at the TRACE section:

In order to activate the application level tracking, set the following value:

Property Value Description EnableDtrue Activates or disables application-level tracking. RequestLimit10 sets the maximum number of requests for tracking. Pageoutput False Displays the tracking information at the end of the page. TraceMode SortbyTime Tracking Information Sort by. Localonly True is set to view the ability to track your browser on non-local computers.

When the application is loaded, the tracking information is not displayed on the page. To view tracking information, we need to use Track Viewer (Trace.Axd):

Figure 7. Application level tracking

Figure 7 shows the tracking information of the last six requests for the application. If you want to see the details of each request, click the "View Details" link in each row. Note that if Trace is set to True in the web.config file, and it is set to false in the page instruction, and tracking will be disabled. Summary ASP.NET makes it easy to debug web applications. Now you already know how to use tracking, try it, look at it how much it is!

转载请注明原文地址:https://www.9cbs.com/read-35444.html

New Post(0)