Search code examples
c#serilog

Serilog Configuration separate EF logs from Application


I am attempting to have my EF logs in another file from my application logs.

Here is my current configuration for Serilog.

 "Serilog": {
    "Using": [
      "Serilog.Sinks.Console",
      "Serilog.Sinks.File",
      "Serilog.Exceptions",
      "Serilog.Sinks.Async",
      "Serilog.Expressions"
    ],
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Warning",
        "Microsoft.EntityFrameworkCore": "Error"
      }
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
          "filter": [
            {
              "Name": "ByExcluding",
              "Args": {
                "expression": "StartsWith(SourceContext, 'Microsoft.EntityFrameworkCore')"
              }
            }
          ]
        }
      },
      {
        "Name": "Async",
        "batchSizeLimit": 100,
        "period": "00:00:02",
        "Args": {
          "configure": [
            {
              "Name": "File",
              "Args": {
                "rollingInterval": "Day",
                "path": "Logs/WrathForged.Auth.log..log",
                "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
                "filter": [
                  {
                    "Name": "ByExcluding",
                    "Args": {
                      "expression": "StartsWith(SourceContext, 'Microsoft.EntityFrameworkCore')"
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      {
        "Name": "Async",
        "batchSizeLimit": 100,
        "Args": {
          "configure": [
            {
              "Name": "File",
              "Args": {
                "path": "Logs/WrathForged.Auth.DatabaseLogs..log",
                "rollingInterval": "Day",
                "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
                "filter": [
                  {
                    "Name": "ByIncludingOnly",
                    "Args": {
                      "expression": "StartsWith(SourceContext, 'Microsoft.EntityFrameworkCore')"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    ],
    "Enrich": [
      "FromLogContext",
      "WithMachineName",
      "WithThreadId",
      "WithExceptionDetails"
    ]
  }

What's happening is, its mirroring the logs and I'm getting no EF logs at all. The contents of the mirrored logs are exactly the same. I am not sure if my filtering is in the correct spot or if the override is messing with something.

Mirrored Logs

Anyone have any idea what I'm doing wrong in my configuration? Thanks a bunch!


Solution

  • I have working example on GitHub here.

    AppSettings.Json

     "Serilog": {
        "Using": [ "Serilog.Sinks.MSSqlServer", "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Enrichers.Environment", "Serilog.Enrichers.Thread", "Serilog.Exceptions", "Serilog.Expressions", "Serilog.Sinks.Async" ],
        "MinimumLevel": "Debug",
        "WriteTo": [
          {
            "Name": "Console",
            "Args": {
              "outputTemplate": "[{Timestamp:yyyy:MM:dd hh:mm:ss} {CorrelationId} {Level:u3}] {Message:lj}{NewLine}{Exception}"
            }
          },
          {
            "Name": "File",
            "Args": {
              "path": "Logs/SystemLog.txt",
              "rollingInterval": "Day",
              "retainedFileCountLimit": 31,
              "buffered": false,
              "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} {CorrelationId} [{Level:u3}] {Properties} {Message:lj} {NewLine}{Exception}",
              "restrictedToMinimumLevel": "Debug"
            }
          },
          {
            "Name": "File",
            "Args": {
              "path": "Logs/MyDBLog.txt",
              "rollingInterval": "Day",
              "retainedFileCountLimit": 31,
              "buffered": false,
              "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} {CorrelationId} [{Level:u3}] {Properties} {Message:lj} {NewLine}{Exception}",
              "filter": [
                {
                  "Name": "ByIncludingOnly",
                  "Args": {
                    "expression": "SourceContext = 'Microsoft' or StartsWith(SourceContext, 'Microsoft')" //"StartsWith(SourceContext,'Microsoft.EntityFrameworkCore.ChangeTracking')",
                  }
                }
              ],
              "restrictedToMinimumLevel": "Debug"
            }
          },
          {
            "Name": "Async",        
            "Args": {
              "configure": [
                {
                  "Name": "File",
                  "Args": {
                    "path": "Logs/My.DatabaseLogs..log",
                    "rollingInterval": "Day",
                    "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} {CorrelationId} [{Level:u3}] {Properties} {Message:lj} {NewLine}{Exception}",
                    "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
                    "filter": [
                      {
                        "Name": "ByIncludingOnly",
                        "Args": {
                          "expression": "@l='Error' or SourceContext = 'Microsoft' or StartsWith(SourceContext, 'Microsoft')" 
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }          
        ],
        "Enrich": [
          "FromLogContext",
          "WithMachineName",
          "WithThreadId",
          "WithExceptionDetails"
        ]
      },
    

    There is an another way to do this. May be useful to you or someone else.:)

    Program.cs

    Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .ReadFrom.Configuration(configFile)
                .WriteTo.Async(db => db.File("Logs/My.Database..Logs..log"), bufferSize: 1024)
                        .Filter.ByIncludingOnly(l => l.Level == LogEventLevel.Error || l.Level == LogEventLevel.Fatal)
                .CreateLogger();
    

    Log file content

    enter image description here

    enter image description here