Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog and Filters from Filters.Expressions in appsettings.json

We would like to get log for:

  1. Console (all log)
  2. File (all log)
  3. File (only filtered log)

All of this configured in appsettings.json.

This is the appsettings.json file for the application:

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.Literate", "Serilog.Sinks.File", "Serilog.Filters.Expressions" ],
    "MinimumLevel": "Verbose",
    "WriteTo": [
      {
        "Name": "LiterateConsole"
      },
      {
        "Name": "File",
        "Args": {
          "path": "%TEMP%\\Logs\\FileWithoutFilter-.log",
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
          "rollingInterval": "Day"
        }
      },
      {
        "Name": "File",
        "Args": {
          "path": "%TEMP%\\Logs\\UPLOADERROR-.log",
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
        },
        "Filter": [
          {
            "Name": "ByIncludingOnly",
            "Args": {
              "expression": "@Level = 'Error' and UploadError is not null"
            }
          }
        ]
      }
    ]
  }
}

But, in spite of Console and File (without filtering) is running fine, File with filtering is logging all the lines, like the file (without filter).

We are sending this log.error line in C# code:

Log.Error("Fichero con error {@UploadError}", true);

But, I have said, all lines are logged to UPLOADERROR file

Some idea what is wrong in the appsettings.file?

Regards.

like image 521
ferpega Avatar asked Jan 03 '23 16:01

ferpega


1 Answers

The solution is using subloggers with serilog. You must then configure the sublogger with the filtering and the sink.

Careful reading of serilog documentation related to general, configuration and filters, was the trick.

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.Literate", "Serilog.Sinks.File", "Serilog.Filters.Expressions" ],
    "MinimumLevel": "Verbose",
    "WriteTo": [
      {
        "Name": "LiterateConsole"
      },
      {
        "Name": "File",
        "Args": {
          "path": "%TEMP%\\Logs\\FileWithoutFilter-.log",
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
          "rollingInterval": "Day"
        }
      },
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByIncludingOnly",
                "Args": {
                  "expression": "@Level = 'Error' and UploadError is not null"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "%TEMP%\\Logs\\UPLOADERROR-.log",
                  "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
                }
              }
            ]
          }
        }
      }
    ]
  }
}

With previous configuration all is working fine now.

like image 64
ferpega Avatar answered Jan 14 '23 05:01

ferpega