54

My NLog targets is like this:

<targets>
  <target xsi:type="Console" name="console" 
    layout="${longdate}|${level}|${message}" />
  <target xsi:type="File" name="ErrorLog" fileName="${basedir}/error.txt"
          layout="${longdate}
          Trace: ${stacktrace} 
          ${message}" />
  <target xsi:type="File" name="AccessLog" fileName="${basedir}/access.txt"
          layout="${shortdate} | ${message}" />
</targets>

But this causes problems if the user isn't an admin on their machine, because they will not have write access to "Program Files". How can I get something like %AppData% to NLog instead of BaseDir?

Julian
  • 33,915
  • 22
  • 119
  • 174
Malfist
  • 31,179
  • 61
  • 182
  • 269

5 Answers5

93

You're looking for the NLog special folders.

Example:

...fileName="${specialfolder:folder=ApplicationData}/Program/file.txt"...
Otiel
  • 18,404
  • 16
  • 78
  • 126
Oren Mazor
  • 4,437
  • 2
  • 29
  • 28
13

Oren's answer should be the right answer. However, for the life of me I couldn't get it to work with my .NET 4.0 website using nLog 2.0.0.0. I ended up using simply

fileName="${basedir}app_data\logs\${shortdate}.log" 
Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
bkaid
  • 51,465
  • 22
  • 112
  • 128
  • The question title uses "AppData" but I don't think {..ApplicationData} is mapped to App_Data, I'd have to read the source to double check. – Chris S Jun 28 '13 at 20:20
  • 2
    This question and accepted answer is about the Windows special folder %appdata%, typically C:\Users\\AppData\Roaming. It seems there is no special variable for the ASP.net App_Data folder. – angularsen Jun 26 '15 at 23:24
7

${specialfolder:ApplicationData} also works

Sylar
  • 71
  • 1
  • 1
1

The previous answers helped solve the problem I was having, but a couple of years later and the solution is now somewhat different under v4.3. The directory and filename are combined with the path.

@theGecko's link is still current for the syntax, but the page is deficient of an example:

https://github.com/nlog/NLog/wiki/Special-Folder-Layout-Renderer

The following example would write the file myLog.log to the current users application data roaming directory C:\USers\current.user\AppData\Roaming\My\Path\Somewhere:

fileName="${specialfolder:dir=My/Path/Somewhere/:file=myFile.log:folder=ApplicationData}"
Hooligancat
  • 3,588
  • 1
  • 37
  • 55
1

For logging to the project directory:

While the previous answers work for the original question, searching for how to log to the project APP_DATA directory leads to this question. And while bkaid's answer works for ASP.NET and for using the APP_DATA folder specifically, for .NET Core and .NET 5 the solution is a bit different, because that motif has been abandoned in favor of defining a wwwroot folder for only those things which should be served, and the remainder being private. The answer for .NET Core/5, then, is to write to the solution root directory:

  1. First, ensure the NLog.Web.AspNetCore assembly is added to nlog.config:
    <extensions>
        <add assembly="NLog.Web.AspNetCore"/>
    </extensions>
    
  2. Then use one of the layout renderers provided by that extension, in this case ${aspnet-appbasepath} which references the solution root directory:
    <targets>
        <target name="file"
                type="File"
                xsi:type="File"
                fileName="${aspnet-appbasepath}/log/${shortdate}.log"
                layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}"/>
    </targets>
    

This will write the file to <solution folder>/log/2021-07-01.log, which will never be served by the public-facing website. Other layout renderers provided by this assembly are listed on the NLog website.

Jonathan E. Landrum
  • 2,748
  • 4
  • 30
  • 46