简体   繁体   English

如何在NLog.Config中使用来自不同类的变量?

[英]How to use variables from different class in NLog.Config?

I have a Class with a static field which holds a path. 我有一个带有包含路径的static字段的类。

public static class PfadSammlung
{
    public static string Path_Example = @"C:\temp";
}

How could I use this path in the NLog.Config file to specify the file name for the target? 如何在NLog.Config文件中使用此路径来指定目标的文件名?

<targets>
    <target xsi:type="File"
        name ="processInfo"
        fileName="C:\temp\ProcessInfoLog.log"
        layout="${longdate}  |  ProcessInfo: ${message}"
    />
</targets>

Any help would be greatly appreciated. 任何帮助将不胜感激。

Basically you need to configure NLog from code. 基本上,您需要从代码配置NLog。 See the offical documentation for details and sample code. 有关详细信息和示例代码,请参见官方文档

Update 更新资料

As Julian pointed out, you can also use variables in you config XML. 正如朱利安指出的那样,您还可以在配置XML中使用变量。 Details can be found here . 详细信息可以在这里找到。

Sample 样品

Config file: 配置文件:

<variable name="logDirectory" value="c:\temp" />
<targets>
    <target xsi:type="File"
        name ="processInfo"
        fileName="${var:logDirectory}"
        layout="${longdate}  |  ProcessInfo: ${message}"
    />
</targets>

Code: 码:

LogManager.Configuration.Variables["logDirectory"] = @"c:\temp\logs";

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

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