我正在尝试使用此 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/