You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Compose中Swagger启动超5分钟:仅Serilog日志级别非Information时出现

问题描述

我有一个基于.NET 8的Minimal API项目,已配置Docker Compose,同时使用Serilog日志框架和Swagger UI。遇到以下异常现象:

  • 当Serilog配置中Microsoft日志级别覆盖设为非Information(比如Error)时,Swagger UI自动启动浏览器需要耗时约5分钟。
  • 应用本身运行正常,期间可以手动访问本地Swagger页面并正常调用API。

相关配置与代码

appsettings.Development.json的Serilog配置

"Serilog": {
  "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
  "MinimumLevel": {
    "Default": "Information",
    "Override": {
      "Microsoft": "Error",
      "System": "Error"
    }
  },
  "WriteTo": [
    { "Name": "Console" },
    {
      "Name": "File",
      "Args": {
        "path": "/app/Logs/log-.txt",
        "rollingInterval": "Day"
      }
    }
  ],
  "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
  "Properties": {
    "Application": "myapp"
  }
}

appsettings.json配置类似,但Microsoft和System的级别同样设为Error;仅当将级别设为Information时,启动延迟问题消失。

Serilog初始化代码

builder.Host.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
    .ReadFrom.Configuration(hostingContext.Configuration));

Swagger与应用初始化代码

public static class SwaggerConfiguration
{
    public static void ConfigureSwagger(IServiceCollection services)
    {
        services.AddEndpointsApiExplorer();
        services.AddSwaggerGen();
    }
}

/// <summary>
/// Class for application initialization
/// </summary>
public static class ApplicationInitialization
{
    public static void Initialize(WebApplication app)
    {
        EndpointMapper.MapAllEndpoints(app);

        if (app.Environment.IsDevelopment())
        {
            app.UseSwagger();
            app.UseSwaggerUI();
        }

        app.UseSerilogRequestLogging();
    }
}

日志信息

启动时间戳日志

17:17:36:967    docker exec -i 27c03df1adf3 /bin/sh -c "if PID=$(pidof dotnet); then kill $PID; fi"
17:17:41:924    ========== Debugging ==========
17:17:42:592    docker ps --filter "status=running" --filter "label=com.docker.compose.service" --filter "name=^/myapp$" --format {{.ID}} -n 1
17:17:42:749    27c03df1adf3
17:22:44:600    Launching https://localhost:59757/swagger ...

应用日志文件内容

2023-12-18 22:12:42.363 +00:00 [INF] Now listening on: http://[::]:8080
2023-12-18 22:12:42.364 +00:00 [INF] Now listening on: https://[::]:8081
2023-12-18 22:12:42.366 +00:00 [INF] Application started. Press Ctrl+C to shut down.
2023-12-18 22:12:42.367 +00:00 [INF] Hosting environment: Development
2023-12-18 22:12:42.367 +00:00 [INF] Content root path: /app
2023-12-18 22:12:42.830 +00:00 [INF] Request starting HTTP/2 GET https://localhost:59757/swagger/index.html - null null
2023-12-18 22:12:43.000 +00:00 [INF] Request finished HTTP/2 GET https://localhost:59757/swagger/index.html - 200 null text/html;charset=utf-8 170.7554ms
2023-12-18 22:12:43.159 +00:00 [INF] Request starting HTTP/2 GET https://localhost:59757/swagger/v1/swagger.json - null null
2023-12-18 22:12:43.166 +00:00 [INF] No action descriptors found. This may indicate an incorrectly configured application or missing application parts. To learn more, visit https://aka.ms/aspnet/mvc/app-parts
2023-12-18 22:12:43.273 +00:00 [INF] Request finished HTTP/2 GET https://localhost:59757/swagger/v1/swagger.json - 200 null application/json;charset=utf-8 113.6402ms
排查思路
  • 排查Docker容器内的网络/资源加载延迟

    • 进入Docker容器,用curl手动请求Swagger UI依赖的外部资源(比如CDN上的swagger-ui.js、swagger-ui.css),看是否存在超时或高延迟。
    • 临时调整Serilog配置,单独把Microsoft.AspNetCore的日志级别设为Information,其他Microsoft模块保持Error,缩小日志范围,查看是否有网络请求相关的延迟日志。
  • 检查Swagger UI的资源加载阻塞

    • 当浏览器最终打开后,打开开发者工具的Network面板,查看所有资源的加载耗时,确认是否有资源超时后重试的情况。
    • 尝试启用Swagger UI的本地资源模式(禁用CDN):在UseSwaggerUI配置中注入本地的CSS和JS文件,排除CDN资源加载慢的问题。
  • 验证IDE调试工具的就绪检测逻辑

    • 从日志时间差(应用22:12处理完请求,IDE17:22启动浏览器,对应UTC+5时区)来看,可能是IDE在检测服务就绪时出现延迟。手动在应用启动后立即访问Swagger地址,对比IDE自动启动的时间差,判断延迟来源。
    • 检查Docker端口映射配置,确认本地端口与容器端口的映射是否存在异常,导致IDE无法及时检测到服务就绪。
  • 排查Serilog与ASP.NET Core日志系统的交互问题

    • 尝试将Serilog的初始化时机提前,或者临时移除Serilog,使用默认日志系统并设置Microsoft级别为Error,看延迟问题是否消失,确认是否是Serilog的配置冲突导致。
    • 移除UseSerilogRequestLogging后再次测试,结合调整日志级别重新验证。
  • 检查Minimal API端点映射的隐藏延迟

    • 日志中出现No action descriptors found提示,虽然Swagger能正常返回,但可能端点映射过程存在隐藏延迟。单独开启Microsoft.AspNetCore.Routing的日志级别为Information,查看端点映射的详细过程是否有异常耗时。

内容的提问来源于stack exchange,提问作者user3007447

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 18:17:06