Config Transformation on NLog does not work

I've managed it without moving all configuration into web.config or app.config. (There is nothing, connected with nlog in web/app.config at all).

1) Using this article: applying msbuild config transformations to any config file I've created all transformations, which are necessary and added transformation task.

2)Then I've checked if there are any other tasks, which do similar things. In my case there was one, which had been created by SlowCheetah(VS extension to automatically add transformations). I've removed it - and everything became ok. (SlowCheetah can restore its settings on next build, so it is better to remove it or suppress its transformation task)

3) My transform files look like this one:

<?xml version="1.0"?>
<!-- For more information on using app.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xmlns="http://www.nlog-project.org/schemas/NLog.xsd">
  <targets>
    <target xsi:type="Database" name="DbLogging"
          connectionString=".\SQLEXPRESS;Initial Catalog=xxxxx;User ID=xxxx;Pwd=xxxx;" xdt:Transform="SetAttributes(connectionString)" xdt:Locator="Match(name)">
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Debug" writeTo="DbLogging" />
    <logger name="Namespace.*" minlevel="Debug" writeTo="DbLogging" xdt:Transform="SetAttributes(writeTo)" xdt:Locator="Match(name)" />
  </rules>
</nlog>

To solve this issue I had to:

1) Avoid the use of nlog.config

2) Create nlog section inside web.config and move the contents of nlog.config to web.config to be able the use the web.config transformation feature with one file. To further instructions please take a look at: NLog configuration instructions

3) Remove xmlns attributes from the nlog node. There seems to be a bug that messes everything during the web.config transformation. You can safely remove the following nodes:

xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

4) I couldn't find a way to transform just a single target under nlog/targets node. To change the connection string of my logger, I had to copy the whole xml node, using a xdt:Transform="Replace" on the parent node like the following:

<nlog
      throwExceptions="true"
      internalLogLevel="Trace"
      internalLogFile="..\..\..\Logs\nlog-app.log" 
      xdt:Transform="Replace">
<!--(copy and paste all nlog configuration here)-->
</nlog>

It appears that I am quite late on this, but I have found that it is necessary for the NLog.config file to have the following:

<?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"
        xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">
...
...
</nlog>

And the NLogTransform.config file (or whatever name it may have) to have the following:

<?xml version="1.0"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
...
...
</nlog>

Here is some sample code to test the NLog transformation:

var tmpConfigFile = new FileInfo("C:\\NLogs\\NLog.config");
var transformFile = new FileInfo("C:\\Transforms\\NLogTransform.config");

if (transformFile.Exists) {
    var xmlTransformableDocument = new XmlTransformableDocument();
    xmlTransformableDocument.Load(tmpConfigFile.FullName);
    var xmlTransformation = new XmlTransformation(transformFile.FullName);
    xmlTransformation.Apply(xmlTransformableDocument);
    xmlTransformableDocument.Save(tmpConfigFile.FullName);
}

I got this working by, in transformation config, include the xmlns:xsi namespace definied in web.config for the nlog section.

<nlog xmlns:xsi="...">
    <variable name="..." value="..." xdt:Transform="Replace" xdt:Locator="Match(name)" />
</nlog>