Search code examples
c#entity-frameworknlog

Nlog Config: override general rules


This is my nlog config from appsettings.json:

"rules": [
  {
    "logger": "Microsoft.EntityFrameworkCore.*",
    "minLevel": "Error",
    "writeTo": "console"
  },
  {
    "logger": "*",
    "minLevel": "Info",
    "writeTo": "console"
  }
]
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
     
     var loggerFactory = LoggerFactory.Create(builder => builder.AddNLog());
     optionsBuilder.UseLoggerFactory(loggerFactory);
}

I would like to log all logs with minLevel info to console, but for Microsoft.EntityFrameworkCore.* I would like to log only Errors and Fatals.

Could you explain me, what I am doing wrong, because info level messages are being logged to console too.


Solution

  • With NLog v5 then you can use FinalMinLevel:

    "rules": [
      {
        "logger": "Microsoft.EntityFrameworkCore.*",
        "finalMinLevel": "Error"
      },
      {
        "logger": "*",
        "minLevel": "Info",
        "writeTo": "console"
      }
    ]
    

    See also: https://github.com/NLog/NLog/wiki/Configuration-file#rules