Tuesday, January 6, 2009

Tracing in .Net

Tracing

We use tracing to pinpoint the problems in the code.Tracing allows to get whole host information about the currently executing request and write it in the trace of the page or in the Trace log.

Tracing can be achieved in two different methods.

-Page Level Tracing
-Application Level tracing

We can also dynamically turn Page level tracing on.
To enable page level tracing for the page we need to set the trace attribute to true in the
@page directive and then use trace.write () or Trace.warn() statements to write trace information to trace output.

Trace Context : Trace context is the class where .net stores information about all http requests and trace information. We can access Trace context class by using Page.trace attribute of the page .

Trace context class has 2 methods to write trace output to the page .They are

Trace.Write( )
Trace.Warn( )


The only difference between them is for Trace.Warn the output is ddisplayed in red in color such that it can be easily notified in the Trace log.

Both methods we have 3 versions .

1. If we pass single argument .Net writes it to Message column of the log file
2. If we use two string arguments, the first string appears in the Category column and the second in the Message column
3. If you use a third argument, it must be of type Exception and contain information about an error, which ASP.NET then writes to the trace log.

Trace.Write(“Page Loaded”)



The Trace Information section of 4 sections.

Category :
A custom trace category that you
specified as the first argument in a Trace.Write (or Trace.Warn) method call. Message :

A custom trace message that you specified as the second argument in a Trace.Write (or Trace.Warn) method call.
From First (s) :
The time, in seconds, since the request processing was started (a running total).
From Last (s) :
The time, in seconds,
since the last message was displayed. This column is especially helpful for seeing how long individual operations are taking.


Application Level Tracing:
If we don’t want to change paging in each and every page and we need to write the trace to entire application we opt for application level tracing.
To accomplish this we need to enable application-level tracing in the web.config file of the application and view the AXD application trace log for trace information.

Enable Application level tracing.
-To enable application tracing ,locate the web.config file in the application
-Add the trace element to System.web section of Web.config file and set the enabled attribute to true

‘<>

‘ <>


web.config:





<configuration>

<system.web>

<httpHandlers>

<remove verb="*" path="trace.axd" />

</httpHandlers>

</system.web>

</configuration>









-View the application trace log by browsing to the trace.axd page from the application root, like this:
http://localhost//trace.axd
trace.axd is not an page but rather it is a special URL that is intercepted by ASP.NET.
It is an http handler.

Some of the trace Elements are
RequestLimit: Defualt number of Http requests stored in the trace information are 10, if limit is reached tracing will be disabled,requests count can be set using this attribute.
pageOutput :If, in addition to viewing the trace.axd file,
you also want to see trace information displayed at the bottom of the page that it is associated with, add pageOutput="true" to the element:

The trace information you will see is identical to what would appear had you placed Trace="true" in the @ Page directive for the page
localOnly :To show trace information
to the local user (i.e., the browser making the request is on the machine serving the request) but not to remote users, make sure the element includes localOnly="true":


If we want to identify problems only
when an exception occurs.we can enable trace dynamically in the catch block.
· Set Page.Trace.IsEnabled = true in the Catch block of your exception handler.
· Write to the trace log by using a Trace.Write of the form Trace.Write("Exception", "Message", exc).












No comments:

Post a Comment