Reputation: 1764
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.
Upvotes: 11
Views: 8104
Reputation: 320
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>
Upvotes: 0
Reputation: 10390
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>
Upvotes: 2
Reputation: 1736
Well - Configuring Tracing clearly specifies:
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
</source>
</sources>
Trace Level section describes some details.
Upvotes: 3
Reputation: 5618
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
Upvotes: 7