C#Dot Net Perls

ASP.NET
Trace.Write and Tracing Use

by Sam Allen

Problem

Determine what is actually running in our ASP.NET applications. Expertly use Trace.Write and tracing in general to accomplish this. Review the basics of tracing in ASP.NET, and use C# to easily benchmark and examine our site. Gain more insight into our code's behavior.

Solution

First we must add the trace markup to our Web.config file. The sample values in the markup I show can and should be changed by you. In fact, I encourage you to flip the attribute values and see what they do as much as possible. (This might give you a good feel on what they do.) Here's how to add the markup.

  1. 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.
  2. 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.
<!-- Add this tag, which is disabled for now. -->

<trace pageOutput="false" requestLimit="10" enabled="false" localOnly="true"
    traceMode="SortByTime" mostRecent="true"/>
Part of trace tag What it means
pageOutput False tells ASP.NET not to embed a large table in each page.
You can 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 in Web.config

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.

<!-- Change pageOutput and enabled attributes. -->

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

Verify tracing is on

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. (I am not sure what.) Next, let's 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. Here we examine the trace message columns and their meanings next.

Trace screenshot.
Message in trace output Meaning
From First (s) The number of seconds (usually a small decimal number) since the very first trace message was reached.
From Last (s) The number of seconds since the immediate previous trace message was reached.

What should I ignore?

Usually, 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 control usually and which are basically irrelevant. (I haven't a clue what Begin EndRenderComplete could be used for.) Most measurements won't be useful until you instrument your own, custom code.

Use tracing in C#

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.

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");
}

What do the message mean?

They give statements of what occurs and timings. The following table shows what you could see on your tracing printout. It is for illustration only. Look at the "From Last (s)" column, and the "Function call end" row. The table will say the time elapsed for our custom method (SuperExpensiveFunction) to execute is 0.04 seconds.

Message From First (s) From Last (s)
Function call start 0.01 0.00
Function call end 0.05 0.04

Disable tracing on your server

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.

What is trace.axd?

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. trace.axd allows you to see traces on a separate page, which is always named trace.axd.

Where is trace.axd?

In the root of your web application. (The root is also where Default.aspx is located.) Now, you can access this page at any time by typing its name into the URL bar of your web browser. But next let's look at an easier way to access trace.axd.

Link to trace.axd

This part of my article 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.

[Put this code in your code-behind file.]

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

[And also put markup in your aspx file that uses the above property.]

<% Response.Write(TraceLink); %>

Conclusion

Good code doesn't do things unnecessarily. It doesn't compute the position of the planets when it is trying to find the speed of a train. 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.

Dot Net Perls is dedicated to sharing code and knowledge. It has
© 2007-2008 Sam Allen. All rights reserved.

Ads by The Lounge