Click here to Skip to main content
15,868,292 members
Articles / Web Development / ASP.NET
Article

Tracing web application with ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.36/5 (24 votes)
25 Apr 20054 min read 113.7K   42   5
This article explains tracing machanism of .NET.

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.

Tracing Methods

  • 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:

ASP.NET
<%@ Page Language="vb" AutoEventWireup="False" Trace="True" 
  Codebehind="TraceTest.aspx.vb" Inherits="TracingExample.Test"%>

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.

VB
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.

ASP.NET
<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 – SortByTime, SortByCategory etc.
  • 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.

Trace Output

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 Trace.Write or Trace.Warn.
  • 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:

VB
If (Context.Request.QueryString("EnableTrace") = "true") Then
   Context.Trace.IsEnabled = True
End If

And you give the URL to enable the trace:

http://MyAppRoot/mypage.aspx?EnableTrace=true

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.

VB
If Trace.IsEnabled Then
      Trace.Write ("Sample trace output / Custom message")
End if

Also, we can use the Warn method. When Warn method is used the trace output will display the given message in red.

VB
If Trace.IsEnabled Then
     Trace.Warn ("Sample trace output / Warning / Custom message")
End If

Further Reading

Refer Msdn.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Web Development:
.Net 2.0, VB.net, ASP.net, Classic ASP, VBScript, Javascript, HTML, DHTML,

OpenSource:
Perl, PHP, Linux, Apache, IIS.

Desktop Developement:
VB.net, VB 6, COM/DCOM, Winsock, MAPI, CDO, Outlook Object Model, LDAP, C, C++, Foxpro 2.5, Clipper.

Database:
MS SQL - Server, MySQL, PostgreSQL

Other:
Enterprise Architect, Microsoft Project, Rational Rose, UML.

Has Nine+ years of experience with IT and now working in Project Manager's Role.

Comments and Discussions

 
GeneralMy vote of 5 Pin
kiran shettar30-Nov-12 21:43
kiran shettar30-Nov-12 21:43 
GeneralHelpful Pin
Brian Stevens9-Apr-09 14:21
Brian Stevens9-Apr-09 14:21 
GeneralMind Blowing article Pin
Viju Khote14-Dec-07 10:07
Viju Khote14-Dec-07 10:07 
GeneralNice one...useful.... Pin
Good Guy27-Mar-07 1:35
Good Guy27-Mar-07 1:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.