0

我有一个用 Dot Net Core 3.1 编写的 Web API 应用程序,对 MongoDB 进行健康检查。我正在使用 AspNetCore.HealthChecks.MongoDb (2.2.2) nuget 进行 Mongo 健康检查。我们注意到我们的生产日志充满了错误,说New MongoClient can't be added into dictionary。能否请你帮忙?

这是在启动时为 MongoDB 健康检查编写的代码,

public void ConfigureContainer(ServiceRegistry services)
{
   services.AddHealthChecks()
                .AddMongoDb(config.ConnectionStrings.MongoDbContext.ThrowIfNull("MongoDbContext"),
                            config.AppSettings.MongoDbName.ThrowIfNull("MongoDbName"), null, null, null);
}


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
     app.UseHealthChecks("/api/v1.0/health", new HealthCheckOptions()
     {
        ResponseWriter = WriteResponse
     });
}

private static Task WriteResponse(HttpContext httpContext, HealthReport result)
{
       httpContext.Response.ContentType = "application/json";
       if (result.Status != HealthStatus.Healthy)
       {
                IReadOnlyDictionary<string, HealthReportEntry> healthResult = result.Entries;
                if (healthResult != null)
                {
                    string json = new JObject(healthResult.Select(pair =>
                            new JProperty(pair.Key, new JObject(
                                new JProperty("Status", pair.Value.Status.ToString()),
                                new JProperty("Description", pair.Value.Description?.ToString(CultureInfo.InvariantCulture)),
                                new JProperty("Exception", pair.Value.Exception?.ToString()
                                ))))).ToString(Formatting.Indented);
                    Log.Error("Error: {json}", json);
                    return httpContext.Response.WriteAsync($"{result.Status.ToString()} {Environment.NewLine} {json}");
                }
       }
       return httpContext.Response.WriteAsync(result.Status.ToString());
}
4

0 回答 0