Getting Started
  Introduction
  What is ASP.NET?
  Language Support

ASP.NET Web Forms
  Introducing Web Forms
  Working with Server Controls
  Applying Styles to Controls
  Server Control Form Validation
  Web Forms User Controls
  Data Binding Server Controls
  Server-Side Data Access
  Data Access and Customization
  Working with Business Objects
  Authoring Custom Controls
  Web Forms Controls Reference
  Web Forms Syntax Reference

XML Web services
   created using ASP.NET

  Introducing XML Web services
  Writing a Simple XML Web service
  XML Web service Type Marshalling
  Using Data in XML Web services
  Using Objects and Intrinsics
  The XML Web service Behavior
  HTML Pattern Matching

ASP.NET Web Applications
  Application Overview
  Using the Global.asax File
  Managing Application State
  HttpHandlers and Factories

Cache Services
  Caching Overview
  Page Output Caching
  Page Fragment Caching
  Page Data Caching

Configuration
  Configuration Overview
  Configuration File Format
  Retrieving Configuration

Deployment
  Deploying Applications
  Using the Process Model
  Handling Errors

Security
  Security Overview
  Authentication & Authorization
  Windows-based Authentication
  Forms-based Authentication
  Authorizing Users and Roles
  User Account Impersonation
  Security and WebServices

Localization
  Internationalization Overview
  Setting Culture and Encoding
  Localizing ASP.NET Applications
  Working with Resource Files

Tracing
  Tracing Overview
  Trace Logging to Page Output
  Application-level Trace Logging

Debugging
  The SDK Debugger

Performance
  Performance Overview
  Performance Tuning Tips
  Measuring Performance

ASP to ASP.NET Migration
  Migration Overview
  Syntax and Semantics
  Language Compatibility
  COM Interoperability
  MTS Transactions

Sample Applications
  A Personalized Portal
  An E-Commerce Storefront
  A Class Browser Application
  IBuySpy.com

  Get URL for this page

Trace Logging to Page Output

Page-level tracing enables you to write debugging statements directly to a page's output, and conditionally run debugging code when tracing is enabled. To enable tracing for a page, include the following directive at the top of the page code:

<%@ Page Trace="true"%>
Trace statements can also be organized by category, using the TraceMode attribute of the Page directive. If no TraceMode attribute is defined, the default value is SortByTime.
<%@ Page Trace="true" TraceMode="SortByCategory" %>

The following example shows the default output when page-level tracing is enabled. Note that ASP.NET inserts timing information for important places in the page's execution lifecycle:

 
VB Trace1.aspx

[Run Sample] | [View Source]

The page exposes a Trace property (of type TraceContext), which can be used to output debugging statements to the page output, provided tracing is enabled. Using TraceContext, you can write debugging statements using the Trace.Write and Trace.Warn methods, which each take a message string or a category and message string. Trace.Warn statements are identical to Trace.Write statements, except they are output in red.


' Trace(Message)
Trace.Write("Begging User Code...")
...
Trace.Warn("Array count is Nothing!")
' Trace(Category, Message)
Trace.Write("Custom Trace","Beginning User Code...")
...
Trace.Warn("Custom Trace","Array count is null!")
VB

When tracing is disabled (that is, when Trace="false" on the Page directive, or is not present), these statements do not run and no Trace output appears in the client browser. This makes it possible to keep debugging statements in production code and enable them conditionally at a later time.

Often you might need to run additional code to construct the statements to pass to the Trace.Write or Trace.Warn methods, where this code should only run if tracing is enabled for the page. To support this, Page exposes a Boolean property, Trace.IsEnabled, which returns true only if tracing is enabled for the page. You should check this property first to guarantee that your debugging code can only run when tracing is on.


If Trace.IsEnabled Then
    For i=0 To ds.Tables("Categories").Rows.Count-1
        Trace.Write("ProductCategory",ds.Tables("Categories").Rows(i)(0).ToString())
    Next
End if
VB

The following example shows the use of Trace.Write and Trace.Warn to output debugging statements. Also note the use of the Trace.IsEnabled property to conditionally run extra debugging code. In this example, the trace information has been sorted by category.

 
VB Trace2.aspx

[Run Sample] | [View Source]

ASP.NET also provides a way to enable tracing for the entire application, not just a single page. For more about application-level tracing, click here.

Section Summary

  1. Page-level tracing is enabled using a Trace="true" attribute on the top-level Page directive.
  2. Page-level tracing enables you to write debugging statements as part of a page's client output. Trace statements are output using the Trace.Write and Trace.Warn methods, passing a category and message for each statement.
  3. Debugging code can be conditionally run, depending on whether tracing is enabled for the page. Use the Trace.IsEnabled property of the page to determine whether tracing is enabled.


Copyright 2001-2002 Microsoft Corporation. All rights reserved.