What is Tracing?
Tracing is an activity of recording the diagnostic information related to a specific web page that is being executed on the web server. In this discussion we will consider ASP.NET related tracing technique.
Need of Tracing
When the application are in development stage, developers can use in-built debuggers for troubleshooting, but in the production environment, using debuggers becomes a huge task for administrators considering the security and related issues. To debug Classic ASP applications, we were having no mechanism to track the execution flow of ASP pages. Thanks to .NET !!. Using .NET technology we can trace the flow with really less efforts. To collect the statistics like execution time, contents of certain object, “response.write” was the only way in classic ASP. Using trace we are now able to view the HTTP headers, session state and likewise information on the trace output. Also, we have flexibility to add our own contents in the trace output.
On the Page
When this method is used, the trace output is displayed on the page that is executed.
Out of Page
In this method, the tracing results are not displayed on the page but these are stored on the web server and in the root folder of the application in a file named as “trace.axd”. After execution of the pages, this file can be viewed on the browser. For example http://yourApplicationroot/trace.axd.
How Do I trace?
The tracing can be enabled at two scopes:
- Page Level
- Application Level
Working with Page Level Tracing
The page level tracing can be enabled using page directive called “
Trace”. If you look at the top of the page, you will find a line something like below:
<%@ Page Language="vb" AutoEventWireup="False" Trace="True"
Also, we have inbuilt
Trace object to enable and disable the tracing dynamically. In the page, on
Load event if you write the below line, the tracing will be enabled.
Trace.IsEnabled = True
Working with Application Level Tracing
When you are working with page level Tracing, you need to know probable page that is to be traced. If your application is complex and if you want to trace you might not be aware exact page that is to be traced. To avoid this problem we, .NET platform, provides a facility to enable the tracing at application level.
As we know that web.config is a file that manages the application level settings. In the same file we can mention setting of the Trace.
<trace enabled="true" pageOutput="true" requestLimit="10"
traceMode="SortByTime" localOnly="true" />
Attributes of Trace Tag
Enabled - Enable or Disable tracing for entire application.
PageOutput – If this attribute value is set to “
true” then the trace output will be displayed on the page. If value of this attribute is set to
false trace, then the trace output can be retrieved using browser. Typically the URL would be http://<yourAppliccationRoot>/trace.axd.
RequestLimit– This attribute is active only when the
PageOutput attribute value is set to
false. This attribute defines the number of page requests that are to be traced.
TraceMode – To manage the order of the trace output, this attribute is used. For Example –
localOnly - Set this attribute to "
false" if you want access the trace log from any client. Otherwise the log will be displayed on local machine only.
When the trace is enabled we get the trace output. This output contains lot of details, some of the sections are as follows:
- Request Details - This section talks about the basic information like session ID, Request Time, Type of HTTP Request, HTTP response details.
- Trace Information - This section provides messages and categories. Also, this section will display the log that we have displayed using
- Control Tree - This section displays the information about controls that are used within the page that is being executed.
- Cookies Collection - If the page is using any cookies, this section will display the details of those cookies.
- Header Information - This section displays detail of the header that is transmitted. This will be a pair of name and value.
- Server Variables - This section will display all server variables. Again, in the form of name and value pair.
Using Trace Object
There are lot of tricks that we can use to enable / disable and write trace. A common and widely used technique is to pass a parameter using
QueryString and enable or disable the trace.
For example, you write the below given code in the page load event:
If (Context.Request.QueryString("EnableTrace") = "true") Then
Context.Trace.IsEnabled = True
And you give the URL to enable the trace:
For writing the trace, we use the same object. Anywhere in the page, if the below line is written, then the trace output will display the same.
If Trace.IsEnabled Then
Trace.Write (“Sample trace output / Custom message”)
Also, we can use the
Warn method. When
Warn method is used the trace output will display the given message in red.
If Trace.IsEnabled Then
Trace.Warn (“Sample trace output / Warning / Custom message”)