Library code snippets

Debug ASP.NET pages using Tracing

In Classic ASP one might debug some code using VB Script:

Response.Write strSQL
Response.End()

and the comment/uncomment the code accordingly. This was a bit of a pain during the development process. In ASP.NET, one can use the Trace feature which simplifies matters somewhat. Page-level tracing can be set in the Page directive on the ASPX page:

<%@ Page Language="c#" Trace="true"%>

and in the code one could use:

string strSQL="select * from " + Table + " " + WhereClause + " order by " + OrderingColumn;
Trace.Write("SQL",strSQL);

When the page is run in the browser, you'll see:

So, you'd be able to see the SQL that is executed. By using Trace.Warn instead of Trace.Write, the line would be highlighted in Red. When the application is deployed, you'd simply disable tracing by setting the Trace attribute to false. If you have many pages that you wish to trace, then it may be more viable to set the tracing in your web.config file:

<system.web>
    <trace enabled="true" requestLimit="20" pageOutput="true" traceMode="SortByCategory" localOnly="true" />
</system.web>

So, you can easily enable/disable tracing as required.

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Debug ASP.NET pages using Tracing.

Leave a comment

Sign in or Join us (it's free).

Julian Roberts I've been a freelance web developer since Feb '00. Prior to that, I'd spent most of my working life in the pub trade. I'd also worked as a security guard in a local factory. Luckily, I was able to ...

Related podcasts

Events coming up

  • Mar 15

    DevWeek 2010

    London, United Kingdom

    DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.

Want to stay in touch with what's going on? Follow us on twitter!