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. 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 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"
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.
RequestLimit:10 here indicates that ASP.NET should store at most 10 trace messages. This is a limit.
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. This helps with debugging.
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"
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. Interpret tracing messages. I 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. This can help performance analysis.
From Last:The number of seconds since the immediate previous trace message was reached. This helps measurements.
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 that you can't usually control.
Disable tracing:Tracing takes time and you don't have time on your production server. You can disable tracing in Web.config.
Example code. 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();
// We are done.
// This trace message will show us how long that function took.
Trace.Write("Function call end");
Trace.axd. 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. The trace.axd file shows traces in a separate page.
Location:The file is in the root of your web application. You can access this page by typing its name into your web browser.
Link to trace.axd. This is entirely optional. But it 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
return "<a href=\"trace.axd\">Trace</a><br/>";
Markup that uses property: ASPX
<% Response.Write(TraceLink); %>Summary. These tips examined the tracing mechanism in ASP.NET. 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.