以编程方式重新配置 NLog LoggingConfiguration 过滤器 [英] Reconfigure NLog LoggingConfiguration filters programmatically

查看:22
本文介绍了以编程方式重新配置 NLog LoggingConfiguration 过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用 NLog 包进行日志记录,但到目前为止它非常适合使用.

This is my first time using the NLog package for logging but so far its been great to work with.

在我的场景中,我需要以编程方式初始化我的 NLog LoggingConfiguration 设置,而不是通过更典型的配置文件场景.我已经对此进行了测试,并默认按照我想要的方式工作.但是我将如何在运行时以编程方式修改我的设置?

In my scenario I need to initialize my NLog LoggingConfiguration settings programmatically rather than thru the more typical config file scenario. I've tested this and got it all working the way I want it by default. But how would I go about modifying my settings programmatically at run-time?

可能这里最常见的情况是应用程序的日志记录级别默认设置为 ERROR,但在特定模块中出现错误,我想将日志记录级别切换为更详细的以跟踪错误.

Probably the most common scenario here is one where the application's logging level is set to ERROR by default but a bug arises in a particular module that I want to switch the logging level to be much more verbose to track down the error.

我想编写一个小网络界面,以便我可以在运行时轻松调整这些设置,但我想确保我对此采取了正确的方法.

I'd like to write a little web interface so I can easily tweak these settings at runtime but I want to make sure I am taking the right approach with this.

推荐答案

我知道我迟到了一年,但我遇到了类似的情况,我需要向用户提供记录器的动态控制.幸运的是,这对于 nlog 来说相对容易.在这里,我只是为已创建的 Logger 启用跟踪级别的日志记录,但显然您可以做任何您想做的事情,包括添加新的目标/规则,或者只是将 LoggerConfiguration 完全替换为您以编程方式生成的.

I know I'm a year late but I just ran into similar scenario where I needed to provide dynamic control of the logger to users. Fortunately, this is relatively easy with nlog. Here I'm just enabling Trace level logging to an already created Logger but obviously you could do anything you wanted including adding new Targets/Rules or just completely replace the LoggerConfiguration with your programmatically generated one.

基本 ColoredConsole nlog.config:

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>

简单的控制台应用程序:

Simple console application:

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();
  }
}

如果您需要从进程外部更改日志记录配置并且必须以编程方式完成,我建议使用 WCF.您可以从您的应用程序或网站公开一个小型 WCF 服务,该服务提供重新配置 nlog 的方法.

If you need to alter the Logging configuration from outside the process and it has to be done programmatically, I would suggest going with WCF. You could expose a small WCF service from either your application or website that provides methods for reconfiguring nlog.

这篇关于以编程方式重新配置 NLog LoggingConfiguration 过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆