Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using AppData location in NLog

Tags:

nlog

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?

like image 625
Malfist Avatar asked Jan 04 '10 15:01

Malfist


4 Answers

You're looking for the NLog special folders.

Example:

...fileName="${specialfolder:folder=ApplicationData}/Program/file.txt"...
like image 171
Oren Mazor Avatar answered Nov 18 '22 01:11

Oren Mazor


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" 
like image 40
bkaid Avatar answered Nov 18 '22 01:11

bkaid


${specialfolder:ApplicationData} also works

like image 7
Sylar Avatar answered Nov 18 '22 01:11

Sylar


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}"
like image 1
Hooligancat Avatar answered Nov 18 '22 03:11

Hooligancat