简体   繁体   English

设置自动刷新.Net跟踪侦听器的日志记录级别

[英]Set logging level of autoflush .Net Trace Listener

In a .Net 4.0 web-service I am using trace autoflush to write to a log file. 在.Net 4.0 Web服务中,我正在使用跟踪自动刷新功能来写入日志文件。

By adding the following in the web.config: 通过在web.config中添加以下内容:

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

Am trying to find a way to log only Trace.TraceError("error info") and exclude Trace.TraceInformation("some verbose debugging stuff") without altering my code and just changing the web.config? 是否试图找到一种方法来仅记录Trace.TraceError(“错误信息”)并排除Trace.TraceInformation(“一些冗长的调试内容”),而无需更改我的代码,而只需更改web.config?

The information I have found on MSDN shows how this can be done by adding code that calls Trace.Flush() and adding sources, switches and sharedlisteners, however I would like to continue using auto-flush and not alter the code. 我在MSDN上找到的信息显示了如何通过添加调用Trace.Flush()的代码以及添加源,开关和共享侦听器来完成此操作,但是我想继续使用自动刷新而不更改代码。

Many thanks in advance :) 提前谢谢了 :)

Old answer: 旧答案:

This appears to be impossible. 这似乎是不可能的。 Trace auto-flush does not have the capacity to have it's level set in the web.config. 跟踪自动刷新没有能力在web.config中设置其级别。

Update: 更新:

The listener may have a filter applied as follows 侦听器可能应用了以下过滤器

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

Note that the initializeData is a string and may take the values of Warning or Error. 请注意,initializeData是一个字符串,并且可以采用Warning或Error的值。 This filters the trace output accordingly. 这将相应地过滤跟踪输出。

Many thanks to Josip for following up on this question. 非常感谢Josip对这个问题的跟进。

Maybe you can use " switchValue " for this purpose like this: 也许您可以为此使用“ 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>

For switch value you would put Warning or Error... 对于开关值,您可以输入警告或错误...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM