Trace Messages

   

The first thing to understand is how to add trace messages to ASP.NET pages. All you must do is add a Trace=True attribute to the @page directive at the top of the ASPX page, as follows :

 <%@ Page language="c#" Trace="true" Codebehind="WebForm1.aspx.cs"     AutoEventWireup="false"     Inherits="Chapter22.WebForm1" %> 

Adding the Trace=True attribute ASP.NET generates extensive information about requests made to that page and appends it as part of a page rendering, as shown in Figure 22.1.

Figure 22.1. Adding Trace=true to the @Page directive causes ASP.NET to output extensive trace information.

graphics/22fig01.gif

Consider for a minute what kind of information is included when you add the Trace=True attribute. For starters, one section gives the request details. Included in this is the section ID (which is a long unintelligible number), the request type (such as GET or POST ), time of request, status code, request and coding (such as Unicode), and the request encoding.

The next section contains Trace information. This comprises trace statements that are output either by the system or by trace statements in your code. For instance, by default there is some boilerplate trace information from the Begin_Load() , End_Load() , Begin_Prerender() methods , and so forth. But you can add custom trace statements by using the Trace.Write() method as follows:

 Trace.Write( "My Message", "This is to let you know that we are here." ); 

You also can use the Trace.Warn() method to send warning messages, as follows:

 Trace.Warn( "My Warning", "You better sell your ACME stock." ); 

The difference between the Trace.Write() method and the Trace.Warn() method is that the Trace.Warn() method displays its trace message in red so that you can pick the warnings out of the list of trace statements more easily. You can see the trace in Figure 22.2.

Figure 22.2. The Trace.Warn() method outputs statements so that you can easily debug applications.

graphics/22fig02.gif

The next section in the trace output is the control tree. This section lists all the controls that have been rendered as results of the page request. You always see the WebForm controls that you have placed on the page. You also see additional controls that were added behind the scenes, which describe items such as the form, any literal text, and other standard HTML items that you have placed in your ASPX code.

The fourth section is the cookies collection. It contains all cookies that have been submitted with requests. Normally, one cookie contains the section ID. Then you also see any additional cookies that exist as part of this request.

The next section is the headers collection. It contains all the information that was received in the HTTP header. This information can be as simple as the host name , or the section can contain a lot of additional information that has been sent from the browser in the HTTP header.

The last section is the server variables. Chapter 3, "Request Objects," spent a good bit of time talking about server variables . Chapter 3 talked about how you could use the Request object and call the Request.Server() method to find out this very same information. But in this section it is all summed up for you so that you don't have to make a lot of calls to Request.Server() to find this information. The information that you see in the server variable section includes the HTTP header, the remote host, pass information, a physical disk path , and much more.

Regarding the Trace property (which contains the Write() and Warn() methods), I want to point out that it references an instance of the TraceContext class. You can query the TraceContext object to learn whether tracing has been enabled. All you need to do is look at the enabled property of the TraceContext object as the following code demonstrates :

 if( Trace.IsEnabled )  {    Trace.Write( "Message", "I am glad that tracing has been enabled." );  } 

Tracing can be enabled at the application level, as opposed to the page level. If you enable tracing at the application level, ASP.NET collects page traces for each request made to the application. You enable tracing at the application level in the Config.Web file. To view the trace output generated by the entire application, you would need to navigate to http://localhost/myapplication/trace.axd. You simply set the trace enabled to true as follows:

 <configuration>    <trace enabled="true" />    <pageOutput="true">  </configuration> 

Table 22.1 lists all the attributes you can use to modify the behavior of application-level tracing.

Table 22.1. Trace Attributes That Can Be Added to ASP.NET Applications

Property

Description

enabled

True if tracing is enabled for the application; otherwise false . The default is false.

pageOutput

True if trace information should be displayed both on an application's pages and in the .axd trace utility; otherwise false . The default is false . Note: Pages that have tracing enabled on them are not affected by this setting.

requestLimit

Number of trace requests to store on the server. The default is 10.

traceMode

Indicates whether trace information should be displayed in the order it was processed , SortByTime , or alphabetically by user -defined category, SortByCategory . SortByTime is the default.

LocalOnly

True if the trace viewer (trace.axd) is available on only the host Web server; otherwise false . The default is true .

   


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233

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