In earlier article we learnt that how to enable debugging in ASP.NET application. In this article we shall learn how to trace the asp.net application or website to know that how the page execution is happening and which method is taking how much time.
Introduction
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.
ASPX PAGE
<%@ page language="C#" trace="true" autoeventwireup="true" codefile="TracePage.aspx.cs"
inherits="TracePage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
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.
CODE BEHIND
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.
OUTPUT
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?
Web.config
<system.web>
<trace enabled="true" pageOutput="true" />
</system.web>
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.