Search code examples
visual-studioweb-configtransformationxdt

How do I transform this element in my Web.config?


I've been googling for about half the day here and I'm still just as clueless as when I started on this.

Here's the relevant portion of my Web.config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
    <!--
    For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.

    The following attributes can be set on the <httpRuntime> tag.
      <system.Web>
        <httpRuntime targetFramework="4.5.2"/>
      </system.Web>
  -->
    <configSections>
        <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
        <sectionGroup name="applicationSettings"
        type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="DasExternalFileTransfer.Properties.Settings"
              type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
        </sectionGroup>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework"
            type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            requirePermission="false"/>
    </configSections>
    <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">
        <targets>
            <target name="logfile" xsi:type="File" fileName="" layout="${longdate} ${callsite} ${level} ${message}"/>
        </targets>
        <rules>
            <logger name="*" minlevel="Debug" writeTo="logfile"/>
        </rules>
    </nlog>

I want to replace the fileName value under configuration/nlog/targets/target. I have tried half a dozen different things in my Web.Dev.config. The latest would be

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <target xdt:Transform="Replace" xdt:Locator="XPath(/configuration/nlog/targets)"  />
    <target xdt:Transform="Replace" fileName="\\newlocation.ad\location1" />
    <system.web>
        <compilation xdt:Transform="RemoveAttributes(debug)" />
    </system.web>
</configuration>

To this point I have seen no effect when I preview my transform in Visual Studio.

I suspect that I am not defining the element in a way that the process can locate it, but I am not sure.

Could someone please help me figure out how to define the element properly so that I can replace the value with a new one? Or am I simply taking the wrong approach?

OK, THINK I'VE FIGURED IT OUT

It appears that the namespaces were tripping me up.

I modified this section of my Web.config

<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">
    <targets>
        <target name="logfile" xsi:type="File" fileName="" layout="${longdate} ${callsite} ${level} ${message}"/>
    </targets>
    <rules>
        <logger name="*" minlevel="Debug" writeTo="logfile"/>
    </rules>
</nlog>

Note, I removed xmlns="http://www.nlog-project.org/schemas/NLog.xsd". Then I modified my Web.Dev.config to

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">
        <targets>
            <target xdt:Transform="SetAttributes(fileName)" xdt:Locator="Match(name)" name="logfile" fileName="\\newlocation.ad\location1" />
        </targets>
    </nlog>
    <system.web>
        <compilation xdt:Transform="RemoveAttributes(debug)" />
    </system.web>
</configuration>

Then, when I preview the transform, I see this as the result <target name="logfile" xsi:type="File" fileName="\\newlocation.ad\location1" layout="${longdate} ${callsite} ${level} ${message}"/>. I just hope now that the removal of the namespace will not adversely affect NLog, but I'll be testing to determine that.


Solution

  • It appears that the namespaces were tripping me up.

    I modified this section of my Web.config

    <nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">
        <targets>
            <target name="logfile" xsi:type="File" fileName="" layout="${longdate} ${callsite} ${level} ${message}"/>
        </targets>
        <rules>
            <logger name="*" minlevel="Debug" writeTo="logfile"/>
        </rules>
    </nlog>
    

    Note, I removed xmlns="http://www.nlog-project.org/schemas/NLog.xsd". Then I modified my Web.Dev.config to

    <?xml version="1.0" encoding="utf-8"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">
            <targets>
                <target xdt:Transform="SetAttributes(fileName)" xdt:Locator="Match(name)" name="logfile" fileName="\\newlocation.ad\location1" />
            </targets>
        </nlog>
        <system.web>
            <compilation xdt:Transform="RemoveAttributes(debug)" />
        </system.web>
    </configuration>
    

    Then, when I preview the transform, I see this as the result <target name="logfile" xsi:type="File" fileName="\\newlocation.ad\location1" layout="${longdate} ${callsite} ${level} ${message}"/>. I just hope now that the removal of the namespace will not adversely affect NLog, but I'll be testing to determine that.