【问题标题】:how do you control the level of a trace listener in the config file如何在配置文件中控制跟踪侦听器的级别
【发布时间】:2011-04-11 22:03:54
【问题描述】:

我正在尝试学习跟踪的内置功能。我不知道如何使用配置来设置写入我听的级别(信息、警告、错误)。

我有默认的 app.config 。在我的代码中,我使用 Trace.TraceInformation() 和 Trace.TraceError。

所有消息都写入我的文本文件。我希望能够更改 app.config 中的某些内容以使其记录 Info 消息或仅记录错误消息。

Module1.vb

Sub Main(ByVal args() As String)
    Dim index As Integer = 0
    For Each arg As String In args
        Trace.TraceInformation(String.Format("Sub Main(): arg({1}) = {0}", arg, index))
        Trace.Flush()

        If arg.Split("=").Count = 2 Then
            If String.Compare(arg.Split("=")(0), "mode", True) = 0 Then _Mode = arg.Split("=")(1)
        End If

        index += 1
    Next
End Sub

app.config

    <sources>
        <!-- This section defines the logging configuration for My.Application.Log -->
        <source name="DefaultSource">
            <listeners>
                <add name="FileLog"/>
                <!-- Uncomment the below section to write to the Application Event Log -->
                <!--<add name="EventLog"/>-->
            </listeners>
        </source>
    </sources>
    <switches>
        <add name="DefaultSwitch" value="1" />

    </switches>
    <sharedListeners>
        <add name="FileLog"
             type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
             initializeData="FileLogWriter"/>
        <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
        <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="HealthSurvey Console"/> -->
    </sharedListeners>

</system.diagnostics>

【问题讨论】:

  • 我想把重点放在 Trace 之外的方法上,称为 TraceInformation、TraceError、TraceWarning。
  • 查看框架源代码我怀疑 Trace.TraceX 方法不支持开关。 (因此你必须使用过滤器)。
  • 想想也许你必须使用 TraceSource 才能使用 TraceSwitches,而 Trace.TraceX 静态不使用 TraceSource

标签: .net logging trace


【解决方案1】:

我不喜欢回答你自己的问题,但我也不喜欢留下问题而不将某些内容标记为答案。当我找到我要找的东西时尤其如此。

这个link 有我需要的信息。我会总结一下,因为它很长。在配置中,您添加一个侦听器。我需要的关键是使用&lt;filter&gt; 作为监听器。有了它,我可以部署我的应用程序,然后更改配置以控制写入文件的文本。我可以添加另一个具有不同过滤器的侦听器,例如事件日志。

无论如何,关键是&lt;filter&gt;。属性initializeData 设置为来自System.Diagnostics.SourceLevels 枚举的文本。

  • 信息允许信息、警告和错误
  • Warning 允许警告和错误
  • 错误只允许错误

app.config

<system.diagnostics>
  <trace autoflush="false" indentsize="1">
    <listeners>
      <add name="textListener"
           type="System.Diagnostics.TextWriterTraceListener"
           traceOutputOptions="None"
           initializeData="C:\Projects\TraceLogOutput.log">
        <filter 
           type="System.Diagnostics.EventTypeFilter"
           initializeData="Information"/>
      </add>
    <remove name="Default" />
  </listeners>
</trace>

module1.vb

Sub Main(ByVal args() As String)

    ' initializeData = Information only
    Trace.TraceInformation("Some Information message")
    Trace.Flush()

    ' initializeData = Information or Warning
    Trace.TraceWarning("Some Warning message")
    Trace.Flush()

    ' initializeData = Information, Warning or Error
    Trace.TraceError("Some Error message")
    Trace.Flush()

End Sub

【讨论】:

  • 回答自己的问题没有错
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-09
  • 2018-06-04
  • 1970-01-01
  • 2017-09-24
  • 2014-04-19
  • 2019-10-02
  • 2016-04-13
相关资源
最近更新 更多