设置 autoflush .Net Trace Listener 的日志记录级别

Set logging level of autoflush .Net Trace Listener

在 .Net 4.0 网络服务中,我使用跟踪自动刷新写入日志文件。

通过在 web.config 中添加以下内容:

<trace autoflush="true" >
  <listeners>
    <add name="TextWriter"
         type="System.Diagnostics.TextWriterTraceListener" 
         initializeData="trace.log" >
    </add>
  </listeners>
</trace>

我正在尝试找到一种方法来仅记录 Trace.TraceError("error info") 并排除 Trace.TraceInformation("some verbose debugging stuff") 而无需更改我的代码并仅更改 web.config?

我在 MSDN 上找到的信息显示了如何通过添加调用 Trace.Flush() 的代码并添加源、开关和共享监听器来完成此操作,但是我想继续使用自动刷新而不是修改代码。

非常感谢:)

旧答案:

This appears to be impossible. Trace auto-flush does not have the capacity to have it's level set in the web.config.

更新:

监听器可能应用了如下过滤器

<configuration>
<system.diagnostics>
    <trace autoflush="true"  >
        <listeners>
            <add name="TextWriter"
                     type="System.Diagnostics.TextWriterTraceListener"
                     initializeData="trace4.log" >
                <filter type="System.Diagnostics.EventTypeFilter" initializeData="Warning" />
            </add>
        </listeners>
    </trace>
</system.diagnostics>

注意initializeData是一个字符串,可能取值为Warning或Error。这会相应地过滤跟踪输出。

非常感谢 Josip 对这个问题的跟进。

也许您可以像这样使用“switchValue”:

<system.diagnostics>
    <trace autoflush="true">
    </trace>
    <sources>
        <source name="SCInterface" switchType="System.Diagnostics.SourceSwitch" **switchValue**="All">
            <listeners>
                <remove name="default"/>
                <add name="HSInterface"
                type="XYZ.Diagnostics.CyclicTextTraceListener, XYZ.Base3x"
                initializeData="D:\Trace\HSR.HSInterface.Trace.log, Size, 10, 5"
                traceOutputOptions="DateTime,ThreadId,Callstack" />
            </listeners>
        </source>
    </sources>
</system.diagnostics>

对于开关值,您可以放置​​警告或错误...