This small article will focus on ASP.NET’s built in Tracing functionality, this offers the developer a place to put all the debug information from their program without it mucking up your expertly designed UI (well I can dream!).
We will discuss the benefits of using Trace, as well as the costs it imposes on you as the developer – especially in a team based environment. There is also a special section of this article devoted to the classic ASP developers that know and love Response.Write, hopefully I can persuade you not to use it any more J
The .NET Documentation describes Tracing as so: -
ASP.NET introduces new functionality that allows you to write debug statements, directly in your code, without having to remove them from your application when it is deployed to production servers. Called tracing, this feature allows you to write variables or structures in a page, assert whether a condition is met, or simply trace through the execution path of your page or application.
In order for these messages and other tracing information to be gathered and displayed, you must enable tracing for the page or application. When you enable tracing, two things occur:
- ASP.NET appends a series of diagnostic information tables immediately following the page's output. The information is also sent to a trace viewer application (only if you have enabled tracing for the application).
- ASP.NET displays your custom diagnostic messages in the Trace Information table of the appended performance data.
Diagnostic information and tracing messages that you specify are appended to the output of the page that is sent to the requesting browser. Optionally, you can view this information from a separate trace viewer (trace.axd) that displays trace information for every page in a given application. This information can help you to clarify errors or undesired results as ASP.NET processes a page request.
Trace statements are processed and displayed only when tracing is enabled. You can control whether tracing is displayed to a page, to the trace viewer, or both.
Turning it on
What I shall show you first is simply how to turn tracing on for one of your existing ASP.NET pages.
First find the Page directive on your ASP.NET page (usually the first line in the page): -
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="intranet._default" %>
Now we need to turn on Tracing for your page, we do this by adding the Trace property to the directive and setting it to true, like this: -
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="intranet._default" Trace=”True” %>
What’s in it?
Request Details
Trace Information
Control Tree
Session Collection
Cookies Collection
Forms Collection
Headers Collection
Server Variables
How does it work with my page HTML?
As soon as ASP.NET detects the </html> tag is in the response buffer it adds the Trace information to the end of the page, this is all encapsulated in a <div> with an id of __asptrace.
This can be very difficult to read if you use absolute positioning as the Trace information will lie behind all your other HTML content.