visual-studio-2012 - 保留 namespace 前缀的转换

标签 visual-studio-2012 nlog xdt-transform

我正在尝试使用此 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 保留 namespace 前缀?

最佳答案

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

<configuration>

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

这成功了。

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

希望能帮助某人。

关于visual-studio-2012 - 保留 namespace 前缀的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18925213/

相关文章:

sql - "This database file is not compatible with the current instance of SQL Server"

html - P 标签上的 HTML 文件的 xdt 转换失败

docker - NLog配置从docker run参数设置连接字符串

asp.net-web-api - 使用 Autofac 将 NLog 注入(inject) Web Api Controller

date - 归档时 NLog 不正确的日期

xml - 如何使用 XMLNS 属性转换元素

c# - 为暂存和生产 web.config 文件生成 xml 文档转换

c++ - Visual Studio 2012不同的值发行/ Debug模式

visual-studio-2012 - 空格显示为点,制表符显示为箭头

c# - 为什么在编译时有 dll 文件被复制到我的项目的 Bin/Debug 文件夹中,尽管没有引用任何这些库?