Mong Zhu Mong Zhu - 3 months ago 23
C# Question

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

I have a Class with a

static
field which holds a path.

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?

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


Any help would be greatly appreciated.

Answer

Basically you need to configure NLog from code. See the offical documentation for details and sample code.

Update

As Julian pointed out, you can also use variables in you config 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";
Comments