Change trace switch level via app.config
Asked Answered
O

4

11

I have app that configures its trace source as follows:

        var traceSource = new TraceSource("MyTraceSource");
        traceSource.Switch = new SourceSwitch("MyTraceSwitch") { **Level = SourceLevels.Information** };

        var traceListener = new TextWriterTraceListener(logFilePath);
        traceListener.TraceOutputOptions = TraceOptions.DateTime;

        traceSource.Listeners.Clear();
        traceSource.Listeners.Add(traceListener);

        Trace.AutoFlush = true;

The app always uses this trace source to trace events. Please note that SourceLevels.Information is hardcoded in trace switch. Now I need to change the trace switch level to Verbose. Is it possible to accomplish via app.config file? I tried many xml-configs but failed. Note I cannot change the source code only app.config.

Odilia answered 15/2, 2013 at 8:57 Comment(0)
P
7

I'm not sure if you are searching for something like this, but I've used once the following xml configuration to: change the trace switch level to Verbose.(App-Config)

  <configuration>
        <system.diagnostics>
            <switches>
            <add name="AppTraceLevel" value="4" /> //4 = Verbose
            </switches>
            // Here would be the Trace Tag with the Listeners (not important for your question)
        </system.diagnostics>
    </configuration>

Maybe it helps

Prevail answered 18/2, 2013 at 8:13 Comment(0)
G
3

Well - Configuring Tracing clearly specifies:

  <sources>
        <source name="System.ServiceModel" 
                switchValue="Information, ActivityTracing"
                propagateActivity="true">
        </source>
  </sources>

Trace Level section describes some details.

Grishilda answered 18/2, 2013 at 8:14 Comment(0)
M
2

Both answers above have value. Here is the complete response. Add this section to your config:

<system.diagnostics>
<sources>
  <source name="MyTraceSource" switchValue="Information">
    <listeners>
      <add name="file" initializeData="c:\temp\logpath.txt" traceOutputOptions="DateTime" type="System.Diagnostics.TextWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

    </listeners>
  </source>
</sources>
</system.diagnostics>
Micronutrient answered 23/2, 2013 at 4:34 Comment(1)
You forgot to mention the switch value in the application is controlled by the switchValue="Information" attribute, overriding the default value specified in the application code. Set it to Verbose instead of Information to do what the original poster asked.Nomadic
O
0

It is a problem that in the original question (vkrzv's post) the listeners were deleted:

traceSource.Listeners.Clear();

Which cause that the listener was added in app.config (In Glenn Ferries wonderful solution. I like it so much. Thank you.) will be deleted.

So one of the full solution is here. You will have 2 log files in c:\temp folder: logcode.txt and logappconfig.txt

Code:

var traceSource = new TraceSource("MyTraceSource");

var traceListener = new TextWriterTraceListener(@"c:\temp\logcode.txt");
traceListener.TraceOutputOptions = TraceOptions.DateTime;

//traceSource.Listeners.Clear(); //we do not want to delete the listener
traceSource.Listeners.Add(traceListener);

Trace.AutoFlush = true;

App.Config:

<system.diagnostics>
<sources>
  <source name="MyTraceSource" switchValue="Information">
    <listeners>
      <add name="file" initializeData="c:\temp\logappconfig.txt" traceOutputOptions="DateTime" type="System.Diagnostics.TextWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </listeners>
  </source>
</sources>
</system.diagnostics>
Orban answered 19/11, 2020 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.