Use app data directory in NLog file path

Somu picture Somu · Nov 21, 2013 · Viewed 23.6k times · Source

How can I set the target in the NLog.config file to create the log file in the user app data directory?

Below is the desired location of the file.

C:\Users\userid\AppData\Local...\LogFile.txt

I tried setting the fileName in the following ways without any success.

  • target name="logfile" xsi:type="File" fileName="%APPDATA%/.../LogFile.txt"

  • target name="logfile" xsi:type="File" fileName="${APPDATA}/.../LogFile.txt"

  • target name="logfile" xsi:type="File" fileName="${%APPDATA%}/.../LogFile.txt"

Answer

Jacco Dieleman picture Jacco Dieleman · Nov 21, 2013

You can achieve that by creating a target like this:

<target name="logfile" xsi:type="File"
    fileName="${specialfolder:folder=ApplicationData}/LogFile.txt"/>

The documentation can be found here.

I hope this helps.