【发布时间】: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