using AppData location in NLog
Asked Answered
G

5

55

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?

Grooms answered 4/1, 2010 at 15:25 Comment(0)
I
94

You're looking for the NLog special folders.

Example:

...fileName="${specialfolder:folder=ApplicationData}/Program/file.txt"...
Innominate answered 4/1, 2010 at 15:55 Comment(5)
Yeah, but how do I use it? I've already tried, fileName="${ApplicationData}/RentalEase/access.txt" and it fails silently.Grooms
I don't remember off the top of my head, but it should look like this: ${specialfolder:folder=SPECIALFOLDERNAME}Innominate
${specialfolder:folder=ApplicationData}Grooms
I missread that, thought you had a '.' between specialfolder and folder instead of a colonGrooms
If it fails silently try adding <nlog throwExceptions="true" /> - ref: github.com/nlog/nlog/wiki/…Leolaleoline
T
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" 
Trenchant answered 24/3, 2011 at 22:50 Comment(2)
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.Outguess
This question and accepted answer is about the Windows special folder %appdata%, typically C:\Users\<user>\AppData\Roaming. It seems there is no special variable for the ASP.net App_Data folder.Kurtiskurtosis
G
7

${specialfolder:ApplicationData} also works

Galanti answered 22/2, 2011 at 6:2 Comment(0)
C
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}"
Concern answered 8/7, 2016 at 23:4 Comment(0)
L
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.

Lippi answered 1/7, 2021 at 23:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.