我有一个用Dot Net Core 3.1编写的Web API应用程序,具有对MongoDB的运行状况检查。我正在使用AspNetCore.HealthChecks.MongoDb(2.2.2)nuget进行Mongo健康检查。我们注意到我们的生产日志中充斥着错误,提示新的MongoClient无法添加到字典中。你能帮忙吗?
这是在启动时编写的用于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());
}