NLog throws configuration exception on all aspnet layout renderers

自闭症网瘾萝莉.ら 提交于 2019-12-03 01:21:52
Darin Dimitrov

Make sure you have referenced the NLog.Extended assembly which is where those layouts are defined and which must have been added by the NuGet package as well to the references:

<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>

As of NLog 4.0 the ASP.NET renderes are now in Nlog.Web http://nlog-project.org/2015/06/13/NLog-Extended_NLog-Web_and_NLog-Windows-Forms.html

Alternative solution if Darin's doesn't work

You must have NLog.Extended referenced as Darin mentions http://nuget.org/packages/NLog.Extended

As of NLog 2.0 you do not need to add reference in the configuration XML.

My problem was that I had no hard references to NLog.Extended in my web layer (where my web.config is) so the compiler wasn't copying the file where it needed to be.

This can be easily fixed by adding a hard reference to NLog.Extended that is a no-op wherever you are configuring your logging:

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

In my case I was using extension of le_nlog and for a reason, it was not installed in the app !

so I installed *le_nlog* by doing so :

PM> Install-Package le_nlog
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!