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

Mon*_*Zhu 2 c# config nlog path

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

public static class PfadSammlung
{
    public static string Path_Example = @"C:\temp";
}
Run Code Online (Sandbox Code Playgroud)

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

public static class PfadSammlung
{
    public static string Path_Example = @"C:\temp";
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激。

And*_*oth 7

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

更新

正如 Julian 指出的,您还可以在配置 XML 中使用变量。可在此处找到详细信息。

样本

配置文件:

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

代码:

LogManager.Configuration.Variables["logDirectory"] = @"c:\temp\logs";
Run Code Online (Sandbox Code Playgroud)