Page Tracing
Enabling Tracing
<%@ Page Trace="true" ... %>
or
protected void Page_Load(Object sender, EventArgs e)
{
Trace.IsEnabled = true;
}
{
Trace.IsEnabled = true;
}
By default, trace messages are listed in the order they were written by your code. Alternatively, you
can specify that messages should be sorted by category using the TraceMode attribute in the
Page directive:
<%@ Page Trace="true" TraceMode="SortByCategory" %>
or the TraceMode property of the Trace object in your code:
Trace.TraceMode = TraceMode.SortByCategory;
Writing Trace Information
protected void cmdWrite_Click(Object sender, EventArgs e)
{
Trace.Write("About to place an item in session state.");
Session["Test"] = "Contents";
Trace.Write("Placed item in session state.");
}
{
Trace.Write("About to place an item in session state.");
Session["Test"] = "Contents";
Trace.Write("Placed item in session state.");
}
or uses overloaded method:
protected void cmdWriteCategory_Click(Object sender, EventArgs e)
{
Trace.Write("cmdWriteCategory_Click",
"About to place an item in session state.");
Session["Test"] = "Contents";
Trace.Write("cmdWriteCategory_Click",
"Placed item in session state.");
}
{
Trace.Write("cmdWriteCategory_Click",
"About to place an item in session state.");
Session["Test"] = "Contents";
Trace.Write("cmdWriteCategory_Click",
"Placed item in session state.");
}
Trace an exception
protected void cmdError_Click(Object sender, EventArgs e)
{
try
{
DivideNumbers(5, 0);
}
catch (Exception err)
{
Trace.Warn("cmdError_Click", "Caught Error", err);
}
}
private decimal DivideNumbers(decimal number, decimal divisor)
{
return number/divisor;
}
{
try
{
DivideNumbers(5, 0);
}
catch (Exception err)
{
Trace.Warn("cmdError_Click", "Caught Error", err);
}
}
private decimal DivideNumbers(decimal number, decimal divisor)
{
return number/divisor;
}
Application-Level Tracing
To enable application-level tracing, you need to modify settings in the web.config file, as
shown here:
<configuration>
<system.web>
<trace enabled="true" requestLimit="10" pageOutput="false" />
...
</system.web>
</configuration>
<system.web>
<trace enabled="true" requestLimit="10" pageOutput="false" />
...
</system.web>
</configuration>
To view tracing information, you request the trace.axd file in the web application’s root directory. This file doesn’t actually exist; instead, ASP.NET automa tically intercepts the request and interprets it as a request for the tracing information. It will then list the most recent co llected requests, provided you’re making the request from the local machine or have enabled remote tracing