Tracing question

Jayson

Member
Joined
Jun 3, 2008
Messages
10
Programming Experience
1-3
Hi,

I have configured tracing within my app so that users can define a trace level within a xml config file. When the app starts it reads the config and sets the trace level accordingly.

VB.NET:
Public TraceSwitch As New TraceSwitch("MyTraceSwitch", "Sets the trace level in this application")

Form_Load(.....)
Dim MyListener As TextWriterTraceListener

'Set the initial trace level. Just in case the config load throws an error.
TraceSwitch.Level = TraceLevel.Info

'then later in form_load....
MyListener = New TextWriterTraceListener(My.Settings.LogPath & My.Settings.MessagesFile)
Trace.Listeners.Add(MyListener)
Trace.AutoFlush = True
TraceSwitch.Level = CType(XMLConfig.DebugLevel, Integer)
What I would like to do is have different log files generated depending on the trace that gets initiated.

For example, if an error then log to an errors log or if just info then to a different log.

I played around with this and can get different logs created however they contain the same trace information. E.g. error and info in the same file.

I know I could just create my own logging class but I thought that perhaps this would be cleaner and easier??

Thanks in advance for your advice.
 
The TraceSwitch levels are increasingly verbose and includes all lower level messages. (1:error 2:warning+error 3:info+warning+error 4:all) This accumulation also applies to TraceEventFilter of listeners, but you can inherit this class and override ShouldTrace function to filter only explicit level messages. Example of such class:
VB.NET:
Public Class ExplicitFilter
    Inherits Diagnostics.EventTypeFilter

    Public Sub New(ByVal filter As TraceEventType)
        MyBase.New(SourceLevels.Off)
        Me.events = filter
    End Sub
    Private events As TraceEventType
    Public Overrides Function ShouldTrace(ByVal cache As System.Diagnostics.TraceEventCache, ByVal source As String, _
                                          ByVal eventType As System.Diagnostics.TraceEventType, ByVal id As Integer, _
                                          ByVal formatOrMessage As String, ByVal args() As Object, _
                                          ByVal data1 As Object, ByVal data() As Object) As Boolean
        Return (events And eventType) = eventType
    End Function
End Class
You could create two trace listeners that write to separate files like this:
VB.NET:
Private Sub initTrace()
    Dim lis As New TextWriterTraceListener("errors.txt")
    lis.Filter = New ExplicitFilter(TraceEventType.Error Or TraceEventType.Warning)
    Trace.Listeners.Add(lis)
    lis = New TextWriterTraceListener("infos.txt")
    lis.Filter = New ExplicitFilter(TraceEventType.Information)
    Trace.Listeners.Add(lis)
    Trace.AutoFlush = True
End Sub
Then write some sample traces:
VB.NET:
Trace.TraceError("this is an error")
Trace.TraceWarning("this is a warning")
Trace.TraceInformation("this is an information")
A conventional listener filtered for SourceLevels.Information will write all these messages, but looking at the sample text files you see them filter explicitly.
 
Tracing problems continue

Hi John,

Thanks for you help with the tracing. Works great!

I'm having one small problem that I was hoping you may be able to shed some light on.

The message that is returned is something like,

DocumentImporterTool.vshost.exe Information: 0 : This is my message.

If possible I would like to display,

Information: 0 : This is my message.

Thanks
 
It is if you write a class that inherits TextWriterTraceListener and override TraceEvent, here you can set source = String.Empty.
 
Back
Top