Enable tracing at application and page level in ASP.NET applications

When we debug our applications we need to have as much information as possible at our disposal. There are a lot of techniques and ways to achieve that.

One way to get lots of debugging information is to use Tracing. We can enable Tracing at the Page Level and Application Level. We can view the viewstate information and the events chain when a page is executing, among other things.

Let's see, how to enable Tracing at the Page level.

 1) Start a new ASP.NET Web site with visual studio 2010 or an earlier version

 2) Go to the default.aspx page in the  "Source View" and in the Page directive add this


 The whole directive should like this


<%@ Page Title="Home Page" Trace="true" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

3) Run your application and see the tracing information in the Default.aspx page

 Have a look at the picture below to see the various settings for enabling tracing at the page level. You can also set TraceMode to SortedByTime or SortByCategory.

4) You can also use the Trace object in your code. Have a look at the code below


protected void Page_Load(object sender, EventArgs e)
        Trace.Warn("this is my message");
        Trace.Write("this is another message");

Run your application and see the messages printed in the screen. The Trace.Warn method outputs the message on the screen in red.

If you want to enable tracing at the application level, type the following in the web.config page


 <trace enabled="true"/>


Then run again your application. After the root of the application , in my case (http://localhost:13983/trace), type trace.axd

The whole URL should look like this.


In that page you can view trace details for all pages of the application.

Bear in mind that page level tracing settings overrides the tracing options at the application level.

1 Comment

Comments have been disabled for this content.