Visual Studio debugging tools and ASP.NET detailed error pages are very helpful
when we have testing a web application. But, sometimes we need a way to identify
problems after we have deployed the application, while we don't have Visual
Studio IDE on that Computer.
We can identify these errors by recording diagnostic information in an event
log, but this assumes that someone will actually review the log regularly.
Moreover, we could display some information directly in the web page. The
problem with this strategy is that we need to remove (or comment out) all this
extra code before we deploy the web application. Otherwise, the website users
could see some strange debugging messages when they don't expect it.
Fortunately, there is an easier way to solve this problem. The ASP.NET provides
a feature called tracing that gives a convenient and flexible way to report
diagnostic information's.
Enabling Tracing:
To use tracing, we need to explicitly enable it. There are several ways to
enable tracing. One of the easiest ways is by adding an attribute to the Page
directive in the .aspx file:
<%@ Page Trace="true" ... %>
We can also enable tracing using the built-in Trace object. Here's an example of
how to turn on tracing in the Page. Load event handler:
using System.Web;
protected void Page_Load(Object sender, EventArgs e)
{
Trace.IsEnabled = true; // (Trace is an instance of the System.Web.TraceContext
class)
}
By default, once the tracing is enabled, it will apply only to local requests.
This limitation prevents end users from seeing the tracing information. If we
need to trace a web page from a remote location, we have to enable remote
tracing by changing some settings in the web.config file.
Tracing Information:
ASP.NET tracing automatically provides a lengthy set of standard, formatted
information. The following example shows what this information looks like. To
build this example, we can start with basic ASP.NET page as shown here,
This page displaying only a single line of text and a Button. If we click the
button, tracing is enabled by setting the Trace.IsEnabled property to true as
shown in the Code Snippet above. When the page is rendered, it will include a
standard and formatted diagnostic information's, as shown below.
Tracing information is provided in several different categories, as Request
Details, Trace Information, Control Tree, Session State and Application State,
Request Cookies and Response Cookies, Headers Collection, Form Collection, Query
String Collection and Server Variables.
Application-Level Tracing:
Application-level tracing allow to enable tracing for an entire application and
the tracing information won't be displayed in the page. Instead, it will be
collected and stored in memory for a short amount of time. We can review the
recently traced information by requesting a special URL.
To enable application-level tracing, we need to modify settings in the
web.config file, as
<configuration>
<system.web>
<trace enabled="true" requestLimit="10" pageOutput="false" />
</system.web>
</configuration>
Tracing Options:
Attribute |
Values |
Description |
Enabled |
True, False |
Turns application-level tracing on or off. |
requestLimit |
Any integer |
Stores tracing information for a
maximum number of HTTP requests. |
pageOutput |
true, false |
Determines whether tracing information
will be displayed on the page. |
traceMode |
SortByTime, SortByCategory |
Determines the sort order
of trace messages. The default is SortByTime. |
localOnly |
true, false |
Determines whether tracing information
will be shown only to
local clients (clients using the same computer) or can be shown to
remote clients as well. By default, this is true and remote clients
cannot see tracing information. |
mostRecent |
true, false |
Keeps only the most recent trace
messages if true. When the requestLimit maximum is reached, the
information for the oldest request is abandoned every time a new request
is received. If false (the default), ASP.NET stops collecting new trace
messages when the limit is reached. |
To view tracing information, we have to request
the trace.axd file in the web application's root directory. This file is
actually doesn't exist, the ASP.NET automatically interprets it as a request for
the tracing information. It will then list the most recent collected requests,
as Shown below.
Thank You.