c# - 如何防止 Serilog 将每一个小步骤自动记录为信息?

标签 c# serilog

我想将 Serilog 用于我的 .Net 5 Web Api 项目。我安装了软件包

  • Serilog.AspNetCore v4.1.0
  • Serilog.Sinks.Console v4.0.0

  • 并将 Program.cs 文件更改为
    public sealed class Program
    {
        public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                .WriteTo.Console()
                .CreateLogger();
            
            CreateHostBuilder(args)
                .Build()
                .Run();
        }
    
        private static IHostBuilder CreateHostBuilder(string[] args) 
            => Host
                .CreateDefaultBuilder(args)
                .UseSerilog()
                .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
    }
    
    我创建了一个用于测试目的的 Controller
    [ApiController]
    [Route("[controller]")]
    public sealed class TodosController : ControllerBase
    {
        [HttpGet("{todoId:Guid}")]
        public IActionResult GetTodoById(Guid todoId) => Ok(todoId);
    }
    
    使用此端点后,我可以在控制台中看到以下内容
    [22:30:11 INF] Now listening on: https://localhost:5001
    [22:30:11 INF] Now listening on: http://localhost:5000
    [22:30:11 INF] Application started. Press Ctrl+C to shut down.
    [22:30:11 INF] Hosting environment: Development
    [22:30:11 INF] Content root path: /home/.../Server
    [22:30:12 INF] Request starting HTTP/2 GET https://localhost:5001/swagger - -
    [22:30:12 INF] Request finished HTTP/2 GET https://localhost:5001/swagger - - - 301 0 - 78.0119ms
    [22:30:12 INF] Request starting HTTP/2 GET https://localhost:5001/swagger/index.html - -
    [22:30:12 INF] Request finished HTTP/2 GET https://localhost:5001/swagger/index.html - - - 200 - text/html;charset=utf-8 64.9042ms
    [22:30:12 INF] Request starting HTTP/2 GET https://localhost:5001/swagger/swagger-ui.css - -
    [22:30:12 INF] Request starting HTTP/2 GET https://localhost:5001/swagger/swagger-ui-bundle.js - -
    [22:30:12 INF] Request starting HTTP/2 GET https://localhost:5001/swagger/swagger-ui-standalone-preset.js - -
    [22:30:12 INF] Sending file. Request path: '/swagger-ui-standalone-preset.js'. Physical path: 'N/A'
    [22:30:12 INF] Sending file. Request path: '/swagger-ui.css'. Physical path: 'N/A'
    [22:30:12 INF] Sending file. Request path: '/swagger-ui-bundle.js'. Physical path: 'N/A'
    [22:30:12 INF] Request finished HTTP/2 GET https://localhost:5001/swagger/swagger-ui-bundle.js - - - 200 986342 application/javascript 191.4506ms
    [22:30:12 INF] Request finished HTTP/2 GET https://localhost:5001/swagger/swagger-ui-standalone-preset.js - - - 200 311804 application/javascript 191.4478ms
    [22:30:12 INF] Request finished HTTP/2 GET https://localhost:5001/swagger/swagger-ui.css - - - 200 142933 text/css 192.6142ms
    [22:30:13 INF] Request starting HTTP/2 GET https://localhost:5001/swagger/v1/swagger.json - -
    [22:30:13 INF] Request finished HTTP/2 GET https://localhost:5001/swagger/v1/swagger.json - - - 200 - application/json;charset=utf-8 83.3874ms
    [22:30:41 INF] Request starting HTTP/2 GET https://localhost:5001/Todos/00119201-3fed-4741-8295-48d697790729 - -
    [22:30:41 INF] Executing endpoint 'Server.Controllers.TodosController.GetTodoById (Server)'
    [22:30:41 INF] Route matched with {action = "GetTodoById", controller = "Todos"}. Executing controller action with signature System.Threading.Tasks.Task`1[Microsoft.AspNetCore.Mvc.IActionResult] GetTodoById(System.Guid) on controller Server.Controllers.TodosController (Server).
    [22:30:41 INF] Executing OkObjectResult, writing value of type 'System.Guid'.
    [22:30:41 INF] Executed action Server.Controllers.TodosController.GetTodoById (Server) in 34.6663ms
    [22:30:41 INF] Executed endpoint 'Server.Controllers.TodosController.GetTodoById (Server)'
    [22:30:41 INF] Request finished HTTP/2 GET https://localhost:5001/Todos/00119201-3fed-4741-8295-48d697790729 - - - 200 - application/json;+charset=utf-8 121.1434ms
    
    我认为这不是理想的行为(如果日志级别是信息而不是跟踪)。我错过了什么?如何防止 Serilog 在不被要求的情况下进行日志记录?
    我已经检查过这个问题 Serilog - MinimumLoggingLevel Information shows every request in log - is that normal?但这没有帮助。未安装 Serilog.Web.Classic 包。

    最佳答案

    这些日志由级别为 Information 的 ASP .NET 主机写入。因此 Serilog 将它们接收为 Information以及。
    您可以做的是覆盖 MinimumLevel为不同的来源,让您可以不断收到Information某些来源编写的级日志,同时忽略 Information主机写入的级别日志。
    这是一个例子:

  • 套装整体MinimumLevelInformation
  • 覆盖 MinimumLevelWarning仅适用于来自 Microsoft 的日志来源
  • 覆盖 MinimumLevelInformation仅适用于来自 Microsoft.Hosting.Lifetime 的日志来源

  • 例如
    Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Information()
        .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
        .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
        .WriteTo.Console()
        .CreateLogger();
    
    您还可以通过 appSettings.json 配置这些源覆盖:
    {
      "Serilog": {
        "MinimumLevel": {
          "Default": "Information",
          "Override": {
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Information"
          }
        }
      },
      "AllowedHosts": "*"
    }
    

    关于c# - 如何防止 Serilog 将每一个小步骤自动记录为信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68718377/

    相关文章:

    c# - 如何通过 Xpath 按下一个元素

    c# - 我在哪里可以找到 .net 4.0 SOS.dll?

    serilog - 有没有办法指定特定的浓缩器只能用于特定的接收器?

    c# - 如何将 C# 6 与网站项目类型一起使用?

    c# - 窗口加载和 WPF

    c# - 替代 Group_Concat 以自定义现有 Entity Framework 模型

    .net - 程序包还原失败。回滚软件包更改-Serilog.AspNetCore

    asp.net-core - Serilog 中是否有一个选项可以在运行时更改日志文件参数,就像更改 LogLevel 一样?

    c# - 在 .NET Core 6.0 Razor 页面应用程序中配置 Serilog 的正确方法是什么?

    c# - 当我需要 ILambdaContext 时,如何在 AWS Lambda 中构建 Serilog 的 DI