24

I have been working to set up NLog v2 on my ASP.NET MVC 3 application and it has worked very well so far. (I'm using the package from the offical nuGet repository) However, when I try to change the log layout to include any of the aspnet-* layout renderers, I get a configuration error. I've reduced the problem to the following minimum use case:

In the configSections block:

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

The Nlog block:

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

If I change layout use any combination of renderers that are not part of the aspnet* family, this works well (I haven't tested every one, but I've looked at quite a few). The error I get is here:

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: 

I have really no idea what's going on. I'm not sure what about that renderer causes the configuration to become invalid. I've been banging around at it most of the day and have gotten nowhere, so I'm hoping someone here can help.

Thank you!

Julian
  • 33,915
  • 22
  • 119
  • 174
Jacob
  • 1,285
  • 2
  • 11
  • 27

4 Answers4

33

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>
Julian
  • 33,915
  • 22
  • 119
  • 174
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thank you. I feel kind of silly now for missing that. I really appreciate the help! – Jacob Jun 16 '11 at 19:21
  • It just refuses to work for me. No matter what variable I try, the output is blank :( – Storm May 14 '15 at 09:15
  • 1
    As some answers have said : This is now in NLog.Web. I completely missed this as all the information online tells you to add NLog.Extended – 4imble Oct 20 '15 at 16:38
  • Thank you, I updated NLog and NLog.Extended with VS NuGet and the execution is ok. – Michael Fayad Dec 10 '21 at 21:46
11

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

innominate227
  • 11,109
  • 1
  • 17
  • 20
8

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();
John Culviner
  • 22,235
  • 6
  • 55
  • 51
1

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
Hugo Dozois
  • 8,147
  • 12
  • 54
  • 58