Asp.NetCore 添加健康检查
打开Startup.cs文件
在ConfigureServices中添加 services.AddHealthChecks();
public void ConfigureServices(IServiceCollection services) { services.AddHealthChecks(); services.AddControllers(); }
在Configure中添加健康检查路径
Net Core 2.2 如下:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } //健康检查 app.UseHealthChecks("/health"); }
Net Core 3.x 如下:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapHealthChecks("/health"); }); }
F5运行,访问https://localhost:5001/health