sna*_*nap 35 .net logging nlog
这是我第一次使用NLog软件包进行日志记录,但到目前为止,它非常适合使用.
在我的场景中,我需要以编程方式初始化我的NLog LoggingConfiguration设置,而不是通过更典型的配置文件方案.我已经对它进行了测试,并将其全部按照默认方式运行.但是,如何在运行时以编程方式修改我的设置?这里最常见的情况可能是默认情况下应用程序的日志记录级别设置为ERROR,但在特定模块中出现错误,我希望将日志记录级别切换为更加冗长以跟踪错误.我想编写一个小的Web界面,以便我可以在运行时轻松调整这些设置,但我想确保我采用正确的方法.
小智 72
我知道我迟了一年但是我遇到了类似的情况,我需要为用户提供对记录器的动态控制.幸运的是,使用nlog相对容易.在这里,我只是为已经创建的Logger启用跟踪级别日志记录,但显然您可以执行任何您想要的操作,包括添加新的目标/规则,或者只是用程序生成的LoggerConfiguration完全替换LoggerConfiguration.
Basic ColoredConsole nlog.config:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target xsi:type="ColoredConsole" name="c"
layout="${longdate} ${uppercase:${level}} ${message}" />
</targets>
<rules>
<logger name="*" minlevel="Error" writeTo="c" />
</rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)
简单控制台应用:
public class Program
{
//Initialize default Logger from config file
private static readonly Logger m_log = LogManager.GetCurrentClassLogger();
public static Logger Log
{
get { return m_log; }
}
public static void Main(string[] args)
{
Log.Trace("You won't see me because config is at LogLevel.Error");
EnabledTraceForAllRules();
Log.Trace("You will see me now though!");
//Pause console window
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
/* Prints:
2013-05-07 16:04:22.7050 TRACE You will see me now though!
Press any key to continue...
*/
}
public static void EnabledTraceForAllRules()
{
foreach(var rule in LogManager.Configuration.LoggingRules)
{
rule.EnableLoggingForLevel(LogLevel.Trace);
}
//Call to update existing Loggers created with GetLogger() or
//GetCurrentClassLogger()
LogManager.ReconfigExistingLoggers();
}
}
Run Code Online (Sandbox Code Playgroud)
如果您需要从进程外部更改Logging配置并且必须以编程方式完成,我建议您使用WCF.您可以从提供重新配置nlog的方法的应用程序或网站公开一个小型WCF服务.
我在NLog文档(https://github.com/NLog/NLog/wiki/Configure-from-code)中找到了“配置API” ,可能对您有帮助。还要在NLog程序集中查看NLog.Config命名空间。
另请参阅:https : //github.com/NLog/NLog/wiki/Reinitialize-NLog-configuration
NLog 4.6.7 引入了在 LoggingRules 中使用布局逻辑的功能。因此,您可以拥有一个带有日志记录规则的 NLog.config,这些规则可以在运行时轻松调整:
<nlog>
<variable name='myLevel' value='Warn'/>
<rules>
<logger minLevel='${var:myLevel}' writeTo="your-target-name" />
</rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)
然后可以myLevel在运行时更新:
LogManager.Configuration.Variables["myLevel"] = "Debug";
LogManager.ReconfigExistingLoggers();
Run Code Online (Sandbox Code Playgroud)
另请参阅: https: //github.com/NLog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules
我看到了一次启用一个级别的答案,但是如果您想镜像 NLog.config 文件的行为,其中设置单个级别也启用所有更高优先级的级别(并禁用较低优先级的级别),您需要启用一系列使用SetLoggingLevels这样的级别:
NLog.config:
<rules>
<logger name="*" minlevel="Trace" writeTo="your-target-name" />
</rules>
Run Code Online (Sandbox Code Playgroud)
代码:
public static void SetLogLevel(LogLevel newLogLevel)
{
foreach (var rule in LogManager.Configuration.LoggingRules)
{
foreach (var target in rule.Targets)
{
if (target.Name == "your-target-name")
{
rule.SetLoggingLevels(newLogLevel, LogLevel.Fatal);
}
}
}
LogManager.ReconfigExistingLoggers();
}
Run Code Online (Sandbox Code Playgroud)
从 NLog 文档:
Run Code Online (Sandbox Code Playgroud)public void SetLoggingLevels(LogLevel minLevel, LogLevel maxLevel);启用记录 (included)
minLevel和 之间的级别maxLevel。所有其他级别将被禁用。
| 归档时间: |
|
| 查看次数: |
28883 次 |
| 最近记录: |