Use app data directory in NLog file path

26,940

Solution 1

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.

Solution 2

The accepted answer doesn't create the log file in AppData\Local directory instead it creates the log file in AppData\Roaming directory. As asked by the OP to create the log file in AppData\Local you have to use LocalApplicationData in specialfolder path.

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

Solution 3

Just an addition to Jacco's answer, you can add a folder name to the path if you'd like the logs kept separately from other data that you might store in appdata, like this:

fileName="${specialfolder:folder=ApplicationData}/Logs/LogFile.txt"/>
Share:
26,940
Somu
Author by

Somu

Software engineer with an interest to explore news things in Life......:D

Updated on July 11, 2022

Comments

  • Somu
    Somu almost 2 years

    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"