代码之家  ›  专栏  ›  技术社区  ›  user3007447

仅当使用带日志级别警告的Serilog时,从DockerCompose启动Swagger需要5分钟以上

  •  0
  • user3007447  · 技术社区  · 2 年前

    我有一个使用ef-core的.net8最小API web项目。

    我已经配置了docker compose,我正在使用Serilog和Swagger UI。

    每当我将Serilog设置配置为将Microsoft的覆盖用作“信息”以外的任何东西时,Swagger UI都需要大约5分钟才能启动浏览器。然而,应用程序中的一切都很好,我可以毫无问题地导航到本地主机上的swagger页面,并进行API调用。

    请查看我的应用设置。开发.json

    "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"
      }
    

    我的应用程序设置类似,但Microsoft和系统设置为错误。除非是信息性的,否则将所有内容设置为相同是行不通的。

    我也尝试过使用和不使用“UseSerilogRequestLogging”扩展方法。

    以下是加载serilog的方法:

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

    以下是我如何加载swaggle和其他Serilog设置:

    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
    

    有什么想法吗?

    0 回复  |  直到 2 年前
    推荐文章