如何以编程方式过滤掉特定记录器的消息?

时间:2011-12-03 21:05:38

标签: vb.net nlog

Salvete!在Nlog编码配置中,我们如何根据其名称过滤掉一个记录器?我想将所有内容记录到locallogtarget - ,除了到richtextmessengertarget的内容。我尝试添加一个过滤器,但我必须做错了,因为过滤器不会过滤任何东西!

这个链接对我有帮助,但并没有完全解决它: Having NLog loggers with different configuration

在日志记录类中,我有这个:

Private MainNlogConfig As New LoggingConfiguration()
Dim locallogtarget As New FileTarget()
Dim labelmessengertarget As New FormControlTarget()
Dim localrule As New LoggingRule("*", LogLevel.Info, locallogtarget)
MainNlogConfig.AddTarget("file", locallogtarget)
MainNlogConfigAddTarget("FormControl", richtextmessengertarget)

With locallogtarget
    .Layout = "${longdate} ${logger} ${message}"
    .FileName = appdir & appName & ".log"
End With
Dim filteringTarget As New FilteringTargetWrapper()
filteringTarget.WrappedTarget = locallogtarget
filteringTarget.Condition = "not equals('${logger}', 'messenger')"
SimpleConfigurator.ConfigureForTargetLogging(filteringTarget, LogLevel.Info)

With richtextmessengertarget
    .Layout = "${message} ${logger}" & vbCrLf
    .FormName = "form_myform"
    .ControlName = "richtext_messenger"
    .Append = True
End With

LogManager.Configuration = MainNlogConfig

要启动信使,我使用它:

Public MessengerLogger = LogManager.GetLogger("messenger")
MessengerLogger.Info("This message should go to richtext control, but not log file.")

0 个答案:

没有答案