Analytics


Google

Sunday, May 25, 2008

Enhanced Tracing in Framework 2.0

Following up on the previous post on logging, seems that the same mechanism can be used on the tracing. In the case of application with multiple modules, you can create multiple TraceSource and associate each TraceSource to each module. That way, you can trace output from each module separately.

To activate the tracesource, you just have to retrieve the tracesource in your program as follows:

Dim ts as new TraceSource("MainForm")

"MainForm" is the name of the tracesource as seen in the config file:
  <system.diagnostics>
<sources>
<!-- This section defines the logging configuration for My.Application.Log -->
<source name="MainForm" switchValue="All">
<listeners>
<add name="LogToFile"/>
</listeners>
</source>
</sources>
<switches>
<add name="DefaultSwitch" value="All" />
<add name="ActSwitch" value="ActivityTracing" />
<add name="VerSwitch" value="Verbose" />
</switches>
<sharedListeners>
<!-- add name="FileLog"
type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
initializeData="FileLogWriter"/ -->
<!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
<!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
<add name="LogToFile" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
initializeData="FileLogWriter"
logFileCreationSchedule="Daily"
traceOutputOptions="DateTime"
BaseFileName="MainWin"
location="Custom"
CustomLocation="c:\log\"
Delimiter ="|"
AutoFlush="True"
Append="True"
/>
</sharedListeners>
</system.diagnostics>


This is superior to the one available in Framework 1.1 because, instead of using a If statement to check the tracelevel prior to performing Trace.WriteLine, you use ts.TraceEvent(TraceEventType.Information, 1, "Started Trace").

In framework 1.1, I facilitated this by creating a subroutine:

'==================================================
' performs a trace write if the switch is > then the pLevel
' If flg = 0 then do a trace.write
' if flg = 1 then do a trace.writeline
' =================================================
Sub traceMsg(ByRef tSwitch As TraceSwitch, ByVal pLevel As TraceLevel, _
ByVal pMsg As String, Optional ByVal traceCat As String = "Debug", _
Optional ByVal flg As Integer = 0)

If tSwitch.Level >= pLevel Then
If flg = 0 Then
Trace.Write(traceCat, pMsg)
Else
Trace.WriteLine(traceCat, pMsg)
End If

End If
End Sub

No comments: