Tracing in ASP.NET
page 1 of 4
Published: 30 Apr 2004
Unedited - Community Contributed
Abstract
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.
by Phil Winstanley
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 35400/ 38

Tracing in ASP.NET

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.


View Entire Article

User Comments

Title: ateeq   
Name: helo
Date: 2012-12-18 4:43:34 AM
Comment:
flksd fsdjlk fjds djs sda s sadlj falk asldfj as
Title: Programmer   
Name: Terence
Date: 2004-07-16 12:42:52 PM
Comment:
It looks like the last post cut some of the code off... but you get the idea. Terence.
Title: Programmer   
Name: Terence
Date: 2004-07-16 12:40:45 PM
Comment:
Nice job Phil. I tranlated your C# code to vb.net for those who are interested in that. Cheers. Terence
'Translated from C# to VB.net.
'July 16, 2004
'Terence J Wehle
'We use shared Methods in this translation so that creating an instance
'of the object is not necessary. This is appropriate since
'each methods is acting only on passed in parameters.

'In this article, Phil Winstanley proves that contrary to the
'NET SDK documentation that “Trace statements are processed and displayed
'only when tracing is enabled.”, the statements are in fact processed,
'although not displayed. If you have many such statements, particularly
'in a loop, this could slow things down for the page even thought the
'Trace is set to "False". This solution checks first whether tracing
'is enable before making the call and thus save resources. It is a
'wrapper for the trace calls.
'---------------------
Imports System
Namespace TJW


Public Class Trace

Public Sub Write(ByVal Message As String)

If (System.Web.HttpContext.Current.Trace.IsEnabled) Then

Try
System.Web.HttpContext.Current.Trace.Write(Message.ToString())
Catch e As Exception
System.Web.HttpContext.Current.Trace.Warn("Trace", "Write", e)
End Try

End If

End Sub

Shared Sub Write(ByVal Category As String, ByVal Message As String)

If (System.Web.HttpContext.Current.Trace.IsEnabled) Then

Try
Throw New Exception("This is a bogus error")
System.Web.HttpContext.Current.Trace.Write(Category.ToString(), Message.ToString())
Catch e As Exception
System.Web.HttpContext.Current.Trace.Warn("Trace", "Write", e)
End Try

End If

End Sub

Public Sub Write(ByVal Category As String, ByVal Message As Stri






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 9:50:55 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search