Variable substitution in nlog.config file


I have a nlog configuration file which contains a connection string like this:

My problem is that this connectionstring differs depending on the environment I am deploying to.
Is there a way to do variable substitution on non-web.config files?



Hi Peter

Yeah you can do them on app.config files too - or is it a different type of file extension?

This should help you


Thank for the quick reply.
Perhaps I am missing something here but according to the link below:

“Values are matched based on the key attribute for appSettings, and the name element for connectionStrings.”

The entry I have in my nlog.config file reads:

I.e. the entry is called variable and the attribute to be replaced is called value.

Will this work?


Best regards

Hi Peter,

Using that format you will have to use a the Substitute variables in files feature. So change the config line like this:
<variable name="NLogConnectionString" value="#{NLogConnectionString}"/> turn on the feature, and add ‘nlog.config’ to the list of files to substitute in.
Also add that variable to your project’s variables.


Hi Vanessa

Okay, I now see what the problem is. We are running version 2.0 and apparently this version does not support the “Substitute variables in files” feature.