Serilog 配置将 EF 日志与应用程序分开

问题描述 投票:0回答:1

我正在尝试将我的 EF 记录到我的应用程序日志中的另一个文件中。

这是我当前的 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"
    ]
  }

发生的情况是,它镜像了日志,但我根本没有收到 EF 日志。镜像日志的内容是完全一样的。我不确定我的过滤是否在正确的位置,或者覆盖是否弄乱了某些东西。

有人知道我的配置做错了什么吗?非常感谢!

c# serilog
1个回答
0
投票

我在 GitHub 上有工作示例这里

应用程序设置.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": "LogLevel = 'Error' or SourceContext = 'Microsoft' or StartsWith(SourceContext, 'Microsoft')" 
                    }
                  }
                ]
              }
            }
          ]
        }
      }          
    ],
    "Enrich": [
      "FromLogContext",
      "WithMachineName",
      "WithThreadId",
      "WithExceptionDetails"
    ]
  },

还有另一种方法可以处理这个问题。

程序.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();

日志文件内容

© www.soinside.com 2019 - 2024. All rights reserved.