Search code examples
app-confignlogxdt-transform

app.config transformation - nlog rules logger replace not works


in App.config I have nlog section:

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

    <variable name="logDirectory" value="C:\AppsLog" />

    <targets>
      <target name="LogDebugTarget" ... />
    </targets>

    <rules>
      <logger name="LogDebug" minlevel="Debug" writeTo="LogDebugTarget" />
      <logger name="LogInfo" minlevel="Info" writeTo="LogInfoTarget" />
      <logger name="LogError" minlevel="Error" writeTo="LogErrorTarget,LogErrorTargetEmail" />
    </rules>

  </nlog>

in App.debug.config I want to replace:

<logger name="LogError" minlevel="Error" writeTo="LogErrorTarget,LogErrorTargetEmail" />

with:

<logger name="LogError" minlevel="Error" writeTo="LogErrorTarget" />

but left rest nodes untouched:

  <logger name="LogDebug" minlevel="Debug" writeTo="LogDebugTarget" />
  <logger name="LogInfo" minlevel="Info" writeTo="LogInfoTarget" />

so in App.debug.config I have written:

  <nlog>
    <rules>
      <add xdt:Transform="Replace" xdt:Locator="Match(name)" name="LogError" minlevel="Error" writeTo="LogErrorTarget" />
    </rules>
  </nlog>

but it does not replace anything - just left whole content of oryginal <nlog> node

What I am doing wrong?


Solution

  • You have a few mistakes: you lost namespaces in the transform file and wrote incorrect xml tag that you want to swap. So it's very easy to fix:

    <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">
      <rules>
        <logger xdt:Transform="Replace" xdt:Locator="Match(name)" name="LogError" minlevel="Error" writeTo="LogErrorTarget" />
      </rules>
    </nlog>