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

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

我有一个 Class 和一个包含路径的 static 字段。

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

如何在 NLog.Config 文件中使用此路径来指定目标的文件名?

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

如有任何帮助,我们将不胜感激。

基本上你需要从代码配置NLog。有关详细信息和示例代码,请参阅 offical documentation

更新

正如 Julian 指出的,您还可以在配置中使用变量 XML。详情可见here.

示例

配置文件:

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

代码:

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