Tracing in ASP.NET

ASP.NET Tracing can be used to investigate the behaviour of the site at run time. Usually to know the value of a particular variable at runtime, you may thought of printing them on screen by assigning text to a control. ASP.NET Framework built-in feature tracing can be used to know the values at runtime without writing any code. You can add additional information to Trace. This post outlines the built-in tracing capabilities and explains the steps to add the additional information to the trace.

Tracing Features

To see the tracing information, you need to enable tracing at the page level or at the site level. By enabling it at page level you can see tracing information on one or more specific pages. Application level tracing is useful when you want to see multiple-pages at same time.To enable tracing in a page, you need to set it Trace attribute value to true in Page directive.

<%@ Page Title="Home Page" Trace="true" TraceMode="SortByCategory" Language="C#"  %>

When you run the trace enabled page, the trace information appears at the bottom of the page as shown below


You can see the request details like request type,date/time and status code it returned at the top of trace details.Additional information on page event lifecycle can be found below the request details. Two time columns, first column shows you the time that took to process since  the first trace message. Second column shows you the elapsed time in seconds.

If you go further down in tracing page then you will see the Control tree which shows you the hierarchical view of the controls in your page.


Below the control tree you see the information about Session, Query String, Cookies and Server variable values


You can enable tracing to the entire website by creating a trace element in web.config file as shown below

<trace mostRecent="true" enabled="true" requestLimit="300" pageOutput="false" localOnly="true"  />

When you set the mostRecent attribute value to true then it discards the old items when number of elements in trace reaches the request limit. When you set pageOutput attribute to false it then does not add the trace output to the page. To see the trace information in this case, you then need to send the request trace.axd file.

Adding Information to the Trace

Adding your own information to Trace is useful when you want to see the variable values or to find out specific information about events. Two methods available in Trace class to do this, one is Write and second one is Warn. Both methods renders the information in similar manner except Warn message appear in red color.

Trace.Write("Showing the variable value", strDebug);
Trace.Warn("Custom Category", "Collection is empty!!!");

Adding the trace information using Write and Warn methods can hurt the system performance. By using enable attribute value in web.config file you can either enable or disable the trace.

Related Posts:

%d bloggers like this: