.Net Core实现健康检查的示例代码(2)

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app .UseRouting() .UseEndpoints(config => { config.MapHealthChecks("/health", new HealthCheckOptions { ResponseWriter=CustomResponseWriter }); }); } private static Task CustomResponseWriter(HttpContext context, HealthReport healthReport) { context.Response.ContentType = "application/json"; var result = JsonConvert.SerializeObject(new { status = healthReport.Status.ToString(), errors = healthReport.Entries.Select(e => new { key = e.Key, value = e.Value.Status.ToString() }) }); return context.Response.WriteAsync(result); }

.Net Core实现健康检查的示例代码

现在以json显示我们的详细信息,完成了健康状态的检查.

健康检查界面

Install-Package AspNetCore.HealthChecks.UI

安装完成后,需要相应地在ConfigureServices()和Configure()方法中调用相应的服务方法。

public void ConfigureServices(IServiceCollection services) { services.AddHealthChecksUI(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseHealthChecksUI(); }

配置完成后,您可以运行应用程序并指向/ healthchecks-ui地址,该端点显示如下的UI.

.Net Core实现健康检查的示例代码

但是界面上没有我们刚才自定义的,那我们在进行配置

Appsetting.json

{ "ApplicationInsights": { "InstrumentationKey": "your-instrumentation-key" }, "Logging": { "LogLevel": { "Default": "Warning" } }, "AllowedHosts": "*", "HealthChecksUI": { "HealthChecks": [ { "Name": "Test Health", "Uri": "https://localhost:44342/health" } ], "EvaluationTimeinSeconds": 10, "MinimumSecondsBetweenFailureNotifications": 60 } }

.Net Core实现健康检查的示例代码

这样就可以看到健康状态了

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdswgg.html