简体   繁体   English

NLOG:如何使用变量来指定目标

[英]NLOG: how to use variable to specify target

Is it possible to use a variable to specify a specific target?是否可以使用变量来指定特定目标?

I want to switch between database and filelogging depending on the environment i'm running my application on.我想根据运行应用程序的环境在数据库和文件记录之间切换。

This however does not seem to work:然而,这似乎不起作用:

<logger name="MyLogger" minlevel="Warn" writeTo="${var:myTargetName}" />

In my application startup I have (error occurs on first line):在我的应用程序启动中,我有(第一行发生错误):

var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); 
var config = LoadConfiguration();
NLog.LogManager.Configuration.Variables["myTargetName"] = config.GetSection("LogTargets:TargetName").Value;   
NLog.LogManager.KeepVariablesOnReload = true;
NLog.LogManager.Configuration.Reload();

When startin the application the following exception is thrown:启动应用程序时抛出以下异常:

"Target ${var:myTargetName} not found."

I guess the variables are not available when parsing the config file.我猜解析配置文件时变量不可用。 How can I set the variables so NLOG uses them while parsing the config-file?如何设置变量以便 NLOG 在解析配置文件时使用它们? Is it possible to use a variable to set an attribute value?是否可以使用变量来设置属性值? Or is this not supported by NLOG?或者这不是 NLOG 支持的吗?

Note: I do have another variable in the NLOG config which does work注意:我在 NLOG 配置中有另一个变量,它确实有效

<target xsi:type="Database" name="databaseTarget">
  <connectionString>${var:connectionString}</connectionString>
  ...........
</target>

I guess the loggers are checked once on startup and the actual target is evaluated when a logevent occurs.我猜记录器在启动时检查一次,并在发生日志事件时评估实际目标。

Update: got it working without variable in config file.更新:让它在配置文件中没有变量的情况下工作。 I had to remove the logger from the config and create it through code我不得不从配置中删除记录器并通过代码创建它

var myCustomTarget = NLog.LogManager.Configuration.FindTargetByName(config.GetSection("LogTargets:TargetName").Value);
NLog.LogManager.Configuration.AddRuleForAllLevels(myCustomTarget , "MyLogger", true);

One way to do it is to have a different Web.config file based on the environment and there you will change the connection string.一种方法是根据环境使用不同的 Web.config 文件,然后您将更改连接字符串。 That is the method I am using.这就是我正在使用的方法。

Also NLOG is initialized once the application starts thus you cannot change what's written in the NLOG.config NLOG 也会在应用程序启动后初始化,因此您无法更改 NLOG.config 中写入的内容

Setting variables via NLog.LogManager.Configuration.Variables does not work for all attributes in nlog.config file.通过 NLog.LogManager.Configuration.Variables 设置变量不适用于 nlog.config 文件中的所有属性。 I don't know why, but that's a known problem and that is unfortunate how it works.我不知道为什么,但这是一个已知问题,不幸的是它是如何工作的。 But there is a simple workaround, here is an example how I solved this problem for the attribute connectionString of a target.但是有一个简单的解决方法,这里有一个例子,我如何为目标的属性 connectionString 解决这个问题。

<target xsi:type="Database" name="tDatabase"
            dbProvider="Microsoft.Data.Sqlite.SqliteConnection, Microsoft.Data.Sqlite"
            connectionString="Data Source =.\${environment:DATABASE_FILE_NAME};"
            commandText="INSERT INTO ...">
      <parameter name="@MachineName" layout="${machinename}" />
      ...
</target>

You can than simple set the environment variable in your code like this您可以像这样在代码中简单地设置环境变量

Environment.SetEnvironmentVariable("DATABASE_FILE_NAME", "foo.db");

So you just use ${ environment :DATABASE_FILE_NAME} and所以你只需使用${ environment :DATABASE_FILE_NAME}

Environment.SetEnvironmentVariable("DATABASE_FILE_NAME", "foo.db");

instead of ${ var :DATABASE_FILE_NAME} and而不是${ var :DATABASE_FILE_NAME}

NLog.LogManager.Configuration.Variables["DATABASE_FILE_NAME"] = "foo.db";

NLog 4.6.7 makes it easier to update the logging-rules at runtime like this: NLog 4.6.7 可以更轻松地在运行时更新日志记录规则,如下所示:

<nlog>
    <variable name="fileMinLevel" value="Off" />
    <variable name="databaseMinLevel" value="Off" />
    <rules>
      <logger minLevel="${var:fileMinLevel}" writeTo="fileTarget" />
      <logger minLevel="${var:databaseMinLevel}" writeTo="databaseTarget" />
    </rules>
</nlog>

Then you can do this:然后你可以这样做:

if (IsDevelopment())
       LogManager.Configuration.Variables["fileMinLevel"] = "Debug";
else
       LogManager.Configuration.Variables["databaseMinLevel"] = "Debug";
LogManager.ReconfigExistingLoggers();

See also: https://github.com/NLog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules另见: https : //github.com/NLog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM