How and Why to Use Trace Option for Debugging in ASP.NET Application

When you are in testing phase of your application, trace option will be of much help to you. Are you using Response.Write( ) statements in your code for debugging? Then this article is of more relevance to you.

If you use Response.Write( ) statements during testing phase, you will have to ensure that all such statements are commented out at the time of production. If not, it will be displayed to Users. Instead, there in an easy and much more efficient approach to be followed. The approach is all about using Tracing in your ASP.NET Application.

Tracing can be performed in .NET in two different ways:

• Using System.Web.TraceContext class
• Using System.Diagnostics.Trace Class and System.Diagnostics.Debug class

This article will focus upon the first option and discuss about usage of trace option with the help of System.Web.TraceContext class. The second option is specific to VS.NET. All trace messages recorded using second option can be viewed in output window of VS.NET.

How do you achieve tracing Using System.Web.TraceContext Class?

You can achieve tracing either at Page Level or at Application Level.

Page Level Tracing:

You can enable tracing for a particular page by including the following page directive:

<%@ Page Trace="true"%>

This directive can also include one more attribute called TraceMode which can accept values SortByTime or SortByCategory. If the TraceMode is mentioned as SortByTime, then the trace information will be sorted and displayed based on time. While including trace messages, you can specify the category. And if you mention the TraceMode as SortByCategory, then the trace information is sorted based on category.

For example, if there are multiple developers working on the same page in different tasks then each task can be named as a category and when you can write trace messages by specifying category as well. Now when the trace information is displayed, you can easily trap trace messages you have included with the help of category since all messages specified in a particular category will be displayed one after the other. Here is the syntax to include the TraceMode attribute:

<%@ Page Trace="true" TraceMode=”SortByCategory”%>


<%@ Page Trace="true" TraceMode=”SortByTime”%>

If TraceMode is not mentioned, then again it is assigned with SortByTime by default.

Tracing is now enabled in your page. How will you write trace messages? Here is an example illustrating usage of tracing:

<%@ Page Trace="true" TraceMode=”SortByCategory”%>
public class testClass{
public int classVar;
public void testTracing() {
Trace.Write(“Category1”,” Beginning of testTracing method”);
if(Trace.IsEnabled) {
If(classVar == 0) {
Trace.warn(“Category1”, “classVar is still not assigned with value”);
Trace.Write(“Category2”, “Testing SortByCategory”);
Trace.Write(“Category1”, “End of testTracing method”);

In this example, Trace.Write statements are used to display trace messages in the browser. Trace.Warn is also similar to Trace.Write, but only difference is that the message provided in Trace.Warn will be shown in red color. If you want to execute certain block of statements if and only if tracing is enabled, then you can use Trace.IsEnabled which will return true if tracing is enabled else it returns false. In the above mentioned examples, Trace.Write and Trace.Warn includes two attributes.

First attribute is the category name and the second attribute is the message to be displayed. When the trace information is displayed, it will be sorted in the order of category since we have mentioned the TraceMode as SortByCategory in page directive. Hence all Category1 trace messages will appear one after the other and so does the trace messages for Category2. This reduces searching effort and makes your debugging easier. Category attribute is optional. Trace.Write and Trace.Warn can include only one attribute containing the message.

If you don’t want to use tracing, you need not comment all Trace.Write and Trace.Warn statements, all that you have to do is remove the following line from your code:

<%@ Page Trace="true" %>

This is because tracing is disabled by default. An alternative way is to set the Trace attribute to false in the page directive:

<%@ Page Trace="false" %>

Application Level Tracing:

If you want tracing to be enabled for the whole application, then add the following entry in web.config file:

<trace enabled="true">

This trace section mentioned above has many more properties associated with it. Here’s an example including all other properties:


Significance of each of the above mentioned property is given below:

• enabled: Tracing is enabled for the entire application if this property is set to true. Default value is false.

• traceMode: It can accept either SortByCategory or SortByTime, to decide on the order of display of the trace information. Default traceMode is SortByTime.

• requestLimit: This number indicates how many trace requests have to recorded in the server. Default value for this property is 10.

• pageOutput: Decides whether the trace information has to be provided for every page or it can only be viewed using the utility trace.axd. By default the value is false.

• localOnly: If you want trace information to be viewed only by users connecting through localhost then you can set this property to true. Set it to false if the trace information has to be viewed by remote users as well. By default, its value is true.

Once the appropriate setting is done in web.config file, you can view all trace information that you have set at page level. In addition, you can view consolidated information of all requests (till the requestLimit specified).

Consolidated information includes Request Detail, Trace Information, Control Hierarchy, Session State, Application State and much more. You can view all these information in trace.axd. Assume that you are running your application from localhost and your application name is testTraceApp, then you can access trace.axd using the link below:


trace.axd is an HTTP Handler supported by ASP.NET and it is not a file.

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.


Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage


Recommended Resource

|How and Why to Use Trace Option for Debugging in ASP.NET Application | How to Display Tool Tip in Your ASP.NET Web Application | How to Use “const” Class Member Modifier in C# | How to Use HTML Help Viewer in Your ASP.NET Web Application | List of Preprocessor Directives Available in C# | More about #define, #undef and Conditional Compilation Directives in C# |Understanding Different Class Member Modifiers in C# | Understanding of Diagnostic Directives and #pragma, #line, #region, #endregion Directives in C# | Usage of Roles API to Perform Authorization in .NET| What are the Different Compiler Engineering Techniques|Writing Unsafe Code in C#|


“Amazon and the Amazon logo are trademarks of, Inc. or its affiliates.”

| Privacy Policy for | Disclosure | Contact |

Copyright - © 2004 - 2023 - All Rights Reserved.