Understanding Tracing in ASP.NET 2.0
 
Published: 23 Aug 2007
Abstract
This article describes the concept of Tracing in ASP.NET 2.0.
by SANJIT SIL
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 27549/ 72

Introduction

In Microsoft .NET Framework we find a "Debugging and Tracing" feature for sorting and fixing errors, which we find in the course of developing our web application and even sometime after the deployment of the application. When the application are in the development stage, using debugging feature developers can set break points in their code and can observe the values of variables using watch window. But the use of debuggers becomes a huge task for administrators considering the security and related issues in the production environment. In classic ASP, using Response.Write method we can display an error message. Although it is easy to use, it has some drawbacks. Before deployment we have to remove or comment out all those Response.Write statement wherever we have used in the application. We cannot forget to remove or comment out any such statement that can be visible to end user/client.

Moreover, when we are working on any live site and using Response.Write to fix any error in respect of any particular page/pages, there is always a possibility that the same can be visible to the client as well. ASP.NET introduces new functionality that allows us to write debug statements directly in our code, without having to remove them from our application when it is deployed to production servers. This feature provides a refined way for outputting page-level information. It uses two methods: Trace.Write and Trace.Warn. It should be noted that in some cases tracing provides some extra facilities compared to debugging for example, showing the amount of information in view state, page processing time, etc. We can enable tracing for the page or application. The sample code snippets have been written in C#.

Page level Tracing

We can enable ASP.NET tracing on a page-by-page basis by adding "Trace=true" to the Page directive in any ASP.NET page.

Listing 1

<%@ Page Language="C#" Trace="true" TraceMode = "SortByCategory" 
    Inherits  = "System.Web.UI.Page" CodeFile="Default.aspx.cs" %>

We can also add the TraceMode attribute that sets SortByCategory or the default, SortByTime. SortByTime option is used to see the methods that take up the most CPU time in application. We can enable tracing programmatically using the Trace.IsEnabled property.

Application Tracing

We can enable tracing for the entire application by adding tracing settings in web.config. In code Listing 2, pageOutput="false" and requestLimit="25" are used. This means trace information is stored for 25 requests, but not displayed on the page because pageOutput attribute is set to false.

Listing 2

<configuration>
    <appSettings/>
    <connectionStrings/>
    <system.web>
        <compilation debug="false" />
        <authentication mode="Windows" />
      <trace enabled ="true" pageOutput ="false" requestLimit ="25" 
          traceMode ="SortByTime " />        
    </system.web>
</configuration>

 

The page-level settings take precedence over settings in Web.config, so if enabled="false" is set in Web.config but trace="true" is set on the page, tracing will occur.

Viewing Trace Data

Tracing can be viewed for multiple page requests at the application level by requesting a special page called trace.axd. We can enable tracing in Web.config as shown in code Listing 3.

Listing 3

<system.web>
<compilation debug="false" />
<authentication mode="Windows" />
<trace enabled ="true" pageOutput ="true" />        
</system.web>
protected void Page_Load(object sender, EventArgs e)
{
  System.Diagnostics.Trace.Write("This is Page_Load method!");
}

Trace.Write() method can be used in the Page_Load event as shown in the above code listing. We can also use the Trace.Warn method. When Trace.Warn method is used the trace output will display the given message in red. When we run the page, we can see (because we have set PageOutput=true) eleven different sections in the browser which provide a great deal of information as specified below.

Request Details: This section includes the ASP.NET Session ID, the character encoding of the request and response, the HTTP conversation's returned status code, the time of request and the type of web request.

TraceInformation: This section shows the different stages of processing that the page went through before being sent to the client. Each section has additional information about how long it took to complete. The timing information located here is valuable when profiling and searching for methods in the application that take too long to execute.

Figure 1

Control Tree: Control tree presents an HTML representation of the ASP.NET Control Tree. It shows each control's unique ID, run time type, the number of bytes it took to be rendered, and the bytes it requires in View State and Control State. This helps us determine whether the enabling control state could affect page transmission times.

Session State: This section displays every item in a particular user's session, their types and their values.

Application State: This section displays every item in the current application's Application object and their type and values.

Request Cookies: This lists all the cookies passed in when the page is requested.

Response Cookies: Lists all the cookies that were passed back during the page's response.

Headers Collection: Shows all the headers that might be passed in during the request from the browser, including Accept-Encoding, indicating whether the browser supports the compressed HTTP responses and Accept languages.

Form Collection: This section displays all of the Form Collection and all its keys and values.

QueryString Collection: This section displays the variables and values submitted in the Querystring collection in the form of keys and values.

Server Variables: This section lists all the server variables and their contents.

Trace.axd: Using page output of tracing we can get only the data collected for the current page request. However, if we want to collect detailed data for all the requests then we need to use Trace.axd. We can invoke Trace.axd tool for the application using http://machinename/applicationname/trace.axd, which is shown below.

Figure 2

In the above figure we can see that there are three request that have been made to this application and "Remaining:7" in the right side of the header suggests that there are seven more remaining requests before tracing stops for this application. The request limit can be set in Web.config which is already shown in code Listing 2.

Following are different options that we can set in web.config file for application tracing:

Enabled: This turns tracing on or off for all pages. It can be override on a page-by-page basis with the Page directive.

traceMode: Determines the sort order of trace messages. Values are SortByTime and SortByCategory.

localOnly: It determines whether tracing information will be visible only to the user of local computers or can be visible to the user of a remote computer. By default it is true.

pageOutput: It determines whether tracing information will be displayed on the page or not by setting values to true or false.

requestLimit: This is any integer number of page requests that are stored and displayed via trace.axd when we are using application level tracing. If we specify any value greater than 10000, ASP.NET treats it as 10000.

mostRecent: If true, ASP.NET keeps only the most recent trace messages. If it reaches the maximum number, the oldest request is removed whenever any new request comes. If it is false, ASP.NET stops collecting new trace messages and ignores subsequent requests. The default value is false.

writeToDiagonosticsTrace: If true, all calls to System.Web.UI.Page.Trace.Write are also forwarded to the System.Diagnostics.Trace.Write and enables us to use all the standard TraceListeners. The default is false

References

Conclusion

As we know, once an application is deployed to a web server the debugger cannot be used without installing Remote Debugging. So Tracing is a simple debugging technique that is superior to classic ASP's Response.Write method and Tracing can be enabled at both the page and Web application-level. Additionally, trace log can be used for performance monitoring and timing.



User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-10-19 2:13:36 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search