"switch"和"switch"之间有什么区别?和“过滤器"在.NET中进行跟踪? [英] What is the difference between "switch" and "filter" in Tracing in .NET?

查看:90
本文介绍了"switch"和"switch"之间有什么区别?和“过滤器"在.NET中进行跟踪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET中的跟踪"中的开关"和过滤器"之间有什么区别?它们似乎以类似的方式工作.

What is the difference between "switch" and "filter" in Tracing in .NET ? They seem to work in similar way.

<system.diagnostics>
    <trace autoflush="true" indentsize="5">
      <listeners>
        <add name="DemoListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="D:\output1.txt">
        </add>
        <remove name="Default" />
      </listeners>
    </trace>    
    <sources>
      <source name="DemoApp" switchName="DemoApp">
        <listeners>
          <add name="DemoListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="D:\output2.txt">
            <filter type="System.Diagnostics.EventTypeFilter" initializeData="Error"/>
          </add>
          <remove name="Default" />
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="DemoApp" value="Error"/>
    </switches>
  </system.diagnostics>

推荐答案

有一点重叠. <filter>命名您编写的从TraceFilter派生的特定类.您可以使用它来抑制跟踪输出,一切皆有可能.它始终适用于特定的TraceListener.

There is a bit of overlap. A <filter> names a specific class that you write that's derived from TraceFilter. Which you can use to suppress trace output, anything is possible. It always applies to a specific TraceListener.

<switches>元素对于配置跟踪和设置TraceSwitch对象的值很有用.然后在代码中进行测试以选择性地绕过跟踪输出.请注意<switches>如何是全局"的,它不适用于特定的侦听器.因此,在TraceSource中测试开关的逻辑位置.开关的一个好用法是配置跟踪详细程度.就像您的错误"值将表明仅跟踪错误.

The <switches> element is useful to configure tracing and set the value of a TraceSwitch object. Which you then test in your code to selectively bypass trace output. Note how <switches> is "global", it doesn't apply to a specific listener. So a logical place to test the switch is in the TraceSource. A good use for a switch is to configure the tracing verbosity. Like your "Error" value would indicate that only errors are ever traced.

这篇关于"switch"和"switch"之间有什么区别?和“过滤器"在.NET中进行跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆