Tracing

Tracing is an easy way to find out what is going on in your program. Back in the days of classic ASP, the only way to trace what was happening in your code was to insert Response.Write statements in strategic places. This allowed you to see that you had reached a known point in the code, and perhaps to display the value of some variables. The big problem with this hand-tracing technique, aside from the amount of work involved, was that you had to laboriously remove or comment out all those statements before the program went into production.

ASP.NET provides better ways of gathering the trace information. You can add tracing at the application level or at the page level. With application-level tracing, every page is traced, while with page-level tracing, you choose the pages to which to add tracing.

Page-Level Tracing

To add page-level tracing, modify the Page directive at the top of the .aspx page, by adding a Trace attribute and setting its value to true, as follows:

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
     Inherits="DebuggingApp.WebForm1" Trace="true" %>

When you view this page, there will now be tables at the bottom that contain a wealth of information about your web application. Select a book from the drop-down list and you will see something like Figure 7-2.

Trace results

Figure 7-2. Trace results

The top section, labeled Request Details, shows basic ...

Get Programming ASP.NET, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.