代码之家  ›  专栏  ›  技术社区  ›  MADCookie

如何控制配置文件中跟踪侦听器的级别

  •  20
  • MADCookie  · 技术社区  · 14 年前

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

    我有默认的app.config。在我的代码中,我使用trace.traceInformation()和trace.traceError。

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

    模块1.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>
    
    2 回复  |  直到 6 年前
        1
  •  29
  •   JJS Joel Coehoorn    9 年前

    我不喜欢回答你自己的问题,但我也不喜欢不把某个问题作为答案就离开。尤其是当我找到我要找的东西时。

    这个 link 有我需要的信息。我总结一下,因为它很长。在配置中,添加一个侦听器。我需要的关键是使用 <filter> 对于听众。使用它,我可以部署应用程序,然后更改配置以控制写入文件的文本。我可以添加另一个拥有不同过滤器的侦听器,比如事件日志。

    总之,钥匙是 <滤波器& GT; . 属性 initializeData 设置为文本来自 System.Diagnostics.SourceLevels 枚举。

    • 信息允许信息、警告和错误
    • 警告允许警告和错误
    • 错误仅允许错误

    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>
    

    模块1.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
    
        2
  •  3
  •   boop lanpa    6 年前

    在这里,您可以看到一个不错的概述:

    http://www.15seconds.com/issue/020910.htm