Config Transformation on NLog does not work

Tamas Ionut picture Tamas Ionut · Mar 16, 2014 · Viewed 14.9k times · Source

I have a web project (ASP.NET MVC 4 project) that has a number of configurations stored in Web.Config and in NLog.config files.

I have several publish profiles PublishProfile1, PublishProfile2 etc. When using a publish profile to deploy my web project to a server, I want to change a number of configs from both config files after deploy (some App Settings in Web.config and some values in NLog.config).

I have followed the steps from here and it works perfectly for changing the settings in Web.Config (e.g. the transformations from Web.PublishProfile1.Config are respected).

This is my NLog.PublishProfile1.Config transformation file:

<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">
  <nlog  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets >
      <target xsi:type="File"
              name="tracelog"
              fileName="NEW_VALUE_HERE" layout="${longdate} [${threadname}::${threadid}] ${pad:padding=5:inner=${level:uppercase=true}} ${logger} - ${message}"
              xdt:Transform="Replace"  xdt:Locator="Match(name)" />
    </targets>
  </nlog>
</nlog>

The problem is that I have the same transforms in NLog.PublishProfile1.config but these transformations are not applied after deploy as well.

Does anyone has a clue on why this transformation does not work for NLog.config but works ok for Web.config on a publish profile?

Answer

M&#225;rio Meyrelles picture Mário Meyrelles · Jun 3, 2014

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>