Debugging and Tracing                                                                        

It is extremely important for today’s Web developers to be able to debug their Web applications. This
includes being able to set bookmarks/breakpoints, watch variables and step through the client-side and server-
side code. Visual Studio does a fine job of supporting these features and much more! Gone are the days of
using Response.Write( ) to spit out current values at the bottom of the Web page, as we did with classic ASP.
For developers being newly introduced to a large Web application, debugging the application can be one of the
fastest ways to learn how it works. It’s preferred that Web developers work locally on their own machine
either with IIS or the Visual Studio Development Web Server.

In order for a Web application to be debugged remotely, The user must have administrative privileges on the
Web server. The Web server must be in the same domain or workgroup as the user attempting to debug the
site. The Web Server must have Visual Studio installed on it. The Web.config file has to be configured to
allow remote debugging and remote access to detailed error messages. Because of these hassles, most Web
developers today perform all development locally on their own workstation.

To configure debugging on a Web application, set the debug=”true” in the Web.config file.


















Thankfully, Visual Studio will do this for you, assuming you accept this prompt:




















Note: By setting the debug mode to “true”, performance will be decreased greatly on the Web server. Turn it
off when you no longer need it. Never enable debugging on a production Web server - for performance and
security reasons.

ASP.NET Tracing offers a great way to capture ever-changing information about a given Web site. With
tracing, you can. View the state of any variable throughout the entire Request/Response process. View ASP.
NET information such as Session ID, the control tree and all the ASP.NET server variables.Turn tracing on or
off with a setting in any of the Web.config files, as a Page/Control/Master directive or programmatically in
your .NET code. Trace information is either written directly below the contents of the Web page or by using
the Trace viewer, trace.axd.

To enable tracing for a Web page, just set the Page directive at the top:















Check out the results. Here’s how the page normally appears with tracing turned off:























Here it is again, but with tracing turned on:




























As you can see, trace information is displayed at the bottom of the page. Notice how the request returned a
HTTP status code of 200 (success!) and the order of the events being raised.
     
Alternatively, you may want to turn on tracing for all your Web pages in the Web.config file:
















As you can tell, there are a lot of different settings for turning tracing at the Web.config level. Instead of
manually typing these in and guessing on their purpose, consider using WAT or IIS to adjust these settings
more accurately.

This screenshot of WAT Trace configuration sums up what the different settings mean:































The Trace information can be cached (as set above) and can be viewed using the Trace viewer, trace.axd. Just
type in the full URL for the Web application and tack on “trace.axd”.

As you can see here, you can view the details of any particular request or clear the cache completely.































Trace can also be enabled in your .NET code by setting the Trace.IsEnabled property to “true”. You can also
check this property in an IF statement to determine if a block of Trace statements should be considered for
execution.

For example:


protected void Page_Load(object sender, EventArgs e)
{
if (Trace.IsEnabled)
{
  Trace.Write("Page_Load", "My important message.");
  Trace.Warn("Page_Load", "I can put variables here, too!");
  Trace.Write("My Custom Category", "I can sort by category.");
}

CSClass1.DoCSStuff();
VBClass1.DoVBStuff();
}


The results of using Trace.Write and Trace.Warn are clear below.




























Since the settings for Trace can be set at the site, page and code levels, it’s important to know the order of
precedence:
Debugging
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
In Code
Page Directive
Web.config
Trace Enabled?
Not Set
Not Set
Not Set
False
Not Set
Not Set
Trace = “true”
True
Not Set
Trace = “true”
Trace =  “true” or “false”
True
Trace.IsEnabled =
“false”
Trace = “true” or “false”
Trace =  “true” or “false”
False
Trace.IsEnabled = “true”
Trace = “true” or “false”
Trace = “true” or “false”
True
Services