禁用不需要的日志记录 - Serilog、.Net 6 Minimal API

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

我想摆脱不需要的日志记录。我设法使用 Serilog 的以下配置过滤掉大部分内容:

     "Serilog": {
        "Using": [ "Serilog.Sinks.File", "Serilog.Sinks.Console" ],
        "MinimumLevel": {
            "Default": "Information",
            "Override": {
                "Microsoft.EntityFrameworkCore.Database.Command": "Error",
                "Microsoft": "Debug",
                "Microsoft.Hosting.Lifetime": "Information",
                "Microsoft.AspNetCore.Hosting.Diagnostics": "Warning",
                "Microsoft.AspNetCore.Mvc.RazorPages": "Warning",
                "Microsoft.AspNetCore.Mvc.ViewFeatures": "Warning",
                "Microsoft.AspNetCore.StaticFiles": "Warning",
                "Microsoft.EntityFrameworkCore.Migrations": "Warning",
                "Microsoft.EntityFrameworkCore.Database": "Warning",
                "Microsoft.AspNetCore.Mvc.Infrastructure": "Warning"
            }
        },

But still I'm getting something like this:

    [2023-05-07 15:08:38.576 +02:00  INF]  Executing endpoint HTTP: GET HeartBeat/{iddevice}/{status}/{version?}
    [2023-05-07 15:08:38.578 +02:00  INF]  Entity Framework Core 6.0.6 initialized Context using provider 'Microsoft.EntityFrameworkCore.SqlServer:6.0.0 with options: None
    [2023-05-07 15:08:38.594 +02:00  INF]  Executed endpoint HTTP: GET HeartBeat/{iddevice}/{status}/{version?}

Each time any API Method is invoked. I'm tracing some kind of a bug and I'd like to have only my own explicit logging. Which overrides I should add to disable it?
.net api serilog
3个回答
0
投票

如果您想从 Microsoft 中删除所有日志 - 只需将“根”类别指定为

Error
None
:

"Serilog": {
    "Using": [ "Serilog.Sinks.File", "Serilog.Sinks.Console" ],
    "MinimumLevel": {
        "Default": "Information",
        "Override": {
            "Microsoft": "None"
        }
     }
}

文档中所述:

"Microsoft"
类别适用于所有以
"Microsoft"

开头的类别

在确定日志级别时使用最具体的类别(因此,如果您需要来自

Microsoft.AspNetCore.Hosting.Diagnostics
的日志,您可以显式添加它们)。


0
投票

要从某些源中删除日志,您可以使用表达式过滤器。你的代码可能看起来像这样

    "WriteTo": [
  {
    "Name": "Console",
    "Args": {
      "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3} {Application}] {Message:lj} {NewLine}{Exception}",
    },
    "configureLogger": {
      "Filter": [
        {
          "Name": "ByExcluding",
          "Args": {
            "expression": "@Level = 'Error' and UploadError is not null"
          }
        }
      ]
    }
  }

0
投票

Serilog 还没有

MinimumLevel: None

替换为:

"Override": {
    "Microsoft": "Warning",
    "Microsoft.Hosting.Lifetime": "Information",
    "Microsoft.EntityFrameworkCore": "Information"
}
© www.soinside.com 2019 - 2024. All rights reserved.