ASP.NET Trace

Trace output screenshot: ASP.NET

Trace.Write helps diagnose problems in ASP.NET. It determines what is actually running in an ASP.NET application. We review the basics of tracing in ASP.NET. We use the C# language to easily benchmark sites.

Start

First we must add the trace markup to our Web.config file. The sample values in the markup I show can be changed. I encourage you to flip the attribute values and see what they do as much as possible. Here's how to add the markup.

Steps

Open Web.config file. Your ASP.NET project should have a Web.config file. If it doesn't, add one through the Website > Add New Item menu. This is an XML file used by ASP.NET to store website configuration settings.

Locate <system.web> tag section. We must add special markup to the inside of the system.web section. In that block, add a nested tag that looks like the code that follows. Visual Studio's IntelliSense will offer some suggestions when you type.

Visual Studio
Web.config markup that disables tracing
    (Add this tag, which is disabled.)

<trace pageOutput="false" requestLimit="10" enabled="false" localOnly="true"
    traceMode="SortByTime" mostRecent="true"/>
Property

Important properties. The above markup has several attributes and values. This next section shows what some of these properties mean, and why they are necessary. The property name is on the left.

PageOutput:False tells ASP.NET not to embed a large table in each page. Change this if you want inline trace messages at the bottom of every aspx page.

RequestLimit:10 here indicates that ASP.NET should store at most 10 trace messages.

Enabled:False here says that tracing should not run at all. You must change this to "true" if you want to enable tracing.

LocalOnly:True means we should only trace on a local computer.

Enable tracing

Obviously, if you want to trace you must set enabled to true. Look at the trace tag you have added to Web.config, and look at the pageOutput attribute. To quickly enable tracing, change pageOutput to true and enabled to true.

Web.config markup to enable tracing
    (Change pageOutput and enabled attributes.)

<trace pageOutput="true" requestLimit="10" enabled="true" localOnly="true"
    traceMode="SortByTime" mostRecent="true"/>

Verify tracing. Now, load your page in Internet Explorer or Firefox and scroll to the bottom. If there is a big ugly gray table, then it is working. If the table isn't there, you need to try something else. Next, we look at the traces in more detail.

Interpret tracing messages. We must think about and interpret the tracing messages. I will show some custom trace messages and then a table displaying what some of them mean. Look at the Message column, and you will some custom trace messages, and then to the right you will see timings.

From First:This is the number of seconds since the first trace message was reached.

From Last:The number of seconds since the immediate previous trace message was reached.

Programming tip

Ignore messages. Usually, you can ignore most of the event traces that don't cover a part of your project that you have customized. ASP.NET does all kinds of internal operations, which you can't usually control and which are basically irrelevant. Most measurements won't be useful until you instrument your own, custom code.

Disable tracing:Tracing takes time and you don't have time on your production server. Because of this, before you put your code on the production server, make sure to disable tracing, as it is a performance drag. You can disable tracing in Web.config.

Example code

Here we will look at Trace.Write and how to use it. We must call Trace.Write in our code-behind or script block in our aspx pages. Let's use tracing to find the time required for a function call. What follows is an example Page_Load event with tracing.

Code that uses Trace.Write: C#

protected void Page_Load(object sender, EventArgs e)
{
    // This goes in the code-behind file. We call Trace.Write twice.
    // The first time will tell us when we first call the function.
    Trace.Write("Function call start");

    // Run some custom C# code.
    bool result = SuperExpensiveFunction();

    // Wow, we are done. This trace message will show us how long that
    // dumb function took.
    Trace.Write("Function call end");
}

Trace.axd

Question and answer

This is a separate file to store tracing messages. If you have pageOutput set to true, your webpage will acquire a large table at the bottom. That will list lots of information—the trace information. The trace.axd file shows traces in a separate page.

Where is it? In the root of your web application. You can access this page at any time by typing its name into the URL bar of your web browser.

Link to trace.axd. This is entirely optional, and I am only showing it because it is neat and can make development more convenient. Here's a way to make a convenient link to trace.axd, which makes visiting the trace message easier. It can also alert you if tracing is enabled.

Response.Write
Code that inserts link to Trace.axd: C#

public string TraceLink
{
    get
    {
	if (Trace.IsEnabled)
	    return "<a href=\"trace.axd\">Trace</a><br/>";
	else
	    return "";
    }
}

Markup that uses property: ASPX

<% Response.Write(TraceLink); %>

Summary

The C# programming language

We looked at the tracing mechanism in ASP.NET and the C# language. Good code doesn't do things that are not required. It doesn't compute the position of the planets when it is trying to find the speed of a train.

Opinion:The great thing about tracing is that it allows you to benchmark your pages, but also allows you to know exactly what code is running on your pages.


C#: ASP.NET