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>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").
<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>
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:
Post a Comment