【问题标题】:Change trace switch level via app.config通过 app.config 更改跟踪开关级别
【发布时间】:2020-11-20 02:32:42
【问题描述】:

我的应用程序将其跟踪源配置如下:

        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;

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

【问题讨论】:

    标签: c# .net logging app-config


    【解决方案1】:

    我不确定您是否正在搜索类似的东西,但我曾经使用过以下 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>
    

    也许有帮助

    【讨论】:

      【解决方案2】:

      嗯 - Configuring Tracing 明确指出:

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

      跟踪级别部分描述了一些细节。

      【讨论】:

        【解决方案3】:

        以上两个答案都有价值。这是完整的回复。将此部分添加到您的配置中:

        <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>
        

        【讨论】:

        • 您忘了提及应用程序中的开关值由switchValue="Information" 属性控制,覆盖应用程序代码中指定的默认值。将其设置为 Verbose 而不是 Information 以执行原始发帖人要求的操作。
        【解决方案4】:

        原来的问题(vkrzv的帖子)中的听众被删除了,这是一个问题:

        traceSource.Listeners.Clear();
        

        在 app.config 中添加监听器的原因(在 Glenn Ferries 中很棒的解决方案。我非常喜欢它。谢谢。)将被删除。

        所以这里有一个完整的解决方案。您将在 c:\temp 文件夹中有 2 个日志文件:logcode.txt 和 logappconfig.txt

        代码:

        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;
        

        应用程序配置:

        <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>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-05-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-24
          相关资源
          最近更新 更多