我正在使用最新的NLog v3.1,并对如何在运行时设置日志记录级别提出疑问。我的NLog.config文件中只有一个目标和记录器。记录器名称=“*”和minlevel =“Info”。我在模块中有以下代码来声明记录器以及函数GetLoggingLevel,我可以在记录器名称中传递它以检索它的级别。但是,我如何设置日志记录级别?目前我必须打开NLog.config XML并修改XML中的最小级别。由于我有autoReload =“true”,它会生效 - 但是希望有一种方法可以使用NLog方法/属性来设置它。
Imports System.Xml
Imports NLog
Module modLogging
Private m_Log As Logger
Public ReadOnly Property Log As Logger
Get
If (m_Log Is Nothing) Then
m_Log = LogManager.GetCurrentClassLogger
End If
Return m_Log
End Get
End Property
Public Sub LogShutdown()
LogManager.Shutdown()
End Sub
Public Function GetLoggingLevel(ByVal loggerName) As String
Dim level As String = String.Empty
If (LogManager.GetLogger(loggerName).IsInfoEnabled) Then
level = "Info"
ElseIf (LogManager.GetLogger(loggerName).IsErrorEnabled) Then
level = "Error"
ElseIf (LogManager.GetLogger(loggerName).IsDebugEnabled) Then
level = "Debug"
End If
Return (level)
End Function
有了这个,我可以在我的项目中轻松调用Log.Info(“some some”)或Log.Error(“some error”)等。我可以获取当前级别并向用户显示此信息,但我希望用户能够将日志记录级别更改为Debug,Info,Error等,但我无法弄清楚如何在配置文件中设置minlevel在运行时没有加载&直接修改配置XML。任何帮助将不胜感激。
最基本的问候
答案 0 :(得分:2)
您可以访问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="consoleDetailed"
xsi:type="Console"
layout="${message}"/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="consoleDetailed" />
</rules>
</nlog>
和一个控制台应用程序,如
Sub Main()
Dim log As Logger = LogManager.GetCurrentClassLogger
log.Debug("debug message")
log.Info("info message")
For Each rule As LoggingRule In LogManager.Configuration.LoggingRules
rule.DisableLoggingForLevel(LogLevel.Debug)
Next
LogManager.ReconfigExistingLoggers()
log.Debug("debug message") REM This line will not be logged
log.Info("info message")
Console.ReadLine()
End Sub
答案 1 :(得分:0)
您需要实现ILoggerProvider
才能返回自己的自定义记录器。您创建的记录器可以实现ILogger
。您将需要复制nlog配置文件并更改最小和最大级别以允许所有日志级别。该类将被覆盖,该类仅用于需要特殊日志记录的特殊请求。
VB:
Public NotInheritable Class CustomLevelNLogProvider
Inherits ILoggerProvider
Private ReadOnly factory As NLog.LogFactory
' pass a separate nlog config path that has logging enabled all the way to trace level, you will override it down below in the LogWrapper class
Public Sub New(ByVal customNlogConfigPath As String)
factory = NLog.Web.NLogBuilder.ConfigureNLog(customNlogConfigPath)
End Sub
Public Function CreateLogger(ByVal categoryName As String) As ILogger
Dim logger = factory.GetLogger(categoryName)
Return New CustomLevelLogger(logger)
End Function
End Class
Public NotInheritable Class CustomLevelLogger
Inherits ILogger
Private ReadOnly innerLogger As ILogger
Private ReadOnly minLevel As LogLevel
Private ReadOnly maxLevel As LogLevel
Public Sub New(ByVal innerLogger As ILogger)
Me.innerLogger = innerLogger
End Sub
' call this method on each request that needs a temporary log level
Public Sub SetLogLevels(ByVal minLevel As LogLevel, ByVal maxLevel As LogLevel)
Me.minLevel = minLevel
Me.maxLevel = maxLevel
End Sub
' implement the ILogger interface, making sure to use minLevel and maxLevel properly, forward calls on to innerLogger if level matches
}
End Class
C#:
public sealed class CustomLevelNLogProvider : ILoggerProvider
{
private readonly NLog.LogFactory factory;
// pass a separate nlog config path that has logging enabled all the way to trace level, you will override it down below in the LogWrapper class
public CustomLevelNLogProvider(string customNlogConfigPath)
{
factory = NLog.Web.NLogBuilder.ConfigureNLog(customNlogConfigPath);
}
' call this method on each request that needs a temporary log level
public ILogger CreateLogger(string categoryName)
{
var logger = factory.GetLogger(categoryName);
return new CustomLevelLogger(logger);
}
}
public sealed class CustomLevelLogger : ILogger
{
private readonly ILogger innerLogger;
private readonly LogLevel minLevel;
private readonly LogLevel maxLevel;
public CustomLevelLogger(ILogger innerLogger)
{
this.innerLogger = innerLogger;
}
// call this method on each request that needs a temporary log level
public void SetLogLevels(LogLevel minLevel, LogLevel maxLevel)
{
this.minLevel = minLevel;
this.maxLevel = maxLevel;
}
// implement the ILogger interface, making sure to use minLevel and maxLevel properly, forward calls on to innerLogger if level matches
}
答案 2 :(得分:0)
NLog版本。 4.6.7使您能够执行此操作:
select count(distinct ip_address) from sh_page_visits
然后您可以执行以下代码,它将动态更新日志记录规则:
<nlog>
<variable name="myLevel" value="Warn" />
<rules>
<logger minLevel="${var:myLevel}" />
</rules>
</nlog>
另请参阅:https://github.com/NLog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules