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"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
   1:  
   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:     }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="demoListBox" runat="server" />
</div>
</form>
</body>
</html>


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


image





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:


image


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"?>
<configuration>
<system.web>
<trace enabled="true" />
</system.web>

</configuration>


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"?>
<configuration>
<system.web>
<trace enabled="true" localOnly="true" pageOutput="true"/>
</system.web>
</configuration>
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.

No comments:

Post a Comment