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

标签 c# path config nlog

我有一个包含路径的 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";

关于c# - 如何在 NLog.Config 中使用来自不同类的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38977847/

相关文章:

python - 为什么使用 .conf 文件而不是 config.py 文件?

多级数组解析PHP Config.ini文件

c# - 具有插件架构的应用程序中的单元测试插件

c# - 参数字典包含不可为空类型 'IDnumber' 的参数 'System.Int32' 的空条目

c# - 随机整数中最可能的位

c# - 在 .NET 中序列化对象时省略所有 xsi 和 xsd 命名空间?

svg - 获取 svg 路径上点的 y 坐标

python - 如何使用文件路径作为函数参数

c++ - 用于管理路径/URL 的库

python - 在 Python 中创建游戏配置/选项 (config.cfg) 文件