是否可以动态告知NLog要登录到哪个目标? [英] Is it possible to tell dynamically NLog which target to log to?

查看:43
本文介绍了是否可以动态告知NLog要登录到哪个目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在WPF应用程序中实现 AppDomain.FirstChanceException ,以便有选择地记录发生,已处理或未发生的每个异常.我不希望将这些异常记录到为NLog配置的目标中.在调用 Logger.Error (或任何 Logger 方法)时,是否有可能使NLog仅记录到一个特定目标?

I want to implement the AppDomain.FirstChanceException in my WPF app, for optionally logging every exception that occurs, handled or not. I don't wish to log these exceptions to the targets I have configured for NLog. Is is possible, around the time of calling Logger.Error (or whatever Logger method) to make NLog only log to one particular target?

推荐答案

可能,但这不是一个好主意,因为:

It's possible, but this is not a good idea because:

  1. 这不是线程安全的.
  2. 将重新配置指定工厂的所有记录器.

配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets async="true">
    <target name="file1" xsi:type="File" fileName="${basedir}/log1.txt" />
    <target name="file2" xsi:type="File" fileName="${basedir}/log2.txt" />
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" writeTo="file1,file2" />
  </rules>
</nlog>

全局工厂配置示例:

var logger = LogManager.GetCurrentClassLogger();

logger.Error("Original Configuration");

var configOrig = LogManager.Configuration;
var configTemp = LogManager.Configuration.Reload();
var rule = configTemp.LoggingRules.First(r => r.NameMatches("*"));
var target = configTemp.FindTargetByName("file2");

rule.Targets.Remove(target);

LogManager.Configuration = configTemp;
logger.Error("Temporary Configuration");

LogManager.Configuration = configOrig;
logger.Error("Original Configuration");

用户创建的出厂配置示例:

Example of a user created factory configuration:

var factory = new LogFactory(LogManager.Configuration);
var logger = factory.GetCurrentClassLogger();

logger.Error("Original Configuration");

var config = factory.Configuration;
var rule = config.LoggingRules.First(r => r.NameMatches("*"));
var target = config.FindTargetByName("file2");

rule.Targets.Remove(target);
factory.ReconfigExistingLoggers();
logger.Error("Temporary Configuration");

rule.Targets.Add(target);
factory.ReconfigExistingLoggers();
logger.Error("Original Configuration");

log1.txt

2015-03-16 21:46:04.5685|ERROR|ConsoleApplication.Program|Original Configuration
2015-03-16 21:46:04.5865|ERROR|ConsoleApplication.Program|Temporary Configuration
2015-03-16 21:46:04.5865|ERROR|ConsoleApplication.Program|Original Configuration

log2.txt

2015-03-16 21:45:26.4238|ERROR|ConsoleApplication.Program|Original Configuration
2015-03-16 21:45:26.4588|ERROR|ConsoleApplication.Program|Original Configuration

解决方案

该问题的最佳解决方案是为每个工厂创建两个工厂和一个记录器.在订阅 AppDomain.FirstChanceException 之前,必须初始化工厂和记录器.否则,如果您有异步操作或其他线程,则会在线程安全方面遇到问题.

The best solution of the problem is to create two factories and one logger for each factory. The factories and loggers must be initialized before you subscribe to AppDomain.FirstChanceException. Otherwise, you will have problems with thread safety if you have async operations or other threads.

这篇关于是否可以动态告知NLog要登录到哪个目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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