You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在web.config转换中替换NLog目标的路径部分内容?

批量替换NLog Target路径的Web.config转换方案

要在Visual Studio 2022中通过web.Debug.config批量替换NLog target元素的fileName和archiveFileName属性根路径,可以使用**XML Document Transform(XDT)**的XPath字符串替换功能,无需逐个修改每个target。

具体转换配置

在web.Debug.config中添加以下代码(确保与web.config中的NLog节点结构对应):

<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <!-- 批量替换所有File类型target的路径属性 -->
    <target xsi:type="File"
            fileName="{replace(./@fileName, 'C:/ProductionPath', 'D:/DevelopmentPath')}"
            archiveFileName="{replace(./@archiveFileName, 'C:/ProductionPath', 'D:/DevelopmentPath')}"
            xdt:Transform="SetAttributes(fileName, archiveFileName)"
            xdt:Locator="Match(xsi:type)" />
  </targets>
</nlog>

代码说明

  • xdt:Locator="Match(xsi:type)":匹配所有xsi:type="File"的target元素,实现批量处理所有文件类型的NLog日志目标。
  • xdt:Transform="SetAttributes(fileName, archiveFileName)":指定要修改的两个属性。
  • XPath replace函数:通过replace(./@属性名, '旧路径', '新路径')语法,将属性值中的旧根路径替换为开发环境路径,保留路径后半段的日志文件结构。

验证转换效果

在Visual Studio中右键点击web.config,选择Preview Transform,即可查看转换后的配置,确认路径是否已正确替换。

内容的提问来源于stack exchange,提问作者jlavallet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 07:50:28