using AppData location in NLog

ε祈祈猫儿з 提交于 2019-11-27 02:07:00

问题


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?


回答1:


You're looking for the NLog special folders.

Example:

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



回答2:


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" 



回答3:


${specialfolder:ApplicationData} also works




回答4:


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


来源:https://stackoverflow.com/questions/2000189/using-appdata-location-in-nlog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!