ASP.NET Tracing

Team-Fly    

Developing XML Web Services and Server Components with Visual C#™ .NET and the .NET Framework, Exam Cram™ 2 (Exam 70-320)
By Amit Kalani, Priti Kalani

Table of Contents
Chapter 10.  Testing and Debugging


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). The TraceContext class of the System.Web namespace is responsible for gathering execution details of a Web request. You can access the TraceContext object for the current request through the Trace property of the Page class. After you have the TraceContext object, you can invoke its member methods to write trace messages to the trace log. Table 10.4 lists some of the important members of the TraceContext class that you should be familiar with.

Table 10.4. 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, or SortByTime.

Warn()

Method

Writes the messages to the trace log in red, which indicates that they are warnings.

Write()

Method

Writes the messages in the trace log.

graphics/alert_icon.gif

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.


You can enable tracing for a page 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.

Take the following steps to enable tracing in a Web application and display debugging information:

  1. Add a new Visual C# ASP.NET Web application project at the following location: http://localhost/EC70320/C10/Example10_2.

  2. Set the pageLayout property of the DOCUMENT element to FlowLayout. Place one TextBox control (txtNumber), a Button control (btnCalculate), and a Label control (lblResult) on the Web form.

  3. Switch to the HTML view of the form in the designer. Add the trace="true" attribute to the Page directive, as shown in the following:

     <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="Example10_2.WebForm1" Trace="true"%> 
  4. Double-click the Button control and add the following code to the event handler to handle the Click event:

     private void btnCalculate_Click(object sender, System.EventArgs e) {     Trace.Write("Factorial", "Inside Button Click event handler");     int intNumber;     try     {         intNumber = Convert.ToInt32(txtNumber.Text);     }     catch (Exception ex)     {         Trace.Warn("Factorial", "Invalid value", ex);         return;     }     if(intNumber < 0)         Trace.Warn("Factorial", "Invalid negative value");     int intFac = 1;     for (int i = 2; i <= intNumber; i++)     {         intFac = intFac * i;         Trace.Write("Factorial", "Value of i: " + i);     }     if(intFac < 1)         Trace.Warn("Factorial" , "There was an overflow");     lblResult.Text = String.Format(         "The factorial of {0} is {1}", intNumber, intFac);     Trace.Write("Factorial" , "Done with computations."); } 
  5. Set the project as the startup project, and run the project. Enter a small value, such as 5, into the number text box and click the Calculate Factorial button. You will see the factorial value displayed, along with some trace messages in the Trace Information section.

  6. Try entering a negative value or a larger value, such as 100, and notice the trace messages displayed in the trace log. You should see warning messages displayed in red.

You can enable tracing for an entire Web application using the application configuration file (web.config). The <trace> element is used to configure tracing for an application; its attributes are as follows:

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

  • localOnly Indicates whether the 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 is disabled when the request limit is reached.

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

graphics/alert_icon.gif

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 the trace attribute is enabled at the page level, the trace information is still displayed along with the page output.


Enabling tracing through the web.config file enables you to view the trace information using the trace viewer in a separate page (trace.axd) instead of displaying it with the page output. The following code segment shows how to modify the <trace> element defined in the <system.web> element of the web.config file:

 <trace enabled="true" requestLimit="10"     pageOutput="false" traceMode="SortByTime"     localOnly="true" /> 

    Team-Fly    
    Top


    MCAD Developing XML Web Services and Server Components with Visual C#. NET and the. NET Framework Exam Cram 2 (Exam Cram 70-320)
    Managing Globally with Information Technology
    ISBN: 789728974
    EAN: 2147483647
    Year: 2002
    Pages: 179

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