Analytics


Google

Saturday, April 26, 2008

Tracing in ASP.Net

Tracing in ASP.Net

Tracing can be enabled either at the page level or for the whole application.

Page level
To activate tracing for a specific page, just add trace=enabled in the page directive as in:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Maint.aspx.vb" Inherits="Maint" Trace="True" %>

When enabled at the page level, the trace information will be appended at the bottom of the page.

Application level
The switch to activate it at the application level is in the web.config. The directive is as follows:

<trace enabled="true" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="false"/>

The above tag is within the system.web tag as in:
<system.web>
<trace enabled="true" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="false"/>
</system.web>


With the application level tag, you have additional flexibility:

pageOutput - true will produce the output to the bottom of each page within the application. false will not.

In addition, another output is available - trace.axd page will be available once the trace is enabled at the application level. A sample of trace.axd is shown here:



TraceSwitch
TraceSwitch can be used to control the level of tracing to be used in the application. There is two steps to this process:

1. Add an entry in the web.config.
2. Create an object to represent the switch.

The web.config entry looks like this:

<system.diagnostics>
<switches>
<add name="debugSwitch" value="4"/>
<!--
Value options are:
Off 0
Error 1
Warning 2
Info 3
Verbose 4
-->
</switches>
</system.diagnostics>


The above entry must be between the <configuration> and </configuration>. The comment is not necessary but is there to remind me what the values mean.

This is how you access the setting:

Dim dSwitch As New TraceSwitch("debugSwitch", "For TraceDebugging")

With this you have properties to identify the trace level:

.TraceError
.TraceWarning
.TraceInfo
.TraceVerbose


Note that the tracelevel is a hierarchy, which means the higher level will include all messages provided for the lower level. So the value will be <=. E.g. TraceInfo will include messages for TraceInfo, TraceWarning and TraceError.

To help reduce the amount of code, I created a helper subroutine to output the trace messages:

Sub traceMsg(ByVal pLevel As TraceLevel, ByVal pMsg As String)
If dSwitch.Level >= pLevel Then
Trace.Write("Debug", pMsg)
End If
End Sub


Trace.Write is used to output the message to the trace listener - in this case the page trace.axd. Note that the tracelevel is in a hierarchy. It means TraceWarning will show messages for TraceWarning and TraceError - TraceInfo will include TraceWarning and TraceError.

To facilitate coding in multiple applications, you can place this in a common library as a module or a share subroutine and compile it into a library (.dll) and then refer to the library in your subsequent projects.

The following is a sample on how to use the tracing.

Web.config

<?xml version="1.0"?>
<configuration>
<system.web>
<trace enabled="true" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="false"/>
<system.diagnostics>
<switches>
<add name="debugSwitch" value="4"/>
<!--
Value options are:
Off 0
Error 1
Warning 2
Info 3
Verbose 4
-->
</switches>
</system.diagnostics>
</configuration>


Again the key section here is the system.diagnostics and trace.

1 comment:

Strovek said...

Additional Note for Non ASP.NET application

There is no default listener for Console programs or Window programs. For Console programs, if you wish to output the trace messages to the console, you can do the following:

Dim writer As TextWriterTraceListener = New TextWriterTraceListener(System.Console.Out)
System.Diagnostics.Debug.Listeners.Add(writer)

System.Console.Out is the output stream for Console class; you can use the same method to stream to a textbox, label or even a file.