Asp.net-Mvc

NLog 在所有 aspnet 佈局渲染器上拋出配置異常

  • June 10, 2011

我一直在努力在我的 ASP.NET MVC 3 應用程序上設置 NLog v2,到目前為止它執行良好。(我使用的是官方 nuGet 儲存庫中的包)但是,當我嘗試更改日誌佈局以包含任何 aspnet-* 佈局渲染器時,我收到配置錯誤。我已將問題減少到以下最小案例:

在 configSections 塊中:

<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>

Nlog 塊:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">

<variable name="logDirectory" value="C:\Logs" />
<targets>
 <target name="logFile" xsi:type="File" fileName="${logDirectory}\app.log"
     layout="${aspnet-user-identity}" />
</targets>       

<rules>
 <logger name="*" minlevel="Info" writeTo="logfile" />
</rules>

如果我更改佈局,使用不屬於 aspnet* 系列的任何渲染器組合,則效果很好(我沒有測試每一個,但我已經看過很多)。我得到的錯誤在這裡:

Configuration Error 
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. 

Parser Error Message: An error occurred creating the configuration section handler for nlog: Exception occurred when loading configuration from C:\..[snip]..\web.config

Source Error: 


Line 16: </configSections>
Line 17: 
Line 18:   <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
Line 19:     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">
Line 20: 

我真的不知道發生了什麼事。我不確定那個渲染器會導致配置無效。我大部分時間都在敲打它,但一無所獲,所以我希望這裡有人能提供幫助。

謝謝!

確保您已引用了NLog.Extended定義這些佈局的程序集,並且 NuGet 包也必須將其添加到引用中:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     throwExceptions="true">

   <extensions>
       <add assembly="NLog.Extended" />
   </extensions>

   <variable name="logDirectory" value="C:\Logs" />

   <targets>
       <target name="logFile" 
               xsi:type="File" 
               fileName="${logDirectory}\app.log"
               layout="${aspnet-user-identity} ${message}" />
   </targets>

   <rules>
       <logger name="*" minlevel="Debug" writeTo="logfile" />
   </rules>

</nlog>

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