Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration on json Appsettings not filtering the levels. #340

Open
alexmbra opened this issue Jul 11, 2023 · 0 comments
Open

Configuration on json Appsettings not filtering the levels. #340

alexmbra opened this issue Jul 11, 2023 · 0 comments
Labels

Comments

@alexmbra
Copy link

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

@alexmbra alexmbra added the bug label Jul 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant