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.
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.
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 STEP9.5 Using the TraceContext Class to Display Debugging Information in a Web Application
|
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.
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 STEP9.6 Setting Application Level Tracing for a Web Application
|
REVIEW BREAK
|
Top |