我有asp.net core 2.2项目,用于构建API。这些API依赖于其他一些服务,例如Azure CosmosDB,Azure ServiceBus,Azure存储服务。我有以下代码来实现HealthCheck:
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddCustomHealthChecks(Configuration);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseHealthChecks("/healthcheck", new HealthCheckOptions{Predicate = _ => true, ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse});
app.UseHealthChecksUI();
}
public static IServiceCollection AddCustomHealthChecks(this IServiceCollection services, IConfiguration configuration)
{
var cosmosDBServiceEndPoint = configuration.GetValue<string>("CosmosDBEndpoint");
var cosmosDBAuthKeyOrResourceToken = configuration.GetValue<string>("CosmosDBAccessKey");
var cosmosDBConnectionString = $"AccountEndpoint={cosmosDBServiceEndPoint};AccountKey={cosmosDBAuthKeyOrResourceToken};";
var hcBuilder = services.AddHealthChecks();
hcBuilder.AddCheck("self", () => HealthCheckResult.Healthy()).AddCosmosDb(connectionString: cosmosDBConnectionString, name: "CosmosDB-check", failureStatus: HealthStatus.Degraded, tags: new string[]{"cosmosdb"});
services.AddHealthChecksUI(setupSettings: setup =>
{
setup.AddHealthCheckEndpoint("Basic healthcheck", "http://localhost:63812/healthcheck");
}
);
return services;
}
关于验证以下URL:
http://localhost:63812/healthcheck
结果:
{
"status": "Healthy",
"totalDuration": "00:00:00.0277870",
"entries": {
"self": {
"data": {},
"duration": "00:00:00.0000026",
"status": "Healthy"
},
"cosmosDB-check": {
"data": {},
"duration": "00:00:00.0276920",
"status": "Healthy"
}
}
}
但是在检查HealthCheckUI时,我没有看到CosmosDB作为仪表板上的依赖项:http://localhost:63812/healthchecks-ui#/healthchecks
谁能帮助我知道如何解决此问题?
如您提供的图片中的描述所示,似乎您使用的是3.0.x版本程序包。因此,如果您的项目是asp.net核心应用2.2,则需要使用AspNetCore.HealthChecks.UI 2.2.X而非3.0.X版本。
也可以更改
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
services.AddCustomHealthChecks(Configuration);
}
到
public void ConfigureServices(IServiceCollection services)
{
services.AddCustomHealthChecks(Configuration);
}
有关更多详细信息,您可以参考如何使用Implement health checks in ASP.NET Core services。