MAD*_*kie 21 .net logging trace
我正在尝试学习跟踪的内置功能.我无法弄清楚如何使用配置来设置写入我的监听的级别(信息,警告,错误).
我有默认的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
Run Code Online (Sandbox Code Playgroud)
的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>
Run Code Online (Sandbox Code Playgroud)
MAD*_*kie 30
我不是回答你自己的问题的粉丝,但我也不喜欢在没有标明答案的情况下留下问题.当我找到我想要的东西时尤其如此.
这个链接有我需要的信息.我会总结一下,因为它很长.在配置中,添加一个侦听器.我需要的关键是使用<filter>
听众.有了它,我可以部署我的应用程序,然后更改配置来控制写入文件的文本.我可以添加另一个具有不同过滤器的监听器,例如可能是事件日志.
无论如何,关键是<filter>
.该属性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>
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)