views:

23

answers:

2

I'm trying to learn the built-in features of tracing. I can't figure out how to use the config to set the level (information, warn, error) that gets written to my listen.

I have the default app.config with it . In my code, I use Trace.TraceInformation() and Trace.TraceError.

All of the messages are written to my text file. I want to be able to change something in the app.config to make it record Info messages or just error messages.

Module1.vb

Sub Main(ByVal args() As String)
    Dim index As Integer = 0
    For Each arg As String In args
        Trace.TraceInformation(String.Format("Sub Main(): arg({1}) = {0}", arg, index))
        Trace.Flush()

        If arg.Split("=").Count = 2 Then
            If String.Compare(arg.Split("=")(0), "mode", True) = 0 Then _Mode = arg.Split("=")(1)
        End If

        index += 1
    Next
End Sub

app.config

    <sources>
        <!-- This section defines the logging configuration for My.Application.Log -->
        <source name="DefaultSource">
            <listeners>
                <add name="FileLog"/>
                <!-- Uncomment the below section to write to the Application Event Log -->
                <!--<add name="EventLog"/>-->
            </listeners>
        </source>
    </sources>
    <switches>
        <add name="DefaultSwitch" value="1" />

    </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="HealthSurvey Console"/> -->
    </sharedListeners>

</system.diagnostics>
+1  A: 

This is a decent overview:

http://www.15seconds.com/issue/020910.htm

ScottE
This is very helpful, but didn't hit the spot because I need to understand Trace.TraceInformation specifically. I updated my question to make it clearer.
MADCookie
A: 

I'm not a fan of answering your own questions, but I also don't like leaving questions without marking something as the answer. This is especially true when I found what I was looking for.

This link had the info I needed. I will summarize because it is pretty long. In the config, you add a listener. The key that I needed was using the <filter> for the listener. With it, I can deploy my application and then change the config to control the text written to the file. I could add another listener that had a different filter like maybe the eventlog.

Anyway, the key was <filter>. The attribute initializeData is set to the text from System.Diagnostics.SourceLevels enum.

app.config

    <trace autoflush="false" indentsize="1">
        <listeners>
            <add name="textListener" type="System.Diagnostics.TextWriterTraceListener" traceOutputOptions="None" initializeData="C:\Projects\TraceLogOutput.log">
                <filter type="System.Diagnostics.EventTypeFilter" initializeData="Information"/>
            </add>
            <remove name="Default" />
        </listeners>
    </trace>
</system.diagnostics>

module1.vb

Sub Main(ByVal args() As String)
    Trace.TraceInformation(String.Format("Sub Main(): Count command arguments {0}", args.Count))
    Trace.Flush()

    Trace.TraceError("Some Error message")
    Trace.Flush()

End Sub
MADCookie