Search code examples
c#serilog

How to configure serilog with json to write in two different files?


I would like to have a log for information and warning and another log to error and fatal.

I would like to use json file and not fluent API, and use only json, because I have read there is problem getting part of the configuration from json and another part from fluent API.

But I have the problem that in both files it is written all the logs, not only information and warning in one file and error and fatal logs in the other.

I have see this question, but it doesn't use json file.

My program.cs file is this:

builder.Services
    .AddSerilog()
    .AddHostedService<Worker>()
    .InstalarServicios(builder.Configuration);


Log.Logger = new LoggerConfiguration()
            .ReadFrom.Configuration(builder.Configuration)
            .Enrich.FromLogContext()
            .CreateLogger();

And this is my json file:

{
  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Information",
        "System": "Warning"
      }
    },

    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "LogEventLevel": "Information, Warning",
          "path": "./logs/log-informacion-.txt",
          "rollingInterval": "Day"
        }
      },

      {
        "Name": "File",
        "Args": {
          "LogEventLevel": "Error, Fatal",
          "path": "./logs/log-errores-.txt",
          "rollingInterval": "Day"
        }
      }
    ]
  }
}

In the json file, I have tried to use "RestrictedToMinimumLevel": "Information" for one file and "RestrictedToMinimumLevel": "Error" for the second, but the result is always the same, it creates two files and in both it writes the same log, no matter if it information or error.

How could I write only in one file information and warning and in the other error and fatal logs?

Thanks.


Solution

  • It seems you are trying to filter your logs then write these filtered logs into distinct files as I can see in your linked example.

    You may try this configuration settings inside json file:

    {
      "Serilog": {
        "Using": [ "Serilog.Sinks.File", "Serilog.Filters.Expressions" ],
        "MinimumLevel": {
          "Default": "Information",
          "Override": {
            "Microsoft": "Information",
            "System": "Warning"
          }
        },
        "WriteTo": [
          {
            "Name": "Logger",
            "Args": {
              "configureLogger": {
                "Filter": [
                  {
                    "Name": "ByIncludingOnly",
                    "Args": {
                      "expression": "Level = 'Information' or Level = 'Warning'"
                    }
                  }
                ],
                "WriteTo": [
                  {
                    "Name": "File",
                    "Args": {
                      "path": "./logs/log-informacion-.txt",
                      "rollingInterval": "Day"
                    }
                  }
                ]
              }
            }
          },
          {
            "Name": "Logger",
            "Args": {
              "configureLogger": {
                "Filter": [
                  {
                    "Name": "ByIncludingOnly",
                    "Args": {
                      "expression": "Level = 'Error' or Level = 'Fatal'"
                    }
                  }
                ],
                "WriteTo": [
                  {
                    "Name": "File",
                    "Args": {
                      "path": "./logs/log-errores-.txt",
                      "rollingInterval": "Day"
                    }
                  }
                ]
              }
            }
          }
        ]
      }
    }
    
    

    In this configuration, distinct filtering and writing into different files inside every logger can be seen.