2

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.

Julian
  • 33,915
  • 22
  • 119
  • 174
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76

1 Answers1

9

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";
Andras Toth
  • 576
  • 4
  • 11
  • thank you for the tip, but I cannot get the target to use the path. I created a Variable, set it in the code, but the target does not save the message into the file – Mong Zhu Aug 16 '16 at 15:44
  • 1
    Just to note, there is no need to move from the XML config to C# in this case. – Julian Aug 16 '16 at 21:28
  • Note that LogManager is a static class from the NLog namespace. I was trying to find it via DI which obviously won't work. – LuvForAirplanes Aug 15 '20 at 14:09