serilog / serilog-aspnetcore

Serilog integration for ASP.NET Core

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Configuration on json Appsettings not filtering the levels.

alexmbra opened this issue · comments

Description
When I use filters from code, it works. But when I try to use them on the configuration on json Appsettings file, it does not filter anything. It writes everything on all log files.

Reproduction
This works:

var logger = new LoggerConfiguration()
               .MinimumLevel.Debug()
               .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
               .Enrich.FromLogContext()
               .WriteTo.Logger(
                   x => x.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Error)
                       .WriteTo.File("../logs/error_.log", formatProvider: CultureInfo.InvariantCulture, rollingInterval: RollingInterval.Day, retainedFileCountLimit: 30)
               )
               .WriteTo.Logger(
                   x => x.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Warning)
                       .WriteTo.File("../logs/warning_.log", formatProvider: CultureInfo.InvariantCulture, rollingInterval: RollingInterval.Day, retainedFileCountLimit: 30)
               )
               .WriteTo.File("../logs/all_.log", formatProvider: CultureInfo.InvariantCulture, rollingInterval: RollingInterval.Day, retainedFileCountLimit: 30)
               .WriteTo.Console(formatProvider: CultureInfo.InvariantCulture)
               .CreateLogger();

           builder.Logging.AddSerilog(logger);

But this does not:

var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();

            var logger = new LoggerConfiguration()
                .ReadFrom.Configuration(configuration)
                .CreateLogger();

            builder.Logging.AddSerilog(logger);


      },
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filters": [
              {
                "Name": "ByIncludingOnly",
                "Args": {
                  "expression": "(@Level = 'Warning')"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "../logs/warning_.log",
                  "rollingInterval": "Day",
                  "retainedFileCountLimit": 30,
                  "outputTemplate": "{Timestamp:o} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}"
                }
              }
            ]
          }
        }
      },
      {
        "Name": "File",
        "Args": {
          "path": "../logs/all_.log",
          "rollingInterval": "Day",
          "retainedFileCountLimit": 30,
          "outputTemplate": "{Timestamp:o} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}"
        }
      }
    ],
    "Enrich": [
      "FromLogContext"
    ],
    "Properties": {
      "Application": "MultipleLogFilesSample"
    }
  }

Expected behavior
Both codes are suppost to filter the log entries by level and write them into the respective files.

Relevant package, tooling and runtime versions
What Serilog version are you using, on what platform?
Visual Studio 2022 with nuget\packages\serilog.aspnetcore\7.0.0

Serilog.Expressions uses @l for the level property, not @Level.

If you still have difficulty getting the configuration to work, posting to Stack Overflow will get more eyes on it. Please feel free to post a link here, in that case.