1

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?

Julian
  • 33,915
  • 22
  • 119
  • 174
Carlos Busca
  • 135
  • 8

2 Answers2

1

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

Julian
  • 33,915
  • 22
  • 119
  • 174
1

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

Carlos Busca
  • 135
  • 8