Please navigate to the bottom of the page for Table of Contents

Tuesday, May 24, 2011

Tracing page execution in ASP.NET

One common interview question (and a real world tool too) is about tracing page execution. The question can be framed in a variety of ways: How can you debug an ASP.NET application that is behaving badly? OR How can you add trace information to the page output? OR How can you enable application level tracing for an ASP.NET web application?, etc.

In simple terms, if you want to output trace messages while a page executes, you can enable tracing for a
particular page or an entire application. The ASP.NET Framework supports both page-level tracing and application-level tracing.

Page level tracing

To enable tracing for a specific page, add Trace=”true” to the @Page element in the .aspx file as shown below:

<%@ Page Language="C#" Trace="true" %>

This Trace attribute enables tracing and causes a Trace Information section to be appended to the bottom of the page. A complete demo default page might look something like this:

<!-- Since this is a demo example, the aspx page does not have a code behind 
and put the C# code and server side functions in the aspx page itself -->

<%@ Page Language="C#" Trace="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<script runat="server">
   2:     void Page_Load()
   3:     {
   4:         for (int counter = 0; counter < 10; counter++)
   5:         {
   6:             demoListBox.Items.Add("item " + counter);
   7:             // NOTE: Trace.Warn calls output the information
   8:             // NOTE: in the trace window
   9:             Trace.Warn("counter=" + counter);
  10:         }
  11:     }

<html xmlns="">
<head runat="server">
<form id="form1" runat="server">
<asp:ListBox ID="demoListBox" runat="server" />

The corresponding sample output from the trace commands is shown in the picture below:


There is a wealth of information that you can glean out of the trace output. In the example above the Page_Load() handler uses the Trace.Warn() method to write messages to the Trace Information section. You can output any string to the Trace Information section that you want.

You need to take advantage of page tracing when you want to determine exactly what is happening when a page executes. You can call the Trace.Warn() method wherever you need in your code. Because the Trace Information section appears even when an error exists on your page, you can use tracing to diagnose the causes of any page errors.

Remember: If you leave this trace attribute in your production code and deploy it, it will be visible to your customers. Take extra precaution to remove it before you deploy your application to production servers.

Another important property that is available to you is called TraceMode as shown below:


This can take either SortByCategory or SortByTime (default). You can set to SortByCategory to sort alphabetically by user-defined category.

Application level tracing

One disadvantage of page tracing is that everyone in the world gets to see your trace information. You can get around this problem by taking advantage of application-level tracing. When application-level tracing is enabled, trace information appears only when you request a special page named Trace.axd.

To enable application level tracing, you need to enable it in the web.config file as shown below:

<?xml version="1.0"?>
<trace enabled="true" />


Once you have enabled tracing in the web.config file, you can load trace.axd to view the trace information.

NOTE 1: By default, the Trace.axd page cannot be requested from a remote machine. If you need to access the Trace.axd page remotely, you need to add a localOnly=”false” attribute to the trace element in the web configuration file.

NOTE 2: If you would like to see the trace output on individual pages, you need to add the pageOutput="true" attribute to the trace element in the web configuration file.

<?xml version="1.0"?>
<trace enabled="true" localOnly="true" pageOutput="true"/>
Now that you are armed with the basic knowledge, it is important to understand that the <trace> element in the web.config file has many configurable properties. I urge you to read more by visiting the MSDN section on ASP.NET tracing overview.


  1. Good Work,Definitely it will help Proffessionals.

  2. Heya just wanted to give you a quick heads up and let
    you know a few of the pictures aren’t loading correctly.
    I’m not sure why but I think its a linking issue. I’ve tried it in two different web browsers and both show the same outcome.... My Web : Jasa SEO Indonesia Murah Terbaik dan Profesional