13

ASPNET Core 2.0 with latest Nlog.

All config files load correctly.

My config file is simple, I just want it to log every thing.

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="Warn"
      internalLogFile="C:\wwwLogs\nlog.log">

  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <targets>
    <target xsi:type="File" name="allfile" fileName="C:\wwwLogs\${shortdate}.log"
            maxArchiveFiles="90"
            archiveNumbering="DateAndSequence"
            archiveAboveSize="250000"
            archiveFileName="archive/log.{#######}.log"
            archiveEvery="Day"
            concurrentWrites="true"
            layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />
  </targets>

  <rules>
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="allfile" />
  </rules>
</nlog>

I can see it in the trace log for nlog it is setting all levels to the correct output.

2017-11-01 14:21:26.3017 Trace Opening C:\wwwLogs\2017-11-01.log with allowFileSharedWriting=False
2017-11-01 14:21:28.5859 Debug Targets for TimeSlotApprovalService by level:
2017-11-01 14:21:28.5859 Debug Trace => allfile
2017-11-01 14:21:28.5859 Debug Debug => allfile
2017-11-01 14:21:28.5859 Debug Info => allfile
2017-11-01 14:21:28.5859 Debug Warn => allfile
2017-11-01 14:21:28.5859 Debug Error => allfile
2017-11-01 14:21:28.5859 Debug Fatal => allfile

In my application when I call this

_logger.LogDebug(JsonConvert.SerializeObject(resultList, Formatting.Indented));
_logger.LogError(JsonConvert.SerializeObject(resultList, Formatting.Indented));
_logger.LogCritical(JsonConvert.SerializeObject(resultList, Formatting.Indented));
_logger.LogWarning(JsonConvert.SerializeObject(resultList, Formatting.Indented));
_logger.LogTrace(JsonConvert.SerializeObject(rankedTimeSlots, Formatting.Indented));

Then the log file only logs these

2017-11-01 14:44:48.2570|TimeSlotApprovalService|**ERROR**|[json...

2017-11-01 14:44:48.2570|TimeSlotApprovalService|**FATAL**|[json...

2017-11-01 14:44:48.2570|TimeSlotApprovalService|**WARN**|[json...

Where are the rest?? Trace and Debug?? Info?

Piotr Kula
  • 9,597
  • 8
  • 59
  • 85
  • 3
    How did you register NLog with ASP.NET Core? You will have to adjust your logging configuration on `appsettings.json` to include Trace logs (it may default to Information). – poke Nov 01 '17 at 16:42
  • 1
    Oh ... my... god. :D I did not know about that! That is the problem. Convert your comment to an answer. I just never realised there was ANOTHER setting.. nLog documentation does not mention that anywhere in their docs and I went through it several times. – Piotr Kula Nov 02 '17 at 08:08

2 Answers2

32

ASP.NET Core and its logging system Microsoft.Extensions.Logging use a central configuration. This configuration applies regardless of the attached logging providers. So if you use NLog as a logging provider, you will still have to configure the logging infrastructure.

By default, the web host builder will automatically use the configuration within the appsettings.json to configure the logging verbosity. And in the default template, this is what the configuration looks like:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

So the default minimum log level to log is Warning. So even if you configure your NLog provider to log at any logging level, it won’t actually receive logging instructions lower than Warning from the logging system.

So you will have to adjust the configuration there to change it. Set it to Trace and it should log everything.

Note that you should still consider using the configuration there as the source of truth on what log levels should be logged. So just keep your NLog configuration to log whatever it gets, and then adjust your appsettings.json to match whatever you want to actually log, depending on the current enviroment (you can create files like appsettings.Development.json and appsettings.Production.json to create environment-specific configurations).

poke
  • 369,085
  • 72
  • 557
  • 602
  • 1
    Thanks. This makes perfect sense now and yes, I split out `Microsoft.*` and `MySystem.*` in nLog to log to various levels as I required. It is just this crucial piece of information was missed in the nLog documentation and frankly.. I asked other developers and they also did not realise this setting was there / causing the issue. So i set the app.settings to trace but set nLog to log Microsoft stuff for Warn only. Then everything else just works.+1 – Piotr Kula Nov 02 '17 at 13:04
  • 2
    I'm having this same issue but I have Default and Microsoft both set to Trace in appsettings.json. Weird. – Murphybro2 Jan 14 '19 at 11:15
  • 11
    An important further note: appsettings.Development.json is created automatically now and hidden away as a child node of the appsettings.json file in solution explorer. I spent an hour trying to figure out what the hell is wrong with my logging until I opened up the project folder in Explorer and noticed the development file which was created by VS and tucked away ever so neatly. – bokibeg Mar 29 '19 at 16:45
  • 1
    Thanks for the appsettings.Development.json hint. I'd worked out the rest but I'd never have found that! – phn Feb 16 '21 at 19:42
5

If you want to do it by code:

        var services = new ServiceCollection();
        services.AddLogging((builder) => builder.SetMinimumLevel(LogLevel.Trace));
Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70