如何在Web.config中转换此元素?

时间:2019-11-21 22:37:04

标签: visual-studio web-config transformation xdt

我已经在这里呆了大约半天,但我还是一头雾水。

这是我的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>

我想替换fileName下的configuration/nlog/targets/target值。 我已经在Web.Dev.config中尝试了六种不同的方法。最新的是

<?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>

到目前为止,当我在Visual Studio中预览转换时,仍然看不到任何效果。

我怀疑我不是以过程可以找到它的方式定义元素,但是我不确定。

有人可以帮助我弄清楚如何正确定义元素,以便可以用新值替换该值吗?还是我只是采取了错误的方法?

好吧,我想出了答案

看来,这些命名空间让我感到震惊。

我修改了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>

注意,我删除了xmlns="http://www.nlog-project.org/schemas/NLog.xsd"。 然后我将Web.Dev.config修改为

<?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>

然后,当我预览转换时,我将其视为结果<target name="logfile" xsi:type="File" fileName="\\newlocation.ad\location1" layout="${longdate} ${callsite} ${level} ${message}"/>。 我只希望现在删除名称空间不会对NLog产生不利影响,但我将进行测试以确定这一点。

1 个答案:

答案 0 :(得分:0)

看来,这些命名空间让我感到震惊。

我修改了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>

注意,我删除了xmlns="http://www.nlog-project.org/schemas/NLog.xsd"。 然后我将Web.Dev.config修改为

<?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>

然后,当我预览转换时,我将其视为结果<target name="logfile" xsi:type="File" fileName="\\newlocation.ad\location1" layout="${longdate} ${callsite} ${level} ${message}"/>。 我现在只是希望删除名称空间不会对NLog产生不利影响,但是我将进行测试以确定这一点。