您可以访问 LogManager.Configuration.LoggingRules 并启用或禁用特定级别的日志记录。例如,使用一个带有 NLog 配置的小项目:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="console" xsi:type="Console" layout="${message}"/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="console" />
</rules>
</nlog>
和一个控制台应用程序
Sub Main()
Dim log As Logger = LogManager.GetCurrentClassLogger
REM Both lines will be logged as defined in the NLog.config
log.Debug("debug message")
log.Info("info message")
For Each rule As LoggingRule In LogManager.Configuration.LoggingRules
rule.DisableLoggingForLevel(LogLevel.Debug)
REM rule.EnableLoggingForLevel(LogLevel.Debug)
Next
LogManager.ReconfigExistingLoggers()
REM This line will not be logged as we just disabled it
log.Debug("debug message")
REM This line will still be logged
log.Info("info message")
Console.ReadLine()
End Sub
将禁用所有级别调试及以下的日志记录。非常关键的是这条线:
LogManager.ReconfigExistingLoggers()
我希望这有帮助。