How to Trace an ASP.NET Application?

Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 14975 red flag
Rating: 5 out of 5  
 1 vote(s)

In earlier article we learnt that how to enable debugging in ASP.NET application. In this article we shall learn how to trace the application or website to know that how the page execution is happening and which method is taking how much time.


Tracing is a mechanism to monitor the execution of the application at development as well as in the production (live) environment. In order to demonstrate how to trace an application, we are going to create a sample page.


<%@ page language="C#" trace="true" autoeventwireup="true" codefile="TracePage.aspx.cs"

inherits="TracePage" %>

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


<html xmlns="">

<head id="Head1" runat="server">




<form id="form1" runat="server">






Notice that we have kept trace="true" attribute in the Page directives in the above code snippet that enables tracing for this page. 

Watch the video of this topic below

Watch video of hundreds of ASP.NET Tips and Tricks here.


protected void Page_Load(object sender, EventArgs e)


Trace.Write("Trace - STARTED Page_Load");

Response.Write("Some data");

Trace.Write("Trace - ENDED Page_Load");


In the code behind of this page, wherever we want we can add the Trace statement. (Remember that Trace statement works in both development as well as release environment).  For example, we have added two trace statement in the above code snippet, you can add any data value in the trace statement that you need to know the execution of the program.


When the above page runs, it gives below output.

In the above picture, notice the output of the page in the browser. In case tracing statements are written on the page and we do not want tracing output, we can change Trace=true to Trace=false in the page directives. We do not need to comment the Trace statements from the code behind file. 

Above example enables tracing at the page level, in case we want to enable or disable tracing at application level we can follow below approach.

How to enable/disable tracing in all pages of the application?


<trace enabled="true" pageOutput="true" />
In the above code snippet, we have kept trace element in the web.config file with 

  • enabled attribute value to true that enables tracing for all pages of the application. If we want to disable tracing at the application level, we can set its value to false.
  • pageOutput value to true that gives the tracing output on the page
In case tracing is enabled at the page level (by specifying trace=true in the Page directive) and tracing is disabled in the web.config file, the page level settings takes precedence.

Stay tuned for more article on Tracing, in the next article we shall learn how to consolidate and save the tracing information in the XML file.

Thanks for reading! Subscribe for RSS feed from below link to get new articles alert directly into your inbox.
Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan

Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on | |

Login to vote for this post.

Comments or Responses

Posted by: Seenuvasan on: 6/30/2011 | Points: 25
nice artical sheo
Posted by: SheoNarayan on: 6/30/2011 | Points: 25
Thanks Sreenuvasan, keep reading more article in this series.

Posted by: Akiii on: 7/3/2011 | Points: 25
good article Sir....looking forward for more..

Thanks and Regards

Login to post response

Comment using Facebook(Author doesn't get notification)