NLOG配置API:存储在变量中使用布局 [英] NLog Configuration API: Using Layouts stored in variables

查看:557
本文介绍了NLOG配置API:存储在变量中使用布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序创建一个日志,应用程序本身,所以当它被激活记录,并发生了什么事在应用程序级别。

My app creates a log for the application itself, so record when it was activated, and what happened at an application level.

该应用程序是围绕配置文件为中心 - 用户加载的配置文件,它告诉应用程序在哪里/何时/什么/如何。所以,我也想为每个配置文件创建一个日志,每个配置文件运行时录制的进度。

The application is centered around 'profiles' - the user loads a profile which tells the application where/when/what/how. So I also want to create a log for each profile, to record the progress each time the profile is run.

没有问题,那么远,但我想要的配置文件日志被存储旁边的配置文件本身,所以这意味着我需要动态配置NLOG,这样我就可以告诉它在运行时fileTarget路径。

No problems so far... except that I want the profile log to be stored alongside the profile itself, so this means I need to configure NLog dynamically, so I can tell it the fileTarget path at runtime.

不过,我也想存储的标准布局,我想用,如NLog.config变量;这似乎是从我读了普通不过的做法。

However, I was also wanting to store the standard layouts I want to use, as variables in NLog.config; this seems to be a common enough approach from what I have read.

然而,在下面的行

fileTarget.Layout = "${myLayout}"

...我得到一个ArgumentException:

...I get an ArgumentException:

LayoutRenderer cannot be found: 'myLayout'

目前,我的布局变量只是:

At the moment, my layout variable is simply:

<variable name="myLayout" value="${message}" />

难道你不能使用变量通过API来指定布局的情况下?我会感到惊讶,如果是这样的话。还是我错了地方?

Is it a case that you can't use variables to specify layout through the API? I would be surprised if that was the case. Or have I gone wrong somewhere?

解决的办法很简单 - 我可以填充 fileTarget.Layout 与手动指定的布局,但尽管如此,我敏锐地发现,如果A计划可以工作。

The solution is simple enough - I can populate fileTarget.Layout with a manually-specified layout, but nevertheless, I'm keen to find out if Plan A could work.

推荐答案

这是不可能的,如果你使用的是从NLOG的网站已编译的二进制,这主要是因为NLOG不公开的API来访问&LT ;变&GT; 元素

It's not possible if you're using a compiled binary from NLog's site, mainly because NLog doesn't expose an API for accessing <variable> elements.

您可以建议笔者添加这种能力,或者如果你真的热衷于具有此功能,然后下​​载并修改源$ C ​​$ C。 文件私人字符串ExpandVariables(字符串输入) XmlLoggingConfiguration.cs 是什么,你会需要公开。

You could suggest the author to add this ability, OR if you're really keen about having this ability, then download and modify the source code. private string ExpandVariables(string input) in file XmlLoggingConfiguration.cs is what you'll need to expose.

祝你好运。

这篇关于NLOG配置API:存储在变量中使用布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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