Asp.net-Mvc

NLog 上的配置轉換不起作用

  • March 16, 2014

我有一個 Web 項目(ASP.NET MVC 4 項目),其中有許多儲存在Web.ConfigNLog.config文件中的配置。

我有幾個發布配置文件PublishProfile1PublishProfile2等。當使用發布配置文件將我的 Web 項目部署到伺服器時,我想在部署後更改兩個配置文件中的一些配置(Web.config中的一些應用程序設置和NLog.config)。

我已按照此處的步驟進行操作,它非常適合更改Web.Config中的設置(例如,尊重來自Web.PublishProfile1.Config的轉換)。

這是我的 NLog.PublishProfile1.Config轉換文件:

<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xmlns="http://www.nlog-project.org/schemas/NLog.xsd">
 <nlog  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <targets >
     <target xsi:type="File"
             name="tracelog"
             fileName="NEW_VALUE_HERE" layout="${longdate} [${threadname}::${threadid}] ${pad:padding=5:inner=${level:uppercase=true}} ${logger} - ${message}"
             xdt:Transform="Replace"  xdt:Locator="Match(name)" />
   </targets>
 </nlog>
</nlog>

問題是我在NLog.PublishProfile1.config中有相同的轉換,但這些轉換在部署後也沒有應用。

有沒有人知道為什麼這種轉換不適用於 NLog.config 但適用於發布配置文件上的 Web.config?

為了解決這個問題,我不得不:

  1. 避免使用nlog.config

  2. 在裡面創建nlog部分web.config並將nlog.config 的內容移動到web.config 以便能夠對一個文件使用web.config 轉換功能。如需進一步說明,請查看:NLog 配置說明

3)從節點中刪除xmlns屬性。nlog似乎有一個錯誤在web.config轉換過程中搞砸了一切。您可以安全地刪除以下節點:

xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  1. 我找不到在 nlog/targets 節點下僅轉換單個目標的方法。要更改記錄器的連接字元串,我必須複製整個 xml 節點,在父節點上使用 xdt:Transform=“Replace”,如下所示:
<nlog
     throwExceptions="true"
     internalLogLevel="Trace"
     internalLogFile="..\..\..\Logs\nlog-app.log" 
     xdt:Transform="Replace">
<!--(copy and paste all nlog configuration here)-->
</nlog>

引用自:https://stackoverflow.com/questions/22438204