alt
Advertisement

Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow VB.NET 2005 arrow Web Application Tracing and Debugging
Site Search
Sponsored Links



Web Application Tracing and Debugging
Article Index
Web Application Tracing and Debugging
Page 2
Page 3

.
.

.

.
.
.

Trace Listeners

While using Trace and Debug, it is important to set up a mechanism for collecting and recording the messages that are sent. Trace messages are received by listeners. The purpose of a listener is to collect, store and route tracing messages. Listeners direct the tracing output to a designated target such as a log or a window or a text file.

Three types of predefined listeners are available:

1. A TextWriterTraceListener that redirects the outpur to an instance of TextWriter or to a stream class.

2. An EventLogTraceListener which redirects the output to an event log

3. A DefaultTraceListener which gives out Write and WriteLine messages to the OutputDebugString and to the Debugger.log method.

If the user wants any listener besides the DefaultTraceListener to receive Debug and Trace output, he must add it to the Listeners collection. Examine the following lines of code:

System.Diagnostics.Trace.WriteLine("Error in Widget 42")
System.Diagnostics.Trace.Listeners.Clear()
System.Diagnostics.Trace.Listeners.Add(New System.Diagnostics.TextWriterTraceListener(Console.Out))
If Not Request.Cookies("username")
Is Nothing Then

The first part is used to add a listener object to a Trace.Listeners collection in the application. The second part is used to make the listener to send tracing information t a console.

Trace Switches

Trace switches helps enable, disable and filter tracing output. These objects are existing in the code and also can be configured externally through the .config file. The .NET infrastructure provides two types of switches namely, BooleanSwitch class and TraceSwitch Class. The BooleanSwitch acts as a toggle switch, either enabling or disabling a variety of trace statements and the TraceSwitch Class allows you to enable a trace switch for a particular tracing level so that the trace message specified for that level and all the levels below it appear. If this switch is disabled, the trace messages will not appear.

Trace switches are useful in filtering information. For instance is the user wants only error messages in the application displayed. By using the .config file to configured with the appropriate settings we can control the types of messages that are received.

The developer needs to create a switch object from a BooleanSwitch class or TraceSwitch Class or he may use a developer defined switch.

A TraceSwitch object has four properties that return Boolean values indicating whether the switch is set at a particular level as given below:

1. TraceSwitch.TraceError Property
2. TraceSwitch.TraceWarning Property
3. TraceSwitch.TraceInfo Property
4. TraceSwitch.TraceVerbose Property

The appropriate levels can be set as per need using the properties as under:

Enumerated value

Integer value

Type of message displayed
(or written to a specified output target)

Off

0

None

Error

1

Only error messages

Warning

2

Warning messages and error messages

Info

3

Informational messages, warning messages, and error messages

Verbose

4

Verbose messages, informational messages, warning messages, and error messages

The following lines of code are used to create a switch object and use it to filter trace output:

Dim myTraceSwitch As New System.Diagnostics.TraceSwitch("SwitchOne", "The first switch")myTraceSwitch.Level = System.Diagnostics.TraceLevel.Error
' This message box displays true, becuase setting the level to
' TraceLevel.Info sets all lower levels to true as well.
MessageBox.show(myTraceSwitch.TraceWarning.ToString())
' This messagebox displays false.
MessageBox.Show(myTraceSwitch.TraceVerbose.ToString())

Conditional Compilation

While developing applications in .NET, the user can select any specific sections of the code to compile while excluding the other sections. Conditional compilation statements are designed to run during compile time and not at run time.

#Const directive is used to declare a conditional compiler constant. The user can typically assign any particular block of code to be compiled by using #IF … Then #Else directive. Let us assume that we are writing debugging statements that check the performance of order processing in a eCommerce solution and is to be used to process data of multiple types of customers who pay using credit card, direct account debit and by cheque. The Conditional compiler code will be like the following:

#If CreditCard Then
…..the code for credit card holders
#Elseif AccoutDebit Then
….the code for AccountDebit
#Else
….the code for other options
#End If

The value of the Credit Card can set a constant to true at compile time to compile the code written for that.

The predefined constants available for conditional compilation are given below:

Constant

Description

CONFIG

A string that corresponds to the current setting of the Active Solution Configuration box in the Configuration Manager.

DEBUG

A Boolean value that can be set in the Project Properties dialog box.

TARGET

A string representing the output type for the project or the setting of the command-line /target option. The possible values of TARGET are: "winexe" for a Windows application, "exe" for a console application, "library" for a class library, and "module" for a module. The /target option may be set in the Visual Studio integrated development environment

TRACE

A Boolean value that can be set in the Project Properties dialog box. By default, all configurations for a project define TRACE.

VBC_VER

A number representing the Visual Basic version, in major.minor format. The version number for Visual Basic




 
< Prev   Next >
Sponsored Links
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape