ASP.NET Tip: Adding Tracing to an Application

For some applications, such as console or Windows Forms applications, it's fairly easy to step through them to debug an issue. However, tracing down errors in web applications and services can be difficult, especially if they happen only in production environments where debugging isn't possible. In these cases, it's helpful to be able to add trace statements to your code that appear only when tracing is enabled on the web page or the web site.

Step 1 is to enable tracing, either at a page or site level. To enable tracing for a single page, add the following to the @Page directive in your ASPX file:

Trace="true"

By default, this will dump the tracing information at the bottom of the web page. In most debugging/tracing situations, this is sufficient. However, if you're working on tracing in an entire site, you can make the change in the Web.config file instead of in each web page. To do this, open up the Web.config and add the following line:

<trace enabled="true" localOnly="false" pageOutput="true" />

The enabled attribute turns tracing on and off. The localOnly attribute controls whether the trace output shows up on machines other than the web server. In my own development environment, I use VMWare Workstation to host a Windows 2003 Server instance. So, I have to turn this attribute to false to see the output when I bring up a browser outside the virtual machine. If you are running IIS locally on your development machine, you won't need to set this attribute. The final attribute indicates that the output should be dumped at the bottom of the page. You also can opt to put the tracing in a separate file, but I've found that having the information right on the page is far easier to work with.

If you add this to a normal web page, you'll get all of this information:

  • Overall information about the request
  • A list of events and the time each event took to complete
  • A tree of all the controls on the page, showing each control's name and the amount of memory each one took in the ViewState, rendering, and control state
  • A list of session variables and their current values
  • A list of application variables and their current values
  • The cookies from the request and the response
  • Request and response headers
  • Contents of the Form, QueryString, and ServerVariables collections

Having all this information can make it much easier to debug issues, even before you add your custom messages.

To add custom messages to the trace output, you can use the Trace object property of the Page class to send messages into the list of events that is dumped to the web page. Here's an example:

Trace.Write("Current value of variable: " & variableValue.ToString());

This will print the line of text at the appropriate time during all the other events that are documented in the trace output. Because tracing can be turned on and off through a configuration file, this is an easy way to leave in debugging commands that you may need in the future. However, because it can be easily enabled and disabled, be careful about how much personal information, such as identification numbers and passwords, you dump into the trace output.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic. Send him your questions and feedback via e-mail at questions@techniquescentral.com.



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds