52
votes

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?

5

5 Answers

90
votes

You're looking for the NLog special folders.

Example:

...fileName="${specialfolder:folder=ApplicationData}/Program/file.txt"...
14
votes

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" 
7
votes

${specialfolder:ApplicationData} also works

1
votes

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}"
1
votes

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.