C#C
C#3y ago
Ikinidae

❔ Serilog: how to write a file for single level

Hi guys! I've been asked to write log levels Information, Warning and Error in development, but each of them in one separated file, so that I have information.txt containing only Information logs, warning.txt containing only Warning logs, Error.txt containing only Error logs.

This is my implementation at the moment (it works):
In Program.cs:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.UseStartup<Startup>();
        webBuilder.UseSerilog((webHostBuilderContext, loggerConfiguration) =>
        {
            loggerConfiguration.ReadFrom.Configuration(webHostBuilderContext.Configuration);
        });
    });


in appsettings:
"Serilog": {
  "MinimumLevel": "Information",
  "WriteTo": [
    {
      "Name": "Console"
    },
    {
      "Name": "File",
      "Args": {
        "path": "wwwroot/Logs/log.txt",
        "rollingInterval": "Day",
        "reatinedFileCountLimit": 14,
        "restrictedToMinimumLevel": "Information"
      }
    }
  ]
},


I have installed Serilog.Expressions package and read the docs, I know I have to use something like this
"Filters": [
  {
    "Name": "ByIncludingOnly",
    "Args": {
      "expression": "@l = 'Information'"
    }
  }
]


but I can't understand where it should be placed. I've tried placing it in Args or between Args and Name but it doesn't work, can somebody help me? 🥺
Was this page helpful?