1

I am working on a ASP.Net web API 2 project and have configured nlog to write application logs to a database.

Since we have 4 environments (local, testing, staging, prod) I need to write logs to different databases.

So, in my web.config I have :

<configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>

Also, I have a Nlog.config file:

<?xml version="1.0" encoding="utf-8"?>
 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
  <target name="ExceptionLog" type="Database">
    <connectionString>
       // DbConnectionString
    </connectionString>
    <commandText>
       // INSERT COMMAND
    </commandText>
       //   Parameters
  </target>  

What I need is a different connection string for each environment. Or perhaps a bunch of files like NLog.Staging.config, NLog.Testing.config etc.

Please advise.

SJMan
  • 1,547
  • 2
  • 14
  • 37
  • Why do you have a config section *and* a separate file? We here have 2 config files: NLog.Release.config and NLog.Debug.config and renaming them accordingly dependend on the build target. That's good enough for our requirements. I guess there are more sophisticated ways. – Fildor Aug 22 '17 at 08:06
  • @Fildor you mean simply create a file for a solution configuration (Nlog.Staging.config and NLog.Release.config) and it would work ?? – SJMan Aug 22 '17 at 08:52
  • Suppose you configure NLog to use NLog.config as configuration source. Then you can provide NLog.Staging.config, NLog.Release.config etc Depending on your deploy target, you then will have to rename the according file to NLog.config which can easily be done by some script. Depends on your deployment tool chain. In my solution, I simply use a post-build statement to copy and rename the appropriate config to the bin folder depending on the build target. – Fildor Aug 22 '17 at 08:56
  • Mind that I do not write that as an answer because that's just what fits our needs and works. There may be as well much better solutions to do this. – Fildor Aug 22 '17 at 09:00

1 Answers1

0

Your question is basically about config files in general. Take a look at SlowCheetah. This will help transform any config file to different layouts based on VS configuration.

We currently have 3 different configurations within Visual Studio, Debug, Staging and Release. Depending on which configuration we compile for, SlowCheetah will transform the Web.Config and NLog.config files to that configuration by transforming lines or whole sections.

JAZ
  • 1,050
  • 6
  • 15