通过app.config更改跟踪开关级别

vkr*_*rzv 11 .net c# logging app-config

我有app配置其跟踪源如下:

        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;
Run Code Online (Sandbox Code Playgroud)

应用程序始终使用此跟踪源来跟踪事件.请注意,SourceLevels.Information在跟踪开关中是硬编码的.现在我需要将跟踪开关级别更改为Verbose.是否可以通过app.config文件完成?我尝试了很多xml-configs但是失败了.注意我无法仅更改源代码app.config.

eMi*_*eMi 7

我不确定你是否在搜索这样的东西,但是我曾经使用过以下的xml配置:( 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>
Run Code Online (Sandbox Code Playgroud)

也许有帮助