Labels

Monday, June 8, 2009

Tracing

Hi,

 

 

Tracing is the process of recording key events during the execution of an application over a discrete period of time. This information can help you understand the code path taken within the application.

Tracing is done at two levels viz. Page Level and Application Level. But here I’ll be not covering these two in detail. Instead the target of this articles is something more. However below is the brief details on the two.

 

Page-Level Tracing

 

·          ASP.NET tracing can be enabled on a page-by-page basis by adding Trace=”true” to the Page directive in any ASP.NET page:

 

<%@ Page Language=”C#” Inherits=”System.Web.UI.Page” Trace=”true” %>

 

·          Additionally, you can add the TraceMode attribute that sets SortByCategory or the default,SortByTime.

·          You can enable tracing programmatically as well, using the Trace.IsEnabled property.

 

 

Application Tracing

 

·          Alternatively, you can enable tracing for the entire application by adding tracing settings in web.config.

 

<configuration>

<system.web>

<trace enabled=”true” pageOutput=”false” requestLimit=”20” traceMode=”SortByTime” localOnly=”true” />

</system.web>

</configuration>

 

·          The page-level settings take precedence over settings in web.config, so if enabled=”false” is set in Sweb.config but trace=”true” is set on the page, tracing occurs.

 

 

Entry

Description

enabled

 A Boolean value that indicates whether tracing is enabled for the application. The default value is true.

RequestLimit

 The number of requests to list in the trace.axd page. The default value is 10.

PageOutput

 A Boolean value that indicates whether trace information is displayed at the bottom of every page in the application. The default value is false.

TraceMode

 A value that indicates the order in which trace messages are displayed. Possible values are SortByTime and SortByCategory; the default value is SortByTime.

LocalOnly

 A Boolean value that indicates whether trace messages should be displayed only on the local computer, not remote computers. The default value is true.

 

 

 

System.Diagnostics.Trace and ASP.NET’s Page.Trace (System.Web.TraceContext)

 

·          There are multiple things named Trace in the whole of the .NET Framework, so it may appear that tracing isn’t unified between Web and non-Web applications.

·          Don’t be confused because there is a class called System.Diagnostics.Trace and there is also a public property System.Web.UI.Page.Trace. The type of the Trace is System.Web.TraceContext.

 

·          System.Web.UI.Page.Trace –

o         Used in Web Technology.

o         This is basically a ASP.NET-specific tracing mechanism

o         Used to collect all the details and timing of a Web request.

 

·          System.Diagnostics.Trace & System.Diagnostics.Debug

 

o         This is the core .NET Framework tracing library.

o         This tracing framework in the Base Class Library is not Web-specific.

o         This is used to receives consistent and complete tracing information when an ASP.NET application calls a non–Web-aware component.

o          

 

Conditional Preprocessor Directives

 

·          The System.Diagnostics namespace contains the Debug and Trace classes, which provide a straightforward means of outputting tracing information from your application.

·          The primary difference between them is that the Debug class is intended for use during development and the Trace class is intended for use throughout the lifecycle of the application.

 

·          M. Imp - These two classes exhibit similar behavior. In fact, internally they both forward their calls to corresponding static methods exposed by the private TraceInternal class.

 

Below are the actual underlying code for Debug.Write() & Trace.Write()

 

[Conditional(“DEBUG”)]

public static void Write(string message)

{

    TraceInternal.Write(message);

}

 

[Conditional(“TRACE”)]

public static void Write(string message)

{

    TraceInternal.Write(message);

}

 

 

 

·          Each of the static methods exposed by the Debug and Trace classes is decorated with the Conditional attribute.

·          This attribute controls whether a call made to a particular method is executed based on the presence of a particular Preprocessing Symbol.

·          The methods exposed by the Debug class are executed only if the DEBUG symbol is defined. The methods exposed by the Trace class are executed only if the TRACE symbol is defined.

 

 

 

 

TraceListeners

 

·          .Net provides three trace listeners.

 

o         DefaultTraceListener (Outputs to Debug Window)

o         TextWriterTraceListener (Outputs to File)

o         EventLogTraceListener (Outputs to Event Log)

 

·          New in .Net 2.0

 

o         WebPageTraceListener

o         XMLWriterTraceListener  - Derives from TextWriterTraceListener and writes out a strongly typed XML file.

o         DelimitedListTraceListener - Also derives from TextWriterTraceListener; writes out  comma-separated values (CSV) files.

 

·          These listeners can be added programmatically or via a .config file.

 

 

TextWriterTraceListener myTextListener = new TextWriterTraceListener(File.Create(@”c:\myListener.log”));

Trace.Listeners.Add(myTextListener);

 

 

<configuration>

<system.diagnostics>

<trace autoflush=”false” indentsize=”4”>

<listeners>

    <add name=”myListener” type=”System.Diagnostics.TextWriterTraceListener” initializeData=”c:\myListener.log” />

    <remove name=”Default” />

</listeners>

</trace>

</system.diagnostics>

</configuration>

 

 

            EventLogTraceListener –

 

           

<configuration>

<system.diagnostics>

<trace autoflush=”false” indentsize=”4”>

<listeners>

     <add name=”EventLogTraceListener” type=”System.Diagnostics.EventLogTraceListener” initializeData=”Wrox”/>

</listeners>

</trace>

</system.diagnostics>

</configuration>

 

 

·          Writing to event log is little tricky because ASP.NET requires explicit write access to the event log.

·          Notice that “Wrox” is passed to the initializeData attribute. The string “Wrox” appears as the application or source for this event. This works fine when debugging your application.

·          However, when your application is deployed, it will probably run under a less privileged account, so you must give explicit write access to a registry key such as below

 

HKLM\System\CurrentControlSet\Services\EventLog\Application\Wrox, where “Wrox” is the same string passed in to initializeData

 

Diagnostic Switches

 

·          Switches are use to change the level of tracing at run time.

·          This functionality is achieved by leveraging classes that inherit from the Switch class within your code.

 

·          The .NET Framework includes three such classes, BooleanSwitch, TraceSwitch & SourceSwitch.

 

Boolean Switch –

 

·          The BooleanSwitch class provides a mechanism to indicate whether tracing should be enabled.

·          To use a BooleanSwitch, create an instance and pass in the switch name that appears in the application’s config file.

 

<configuration>

<system.diagnostics>

<switches>

<add name=”ImportantSwitch” value=”1” /> <!-- This is for the BooleanSwitch -->

<add name=”LevelSwitch” value=”3” /> <!-- This is for the TraceSwitch -->

<add name=”SourceSwitch” value=”4” /> <!-- This is for the SourceSwitch -->

</switches>

</system.diagnostics>

</configuration>

 

 

// If ImportantSwitch is set to 1 in the config file, the call to WriteIf sends a string to trace output.

BooleanSwitch aSwitch = new BooleanSwitch(“ImportantSwitch”, “Show errors”);

System.Diagnostics.Trace.WriteIf(aSwitch.Enabled, “The Switch is enabled!”);

 

 

Trace Switch –

 

·          If you want to achieve more granularity when you configure which tracing information to

·          display, you can use the TraceSwitch class. You can set an instance of a TraceSwitch class

·          to a numeric value to indicate the level of tracing information that should be displayed

 

·          The TraceSwitch class supports five levels of tracing, from 0 through 4

 

N/A

0

Tracing is turned off.

TraceError

1

Error messages only.

TraceWarning

2

Warning and error messages.

TraceInfo

3

Informational, warning, and error messages.

TraceVerbose

4

Verbose.

 

·          Setting an instance of the TraceSwitch class to a particular value is cumulative. For example, if the value is set to 3, not only is TraceInfo enabled, but TraceWarning and TraceError are enabled as well.

 

 

TraceSwitch tSwitch = new TraceSwitch(“LevelSwitch”, “Trace Levels”);

System.Diagnostics.Trace.WriteIf(tSwitch.TraceInfo, “The Switch is 3 or more!”);

 

 

SourceSwitch

 

·          New with .NET 2.0 is SourceSwitch, which is similar to TraceSwitch but provides a greater level of granularity.

·          You call SourceSwitch.ShouldTrace with an EventType as the parameter:

 

SourceSwitch sSwitch = new SourceSwitch(“SourceSwitch”, “ Even More Levels”);

System.Diagnostics.Trace.WriteIf(sSwitch.ShouldTrace(TraceEventType.Warning), “The Switch is 4 or more!”);

 

 

 

 

Thanks & Regards,

Arun Manglick || Senior Tech Lead || Monetrics - MDE || +91 20 30230500 Ext: 622 | +91 9850901262

 

No comments:

Post a Comment