Analyzing Problems and Measuring Performance via the Trace Class


The trace tools in the .NET Framework make use of the Trace class, which provides properties and methods that help you trace the execution of your code. By default, tracing is enabled in VB 2005, so not unlike your previous debug discussion, all you have to do is set up the output and utilize its capabilities.

You can specify the detail level you want to perform for your tracing output by configuring trace switches. You will see an example of setting a trace switch shortly, but it is important to understand what a trace switch can do and what the settings for trace switches mean.

Trace switches can be either BooleanSwitch or TraceSwitch. BooleanSwitch has a value of either 0 or 1 and is used to determine whether tracing is off or on, respectively, while TraceSwitch allows you to specify a level of tracing based on five enumerated values. You can manage a BooleanSwitch or TraceSwitch as an environment variable. Once a switch is established, you can create and initialize it in code and use it with either trace or debug.

A TraceSwitch can have five enumerated levels, which can be read as 04 or checked with four properties provided in the switch class interface. The four properties return a Boolean value based on whether the switch is set to a certain level or higher. The five enumerated levels for TraceSwitch are as follows:

Open table as spreadsheet

Level

Description

0

None

1

Only error messages

2

Warning and error messages

3

Information, warning, and error messages

4

Verbose, information, warning, and error messages

The four properties are TraceError, TraceWarning, TraceInfo, and TraceVerbose. For example, if your switch were set at number 2 and you asked for the TraceError or TraceWarning properties, they would return True, while the TraceInformation and TraceVerbose properties would return False.

An environment variable managed either via the command line or under My computer image from book Properties image from book Advanced within the Environment Variables button.

Within the Environment Variables button, you add a new User variable, giving it the SwitchName and Value for that switch.

From the command line, type Set _Switch_MySwitch = 0

The value on the left of the = symbol is the name of the switch, and the value on its right is either 0 or 1 for a BooleanSwitch or 04 for a TraceSwitch. Note that there is a space between the word Set and the leading underscore of _Switch. Once you have typed this line, if you follow that by the plain SET command at the command line, it will show your new switch as an environment variable, as shown in Figure 9-6.

image from book
Figure 9-6

For the example that follows, you have the output directed to the default Output window:

  Sub TraceExample1()   Dim objTraceSwitch As TraceSwitch   objTraceSwitch = New TraceSwitch("ExampleSwitch", "Test Trace Switch")   objTraceSwitch.Level = TraceLevel.Error   Try     Throw (New EntryPointNotFoundException())   Catch objA As System.EntryPointNotFoundException     Trace.WriteLineIf(objTraceSwitch.TraceVerbose, _         "First Trace " & objA.Source)     Trace.WriteLineIf(objTraceSwitch.TraceError, _         "Second Trace " & objA.Message)   End Try End Sub 

You begin by assigning your switch to an existing registry entry and setting its level:

 objTraceSwitch = New TraceSwitch("ExampleSwitch", "Test Trace Switch") objTraceSwitch.Level = TraceLevel.Error

After you throw your exception, you first cause your trace output listener to catch the Source property of your Exception object based on whether the value of your switch is TraceVerbose or better:

 Trace.WriteLineIf(objTraceSwitch.TraceVerbose, _     "First Trace " & objA.Source)

Because the tracing level is set to Error, this line is skipped; and you continue by writing a trace to the Output window to include the message information if the level is set to Error:

 Trace.WriteLineIf(objTraceSwitch.TraceError, _     "Second Trace " & objA.Message)

As indicated in your Output window, you successfully wrote only the second trace line based on the level being Error on your trace switch (see Figure 9-7).

image from book
Figure 9-7

Tracing can also be helpful in determining the performance of your application. Overall, your application might appear to be working fine, but it is always good to be able to measure the performance of your application so that environment changes or degradation over time can be counteracted. The basic concept here is to use conditional compilation so that you can toggle your performance-measuring code on and off:

  Sub TraceExample2()   Dim connInfo As New Connection()   Dim rstInfo As New Recordset()   #Const bTrace = 1   Dim objWriter As New _     IO.StreamWriter(IO.File.Open("c:\mytext.txt", IO.FileMode.OpenOrCreate))   connInfo.ConnectionString = "Provider = sqloledb.1" & _     ";Persist Security Info = False;" & "Initial Catalog = Northwind;" & _     "DataSource = LocalServer"   connInfo.Open(connInfo.ConnectionString, "sa")   Trace.Listeners.Add(New TextWriterTraceListener(objWriter))   #If bTrace Then     Trace.WriteLine("Begun db query at " & now())   #End If   rstInfo.Open("SELECT CompanyName, OrderID, " & _     "OrderDate FROM Orders AS a LEFT JOIN Customers" & _     " AS b ON a.CustomerID = b.CustomerID WHERE " & _     "a.CustomerID = 'Chops'", connInfo, _     CursorTypeEnum.adOpenForwardOnly, _     LockTypeEnum.adLockBatchOptimistic)   #If bTrace Then     Trace.WriteLine("Ended db query at " & now())   #End If   Trace.Listeners.Clear()   objWriter.Close()   rstInfo.Close()   connInfo.Close()   rstInfo = Nothing   connInfo = Nothing End Sub 

Important 

This subroutine uses ADO, so be sure to add a reference to an ADO library and an Imports ADODB statement in the declarations section of the module.

In this simple example, you are trying to measure the performance of a database query using a conditional constant defined as bTrace by the following code:

 #Const bTrace = 1

You establish your database connection strings, and then right before you execute your query you write to a log file based on whether you are in tracing mode or not:

 #If bTrace Then   Trace.WriteLine("Begun db query at " & now()) #End If

Again, after your query returns you write to your log only if you are in tracing mode:

 #If bTrace Then   Trace.WriteLine("Ended db query at" & now()) #End If

Always remember that tracing can potentially slow the application down, so use this functionality only when troubleshooting, not all the time.




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net