简体   繁体   中英

Set ApplicationData folder on app.config and get it on Nlog.config

I'd like to set the path of NLog log file on app.config.

I know how to set the applicationData folder on NLog.config. It's like that:

<target xsi:type="File" 
    name="logFileCsv" 
    fileName="${specialfolder:folder=ApplicationData}/something/something.log">

And if I use the NLog.Extended I know how to get key values from app.config and use them on NLog.config, like that:

<target xsi:type="File" name="logFileCsv" fileName="${appsetting:name=LogPath">

And on app.config should be like this

<add key="LogPath" value="${APPDATA}/something/something.log" />

But it's not working the way I want. NLog is creating the log file on c:\\..\\${APPDATA}/something/something.log"

So, I'd like to know how to set the ApplicationData folder on app.config to be understandable by NLog?

Unfortunately the ${appsetting} won't evaluate the value.

As work around you can do:

<target fileName="${APPDATA}/${appsetting:name=LogPath}" />

note: wrapping in a ${replace} also won't work

I tried the answer of Julian , but it wasn't utterly successful:

Error FileTarget(Name=logFileCsv): Failed write to file '\\\\something\\something.log

However, with that help and workaround, I figure out how to do it: So in App.config just add the final path:

<add key="LogPath" value="\something\something.log"/>

and in NLog.Config I did this:

<target xsi:type="File" name="logFileCsv" fileName="${specialfolder:folder=ApplicationData}/${appsetting:name=LogPath}">

And now the log is written on the right path: C:\\Users\\user\\AppData\\Roaming\\something\\something.log

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM