
How and Why to Use Trace Option for Debugging in ASP.NET ApplicationWhen you are in testing phase of your application, trace option will be of much help to you. Are you using Response.Write( ) statements in your code for debugging? Then this article is of more relevance to you.
If you use
Response.Write( ) statements during testing phase, you will have to ensure
that all such statements are commented out at the time of production.
If not, it will be displayed to Users. Instead, there in an easy and much
more efficient approach to be followed. The approach is all about using
Tracing in your ASP.NET Application. Tracing
can be performed in .NET in two different ways: Using
System.Web.TraceContext class This article
will focus upon the first option and discuss about usage of trace option
with the help of System.Web.TraceContext class. The second option is specific
to VS.NET. All trace messages recorded using second option can be viewed
in output window of VS.NET. How do
you achieve tracing Using System.Web.TraceContext Class? You can achieve
tracing either at Page Level or at Application Level. Page Level
Tracing: You can enable
tracing for a particular page by including the following page directive: <%@ Page
Trace="true"%> This directive
can also include one more attribute called TraceMode which can accept
values SortByTime or SortByCategory. If the TraceMode is mentioned as
SortByTime, then the trace information will be sorted and displayed based
on time. While including trace messages, you can specify the category.
And if you mention the TraceMode as SortByCategory, then the trace information
is sorted based on category. For example,
if there are multiple developers working on the same page in different
tasks then each task can be named as a category and when you can write
trace messages by specifying category as well. Now when the trace information
is displayed, you can easily trap trace messages you have included with
the help of category since all messages specified in a particular category
will be displayed one after the other. Here is the syntax to include the
TraceMode attribute: <%@ Page
Trace="true" TraceMode=SortByCategory%> Or <%@ Page
Trace="true" TraceMode=SortByTime%> If TraceMode
is not mentioned, then again it is assigned with SortByTime by default. Tracing is
now enabled in your page. How will you write trace messages? Here is an
example illustrating usage of tracing: <%@ Page
Trace="true" TraceMode=SortByCategory%> In this example,
Trace.Write statements are used to display trace messages in the browser.
Trace.Warn is also similar to Trace.Write, but only difference is that
the message provided in Trace.Warn will be shown in red color. If you
want to execute certain block of statements if and only if tracing is
enabled, then you can use Trace.IsEnabled which will return true if tracing
is enabled else it returns false. In the above mentioned examples, Trace.Write
and Trace.Warn includes two attributes. First attribute
is the category name and the second attribute is the message to be displayed.
When the trace information is displayed, it will be sorted in the order
of category since we have mentioned the TraceMode as SortByCategory in
page directive. Hence all Category1 trace messages will appear one after
the other and so does the trace messages for Category2. This reduces searching
effort and makes your debugging easier. Category attribute is optional.
Trace.Write and Trace.Warn can include only one attribute containing the
message. If you dont
want to use tracing, you need not comment all Trace.Write and Trace.Warn
statements, all that you have to do is remove the following line from
your code: <%@ Page
Trace="true" %> This is because
tracing is disabled by default. An alternative way is to set the Trace
attribute to false in the page directive: <%@ Page
Trace="false" %> Application
Level Tracing: If you want
tracing to be enabled for the whole application, then add the following
entry in web.config file: <configuration> This trace
section mentioned above has many more properties associated with it. Heres
an example including all other properties: <configuration> Significance
of each of the above mentioned property is given below: enabled:
Tracing is enabled for the entire application if this property is set
to true. Default value is false. Once the
appropriate setting is done in web.config file, you can view all trace
information that you have set at page level. In addition, you can view
consolidated information of all requests (till the requestLimit specified).
Consolidated
information includes Request Detail, Trace Information, Control Hierarchy,
Session State, Application State and much more. You can view all these
information in trace.axd. Assume that you are running your application
from localhost and your application name is testTraceApp, then you can
access trace.axd using the link below: http://localhost/testTraceApp/trace.axd
trace.axd
is an HTTP Handler supported by ASP.NET and it is not a file.
_______________________________________________________________________
FREE
Subscription
Subscribe
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ |