2011-06-10 17 views
20

我一直在努力在ASP.NET MVC 3應用程序上設置NLog v2,並且迄今爲止它工作得非常好。 (我使用的是官方nuGet存儲庫中的包)但是,當我嘗試更改日誌佈局以包含任何aspnet- *佈局渲染器時,出現配置錯誤。我已經減少了問題以下最低的用例:NLog在所有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: 

我真的不知道發生了什麼事。我不確定那個渲染器會導致配置失效。我一整天的大部分時間都在喋喋不休,因此我希望有人能幫上忙。

謝謝!

回答

31

確保已引用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> 
+0

謝謝。因爲錯過了這些,我現在感覺很傻。我非常感謝幫助! – Jacob 2011-06-16 19:21:11

+0

它只是拒絕爲我工作。無論我嘗試什麼變量,輸出都是空白的:( – Storm 2015-05-14 09:15:25

+1

正如有些答案所說:現在在NLog.Web中,我完全錯過了這一點,因爲所有在線信息都告訴你添加NLog.Extended – 4imble 2015-10-20 16:38:59

1

在我來說,我是用擴展的le_nlog和一個原因,它沒有安裝在應用程序!

所以我安裝* le_nlog *這樣做:

PM> Install-Package le_nlog 
8

替代解決方案,如果Darin的不工作

您必須NLog.Extended引用爲達林提到 http://nuget.org/packages/NLog.Extended

從NLog 2.0開始,您不需要在配置XML中添加引用。

我的問題是我沒有硬引用NLog.Extended在我的web層(我的web.config是),所以編譯器沒有複製它需要的文件。

這可以通過添加硬盤參考NLog.Extended這是一個無操作,無論你在配置你的日誌很容易解決:

//forces the compiler to include NLog.Extensions in all downstream output directories 
private static AspNetApplicationValueLayoutRenderer blank = new AspNetApplicationValueLayoutRenderer();