【问题标题】:How to configure message filtering through switches如何通过交换机配置消息过滤
【发布时间】:2011-02-02 15:21:16
【问题描述】:

这是我的代码

 public static TraceSource TS = new TraceSource("myTraceSrc", SourceLevels.All);
    static void Main(string[] args)
    {

        TS.TraceInformation("Hello Information Trace from Main");
        TS.TraceEvent(System.Diagnostics.TraceEventType.Error, 1, "Hello Error Trace from Main");

}

这里是配置文件

<system.diagnostics>
    <trace autoflush="true" />
    <sources>
        <source name="myTraceSrc" switchName="switch1" >
            <listeners>
                <add type="System.Diagnostics.TextWriterTraceListener" name="myLocalListener" initializeData="c:\WasteBin\Test.Log"  />
                <add name="consoleListener" />
            </listeners>
        </source>
    </sources>
    <sharedListeners>
        <add type="System.Diagnostics.ConsoleTraceListener" name="consoleListener" traceOutputOptions="None" />
        <add type="System.Diagnostics.EventTypeFilter" name="EventListener" traceOutputOptions="None" />
    </sharedListeners>
    <switches>
        <add name="switch1" value="all"  />
    </switches>
</system.diagnostics>

我希望我的所有消息都发送到控制台和文本文件,但只有错误应该发送到事件日志。如何使用配置设置进行设置?

【问题讨论】:

    标签: c# trace system.diagnostics


    【解决方案1】:

    尝试为您的任务使用日志库(Nlog、log4net)。他们有消息过滤和路由。

    【讨论】:

    • 我已经在考虑使用一个,但你是说通过 TraceSource 不可能做到这一点?
    • 您可以在侦听器中按类别或事件类型过滤 TraceSource。检查此链接Use TraceSource and Filters with Trace Listeners
    • -1,根本不回答这个问题 - OP 想知道如何配置他的 TraceSource 侦听器而不是存在哪些替代日志系统。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2013-03-04
    • 2022-11-17
    相关资源
    最近更新 更多