Tracing ASP.NET Website

In ASP.NET there two types of Tracing

  • Application Level
  • Page Level

Page level Tracing takes the precedence over the Application Level tracing.

Lets start with creating new website.

In web.config add following entries to enable Application level tracing below System.web element.



pageOutput=”true” – add the tracing information at the bottom of the ASPX page.

localOnly=”false” – tracing will be available to any web page.

requestLimit=”10″ – How many request should be saved on the server.

mostRecent=”true” – whether or not display the recent tracing information if the request reach at the limit.

After adding above tag in web.config, the output of ASP.NET page will look like:

ASP.NET Tracing Output

Here you can see much of informations displayed like SessionId, Request Status, Controls available on that page,  All the events of the page with start and end time etc and thus you can figure out the performance of your web application.

No Trace for a particular Page

Create a web page and in aspx file and at header write the tag Trace=”false”

Create few controls like link button and view the page in browser. You will see that the page is displayed with no tracing information, although its turned ON in web.config but page level tracing has the precedence over application level tracing.

Writing Custom Trace Information

Now on the click event of Link buttons write the custom trace information.

protected void lblwriteMessage_Click(object sender, EventArgs e)
	Trace.Write("Custome Message", "Write Link Button clicked");
protected void lblwarnMessage_Click(object sender, EventArgs e)
	Trace.Warn("Custome Message", "Warn Link Button clicked");

when you will click on the WriteLink button, below output will be seen.


and when you will click on warn button, the message will be displayed in Red color. so it is suggested to display the the important trace message as a warn.


Page.Trace Vs System.Diagnostics.Trace

Create a new class file and write a static function which will write the Trace Message. You will notice that Trace object is not available by default like ASPX page instead you will need to import the package “System.Diagnostics”.

So, there is lot difference in System.Diagnostic.Trace and Page.Trace.

Trace from Diagnostics display the trace information in output window of Visual studio whereas the Trace from page displays the Trace information in ASPX Page.

Integrate System.Diagnostics with ASPX Page (Routing all Trace information to web page)

We will need to add the listener in web.config file to route all the tracing information to the single web page.

To integrate the System.Diagnostics Trace with ASPX page, write below line of code in web.config.

       <add name="WebPageTraceListener"
            type="System.Web.WebPageTraceListener, System.Web, Version=2.0.3600.0, Culture=neutral,

The output will look like below image:
ASP.NET Tracing Application Level
trace.axd file

This file contains all the information about the tracing of the application.

To view the trace information just write the file name and you will get page which will look like below snap.

path structure : http://<servername>/webapp/trace.axd

Application Trace in ASP.NET
Application Trace in ASP.NET

Creating Custom Trace Listeners

Database trace listener

.NET has provided us the flexibility of writing our own Trace Listeners in the form of the TraceListener class. Every Trace Listener is inherited from this class; therefore, in order to implement your own Trace Listener, you must inherit your Trace Listener class from this class.

TraceListener class has many virtual and abstract methods; at the very least, each inheritor of this class must implement the Write and WriteLine methods; other important methods are Fail, Close, and Flush. Inheritors are not supposed to implement these methods but it is a good idea to implement these methods. Descriptions of these methods are given in the beginning of the article.

Method Name Result
Fail Outputs the specified text with the Call Stack.
Write Outputs the specified text.
WriteLine Outputs the specified text and a carriage return.
Flush Flushes the output buffer to the target media.
Close Closes the output stream in order to not receive the tracing/debugging output.

Write and WriteLine methods are overloaded; following is a list of all the overloaded version of the Write method:

  • public override void Write(string message)
  • public override void Write(object o)
  • public override void Write(string message, string category)
  • public override void Write(object o, string category)

For this article, I have created a Trace Listener, DatabaseTraceListener, which actually stores trace and debug messages into a database.

add below code in Web.config which stores the connection string information.

<pre><add key="ConnectionString" value="Data Source=.SQLExpress;Integrated Security=True;User Instance=True;
AttachDBFilename=|DataDirectory|TraceDB.mdf" /></pre>
<pre><add key="MaximumRequests" value="2" /></pre>

The structure of Table is:

  • TraceDateTime column stores the date and time of the trace message
  • TraceCategory column stores the actual category of the trace message
  • TraceDescription column stores the trace message
  • StackTrace column contains the stack trace
  • DetailedErrorDescription column contains the detailed error message which will be passed in second parameter of the Fail method.
  • create below stored procedure in SQLExpress

    Stored Procedure ASP.NET Tracing
    Now create a class named “DatabaseTraceListener” which inherit the abstract class “TraceListener”. You can check the code snippets in Source code of this article.

    if you want to implement your own Trace Listener, simply derive your listener class from TraceListener class, implement at least the Write and WriteLine methods and you are done.
    In our Example, Flush and Close methods simply save all the cached messages into the database.

    Saving Trace information in File

    We can also save the trace information in log files.

    To save the trace information in File, simply add the following entry in web.config

    <add name="myListener"
    initializeData="TextWriterOutput.log" />

    1 comment

    Leave a comment

    Your email address will not be published.

    This site uses Akismet to reduce spam. Learn how your comment data is processed.