ASP.NET Tracing

   


Implement tracing: Display trace output

Log test results: Control debugging in the Web.config file.

In addition to the Trace and Debug classes, ASP.NET supports one more method for tracing, which is specially designed for Web applications and Web services. This method of tracing is called ASP.NET tracing.

With the help of ASP.NET tracing, you can view trace messages and diagnostics information of a Web request along with the page output or through a separate trace viewer utility ( trace.axd ). You can write custom trace messages by using the System.Web.TraceContext class. The TraceContext class is responsible for gathering the execution details of a Web request. You can access the TraceContext object for the current request through the Trace property of the Page class. Once you have the TraceContext object, you can invoke its member methods to write trace messages to the trace log. Table 9.5 lists some of the important members of the TraceContext class that you should be familiar with.

Table 9.5. Important Members of the TraceContext Class

Member

Type

Description

IsEnabled

Property

Specifies whether tracing is enabled for a request.

TraceMode

Property

Indicates the sort order in which the messages should be displayed. It can have one of three values Default , SortByCategory , and SortByTime .

Warn

Method

Writes messages to the trace log in red to indicate that they are warnings. It has three overloadsone with message, one with category and message, and the last one with category, message, and exception object.

Write

Method

Writes the messages to the trace log. It has the same three overloads as the Warn method.

NOTE

Tracing Beyond a Page The Page class exposes a Trace property that gives you access to the TraceContext object. If you want to write messages from outside the page (for example, from the global.asax file or from custom server controls) you can access the Trace property of the HttpContext object via the Control.Context or HttpContext.Current property.


EXAM TIP

IsEnabled Property The IsEnabled property can be dynamically assigned to turn tracing for a page on or off. It can also be used to include or exclude code based on the Trace setting for a page.


By default, tracing is not enabled. Thus, the trace messages and diagnostics information are not displayed. In the following sections, I'll show you how to enable tracing at the page level as well as the application level.

ASP.NET Page-Level Tracing

You can enable tracing for a Page by using the Trace attribute of the Page directive. When the Trace attribute is set to true in the Page directive, the page appends the tracing information of the current Web request with its output. You can also enable tracing by setting the DOCUMENT object's Trace property to true . Step by Step 9.5 demonstrates how to enable tracing in a page and write trace messages to the trace log.

STEP BY STEP

9.5 Using the TraceContext Class to Display Debugging Information in a Web Application

  1. Add a new Visual Basic ASP.NET Web application named StepByStep9-5 to the solution.

  2. Add a new Web Form to the project. Name the Web Form StepByStep9-5.aspx . Set the page to FlowLayout mode.

  3. Place two TextBox controls ( txtNumber and txtFactorial ), three Label controls, and a Button control ( btnCalculate ), on the Web Form and arrange the controls as shown in Figure 9.11.

    Figure 9.11. Design of a form that calculates the factorial of a given number.

  4. Switch to HTML view of the form in the designer. Add the trace="true" attribute to the Page directive:

     <%@ Page Language="vb" AutoEventWireup="false"  Codebehind="StepByStep9-5.aspx.vb"  Inherits="_305C09.StepByStep9_5" Trace="True"%> 
  5. Double-click the Button control and add the following code to the event handler to handle the Click event:

     Private Sub btnCalculate_Click(_  ByVal sender As System.Object, _  ByVal e As System.EventArgs) _  Handles btnCalculate.Click     ' Write a trace message     Trace.Write("Factorial", _      "Inside Button Click event handler")     Dim intNumber As Integer     Try         intNumber = Convert.ToInt32(txtNumber.Text)     Catch ex As Exception         Trace.Warn("Factorial", "Invalid value", ex)         Exit Sub     End Try     If intNumber < 0 Then         Trace.Warn("Factorial", _          "Invalid negative value")     End If     Dim intFac As Integer = 1     Dim i As Integer     Try         For i = 2 To intNumber             intFac = intFac * i             Trace.Write("Factorial", _              "Value of i: " & i)         Next     Catch ex As Exception         Trace.Warn("Factorial", _          "There was an overflow")     End Try     txtFactorial.Text = intFac.ToString()     Trace.Write("Factorial", _      "Done with computations, returning...") End Sub 
  6. Set the project StepByStep9-5 as the startup project. Set the Web form as the start page for the project.

  7. Run the project. You will notice that the page displays a wide range of information after its general output. Enter a value into the number text box and click the Calculate button. You will see the factorial value displayed in the factorial text box, along with some trace messages in the Trace Information section, as shown in Figure 9.12.

    Figure 9.12. You can view the trace log along with a page's output by setting the trace attribute of the Page directive to true .

  8. Try entering a negative value or a large value (say, 100 ). You'll see the trace messages displayed in the trace log. Warning messages are displayed in red.

As you can see, when tracing is enabled, ASP.NET displays a great deal of information related to the request in addition to messages written by you. The information is displayed by grouping it in different tables:

  • Request Details Includes the session identifier, the time the request was made, the request character encoding, the type of HTTP request (GET or POST), the HTTP response status code, and the response character encoding.

  • Trace Information Includes the messages and warnings generated by the ASP.NET engine or by you by making calls to the Write or Warn methods of the TraceContext class. It displays the information in four columns the category of the message, the trace message, the number of seconds since the first trace message was displayed, and the number of seconds since the most recent trace message was generated on the server.

  • Control Tree Includes the entire collection of controls in the ASP.NET page hierarchically. The information is displayed in four columnsthe control identifier, the fully qualified type of the control, the size (in bytes) of the rendered control including its child controls, and the size (in bytes) of the view state of the control excluding its child controls.

  • Session State Includes the session state only if any data is stored in the session. The Session State table displays the session key, the fully qualified type of the session data stored, and the value of the session data.

  • Cookies Collection Includes the cookies associated with the application. The information displayed is the name of the cookie, the value of the cookie, and its size.

  • Headers Collection Includes the HTTP headers passed to the Web page. It displays the name of the header and its value.

  • Form Collection Includes the form collection. It is displayed only if there is a Web Form defined in the page and the form is posting back from the server. It displays the name of the control in the form and its value.

  • Querystring Collection Includes the querystring collection only if any querystring parameters are passed while requesting the page. It displays the name of the querystring parameter and its value.

  • Server Variables Includes all the server variables associated with the page. It displays the name of the server variable and its value.

As apparent from the preceding list, the trace log definitely helps a great deal to understand the program's execution path . It provides various state information, page control's structure information, and performance information. This information can be of great help in debugging and improving the quality of the program.

ASP.NET Application-Level Tracing

You can enable tracing for the entire Web application using the application configuration web.config file in the application's root directory. Enabling tracing through the web.config file allows you to view the trace information using trace viewer in a separate page instead of displaying it with the page output. The <trace> element is used to configure tracing for an application. The attributes of the <trace> element are

  • enabled Indicates whether tracing is enabled for an application. If enabled, trace information can be viewed using trace viewer.

  • localOnly Indicates whether trace viewer can be viewed by only the local client (running on the Web server itself) or by any client.

  • pageOutput Indicates whether the trace information should be displayed along with the page output.

  • requestLimit Indicates the number of requests whose trace information should be stored on the server. Tracing gets disabled when the request limit is reached.

  • traceMode Indicates the order in which the trace messages should be displayed in the Trace Information section. It can be either SortByCategory (sorted by the Category column) or SortByTime (sorted by the First(s) column).

EXAM TIP

Page-Level Tracing Overrides The page-level trace setting overrides the trace setting for the application. For example, if pageOutput is set to False in the web.config file and if the trace attribute is enabled at page level, the trace information is still displayed along with the page output.


Thus when the tracing is enabled for an application, requests for each of its pages can be viewed using Trace Viewer (unless the Page directive's trace attribute is set to false). Trace Viewer can be viewed by just navigating to trace.axd from any directory in an application. You will notice that no trace.axd file is in the application directory structure; this request is handled by TraceHandler defined in the machine.config file's <httpHandlers> element. Trace Viewer lists all the page requests in an application along with the time of request, the filename, an HTTP status code, the type of request, and a link to view the trace log of the request. It also contains a link to clear the current trace information of all page requests.

Step by Step 9.6 demonstrates how to set application level tracing.

STEP BY STEP

9.6 Setting Application Level Tracing for a Web Application

  1. Open the web.config file of the Web application StepByStep9-5 from the Solution Explorer. Modify the <trace> element defined in the <system.web> element as follows :

     <trace     enabled="true"     requestLimit="10"     pageOutput="false"     traceMode="SortByTime"     localOnly="true" /> 
  2. Remove the trace="true" attribute from the Page directive of the form StepByStep9-5.aspx .

  3. Run the project. Notice that there is no trace information along with the page display. Enter a value into the number text box and click the Calculate button. You will see the factorial value displayed in the factorial text box. You can repeat this step more than once if you like.

  4. Now navigate to trace.axd under your application directory by typing >http:// localhost /StepByStep9-5/Trace.axd (substitute the name of your Web server for localhost if the Web server is not on your development computer). This will load the Application Trace page as shown in Figure 9.13. There will be one POST line in the output for each request you made in step 3.

    Figure 9.13. Trace viewer displays all the page requests in the Application Trace page.

  5. Click the View Details link in the second row (one with post request). Notice that the entire trace log for that page request is displayed as shown in Figure 9.14.

    Figure 9.14. You can view the complete trace log of a page request by using the trace viewer utility (trace.axd).

  6. Now open the web.config file and change the pageOutput attribute to true . Run the project. Enter a value into the number text box and click the Calculate button. You will see the factorial value being displayed in the factorial text box, along with the entire trace log being appended to the trace output. You can also view the trace log with the help of trace viewer as shown in steps 4 and 5.

  7. Now open the form in HTML view in the designer. Add trace="false" attribute in the Page directive. Run the project. Enter a value into the number text box and click the Calculate button. You will see the factorial value being displayed in the factorial text box. You will notice that neither the page output nor the trace viewer displays the trace information. This is because the page-level directive overrides the settings in the web.config file.

  8. Set the enabled attribute to false in the web.config file.

REVIEW BREAK

  • The System.Web.TraceContext class can be used to display trace messages in a Web application. These messages can be easily viewed by using the trace viewer utility or at the end of the page output. ASP.NET displays various page request details, along with any custom trace messages from the code.

  • To enable ASP.NET tracing for a single page, set the trace attribute of the Page directive to true .

  • ASP.NET Tracing also can be enabled at the application level by setting the enabled attribute of the <trace> element to true in the application-wide web.config file.


   
Top


MCAD. MCSD Training Guide (Exam 70-310. Developing XML Web Services and Server Components with Visual Basic. NET and the. NET Framework)
MCAD/MCSD Training Guide (70-310): Developing XML Web Services and Server Components with Visual Basic(R) .NET and the .NET Framework
ISBN: 0789728206
EAN: 2147483647
Year: 2002
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net