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

查看:71
本文介绍了以编程方式重新配置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.

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

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启用跟踪级别的日志记录,但是显然您可以做任何您想做的事情,包括添加新的Targets/Rules或用编程生成的LoggerConfiguration完全替换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天全站免登陆