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 

 

posted @ 2020-05-12 10:21  Anybt  阅读(677)  评论(1编辑  收藏  举报