Question

I'm trying to insert an NLog custom config section into my Web.config using this XDT section:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" throwExceptions="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xdt:Transform="InsertIfMissing" >
    <targets>
        <target xsi:type="File" name="logfile" fileName="H:\testLog.txt" layout="${longdate} ${uppercase:${level}} ${message}" />
    </targets>
    <rules>
        <logger name="*" minlevel="Trace" writeTo="logfile" />
    </rules>
</nlog>

When I run the XDT transform, my Web.Debug.config contains:

<nlog throwExceptions="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd">
    <targets>
        <target d4p1:type="File" name="logfile" fileName="H:\testLog.txt" layout="${longdate} ${uppercase:${level}} ${message}" xmlns:d4p1="http://www.w3.org/2001/XMLSchema-instance" />
    </targets>
    <rules>
        <logger name="*" minlevel="Trace" writeTo="logfile" />
    </rules>
</nlog>

Typically, a namespace prefix is arbitrary, so transforming xsi to d4p1 would cause no issues.

However, I get a runtime exception in my application from NLog when I use d4p1. Manually changing the instances of d4p1 to xsi fixes the issue, but it subverts the utility of config transformation for me if the user needs to manually alter the file afterward.

Is there a way to preserve namespace prefixes using XDT?

Was it helpful?

Solution

We had exactly the same issue. I'm not sure why it suddenly started happening with a particular project, but the solution for us was to add the xsi namespace to the top level of the original configuration file (ie the base file the transformations work on). So...

<configuration>

... would become...

<configuration xmlns:xsi="http://www.nlog-project.org/schemas/NLog.xsd">

This did the trick.

An alternative approach that also worked was to apply the transforms on child elements of the nlog element.

Hope that helps someone.

OTHER TIPS

I started to see this problem when I moved my xdt:Transform attribute from the target and rule tags to nlog. Moving them back to the original tags like this solved it:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" throwExceptions="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets xdt:Transform="InsertIfMissing">
    <target xsi:type="File" name="logfile" fileName="H:\testLog.txt" layout="${longdate} ${uppercase:${level}} ${message}" />
  </targets>
  <rules xdt:Transform="InsertIfMissing">
    <logger name="*" minlevel="Trace" writeTo="logfile" />
  </rules>
</nlog>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top