NLog会在所有aspnet布局渲染器上抛出配置异常

Jac*_*cob 23 asp.net-mvc nlog asp.net-mvc-3

我一直在努力在我的ASP.NET MVC 3应用程序上设置NLog v2,到目前为止它运行良好.(我使用的是来自官方的NuGet库包).然而,当我尝试更改日志布局包括任何aspnet-*布局渲染器,我得到一个配置错误.我已将问题减少到以下最小用例:

在configSections块中:

<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

如果我改变布局中使用不属于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: 
Run Code Online (Sandbox Code Playgroud)

我真的不知道发生了什么.我不确定该渲染器会导致配置无效.我大部分时间一直在打它而且无处可去,所以我希望这里有人可以提供帮助.

谢谢!

Dar*_*rov 32

确保引用了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>
Run Code Online (Sandbox Code Playgroud)


Joh*_*ner 8

如果达林不起作用的替代解决方案

您必须将NLog.Extended引用为Darin提及 http://nuget.org/packages/NLog.Extended

从NLog 2.0开始,您无需在配置XML中添加引用.

我的问题是我在我的web层(我的web.config所在的位置)没有NLog.Extended的硬引用,因此编译器没有将文件复制到需要的位置.

这可以通过向NLog添加硬引用来轻松修复.在您配置日志记录的任何位置都是无操作:

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