【问题标题】:Transform that preserve namespace prefixes保留命名空间前缀的转换
【发布时间】:2013-09-26 08:09:54
【问题描述】:

我正在尝试使用此 XDT 部分将 NLog 自定义配置部分插入我的 Web.config:

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

当我运行 XDT 转换时,我的 Web.Debug.config 包含:

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

通常,命名空间前缀是任意的,因此将 xsi 转换为 d4p1 不会导致任何问题。

但是,当我使用d4p1 时,我的应用程序中出现了来自 NLog 的运行时异常。手动将d4p1 的实例更改为xsi 可以解决此问题,但如果用户之后需要手动更改文件,它会颠覆我的配置转换实用程序。

有没有办法使用 XDT 保留命名空间前缀?

【问题讨论】:

  • 您找到解决方案了吗?我也有同样的问题。
  • 抱歉,还没有。这种 XDT 行为很严重。

标签: visual-studio-2012 nlog xdt-transform


【解决方案1】:

我们遇到了完全相同的问题。我不确定为什么它突然开始发生在一个特定的项目中,但我们的解决方案是将 xsi 命名空间添加到原始配置文件的顶层(即转换工作的基本文件)。所以...

<configuration>

...会变成...

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

这成功了。

另一种可行的方法是将转换应用于 nlog 元素的子元素。

希望对某人有所帮助。

【讨论】:

  • 这似乎不对。 xsi 命名空间应该是 "http://www.w3.org/2001/XMLSchema-instance"
  • @StevenLiekens 评论为我工作
【解决方案2】:

当我将我的 xdt:Transform 属性从目标和规则标签移动到 nlog 时,我开始看到这个问题。像这样将它们移回原始标签解决了它:

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

【讨论】:

  • 谢谢!我一直有这个问题,每个解决方案都没有用一个例子来解释如何让它工作。它正在工作!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多