1

Debug and Trace logs not printed although minlevel set to Trace on appsettings.json. actual output:

FATAL|ConsoleAppLogging.Program|LogCritical
ERROR|ConsoleAppLogging.Program|LogError
INFO|ConsoleAppLogging.Program|LogInformation
WARN|ConsoleAppLogging.Program|LogWarning

I created a sample project repo that can be found here https://github.com/yaniv120892/ApplicationLogging

using .NetCore 3.1 and packages :

  • Microsoft.Extensions.Configuration.Json 3.1.5
  • NLog 4.7.2
  • NLog.Extensions.Logging Version=1.6.4

appsettings.Json:

{
  "NLog": {
    "targets": {
      "logConsole": {
        "type": "ColoredConsole",
        "layout": "${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}"
      }
    },
    "rules": [
      {
        "name": "*",
        "minLevel": "Trace",
        "writeTo": "logConsole"
      }
    ]
  }
}

ApplicationLogging.cs:

 public static class ApplicationLogging
    {
        public static ILogger CreateLogger<T>() =>
            s_loggerFactory.CreateLogger<T>();

        private static readonly ILoggerFactory s_loggerFactory = LoggerFactory.Create(AddNLog);

        private static void AddNLog(ILoggingBuilder builder)
        {
            var configJson = new ConfigurationBuilder()
                .SetBasePath(System.IO.Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();
            LogManager.Configuration = new NLogLoggingConfiguration(configJson.GetSection("NLog"));
            builder.AddNLog();
        }
    }

Program.cs:

public class Program
    {
        private static readonly ILogger s_logger = ApplicationLogging.CreateLogger<Program>();

        static void Main()
        {
            s_logger.LogCritical("LogCritical");
            s_logger.LogDebug("LogDebug");
            s_logger.LogError("LogError");
            s_logger.LogInformation("LogInformation");
            s_logger.LogTrace("LogTrace");
            s_logger.LogWarning("LogWarning");
        }
    }
Yaniv daye
  • 73
  • 7
  • try this accordingly `var config = new NLog.Config.LoggingConfiguration(); var logconsole = new NLog.Targets.ConsoleTarget("logconsole"); // Rules for mapping loggers to targets config.AddRule(LogLevel.Info, LogLevel.Fatal, logconsole);` – Vivek Nuna Jul 07 '20 at 13:49
  • When I change to implementation to your suggestion it doesn't work (nothing is printed) – Yaniv daye Jul 07 '20 at 14:17
  • you need to add other rules also by using `AddRule` method – Vivek Nuna Jul 07 '20 at 14:18
  • Maybe you have an environment specific `appsettings.json`. Ex. `appsettings.development.json`. See also https://github.com/NLog/NLog.Web/wiki/Missing-trace%5Cdebug-logs-in-ASP.NET-Core-3%3F – Rolf Kristensen Jul 07 '20 at 18:42
  • I don't have appsettings.development.json file in this project. I added a sample project to github, you can find it here: https://github.com/yaniv120892/ApplicationLogging – Yaniv daye Jul 08 '20 at 13:08

1 Answers1

2

Think you are missing SetMinimumLevel:

public static class ApplicationLogging
{
    public static ILogger CreateLogger<T>() => s_loggerFactory.CreateLogger<T>();

    private static readonly ILoggerFactory s_loggerFactory = LoggerFactory.Create(AddNLog);

    private static void AddNLog(ILoggingBuilder builder)
    {
        var configJson = new ConfigurationBuilder()
            .SetBasePath(System.IO.Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();
        var nlogConfig = new NLogLoggingConfiguration(configJson.GetSection("NLog"));
        builder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
        builder.AddNLog(nlogConfig);
    }
}
Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
  • Thanks! works like a magic. please mark this comment as the correct answer – Yaniv daye Jul 08 '20 at 20:16
  • Why do I have to set minimum level in code? Isn't it something that already is configured by the NLog config? – BlackMatrix Feb 14 '21 at 12:07
  • @BlackMatrix NLog LoggingProvider is a citizin in the Microsoft Extension Logging (MEL) LoggerFactory. SetMinimumLevel updates the filter-config inside MEL-LoggerFactory, so it will not distub the expected behavior of the NLog-filter-logic. Planning to change the NLog LoggingProvider to include a new setting ReplaceLoggerFactory so instead of being a citizen then becomes the dictator. – Rolf Kristensen Feb 14 '21 at 12:44