| | | 1 | | using Microsoft.AspNetCore.Diagnostics.HealthChecks; |
| | | 2 | | |
| | | 3 | | namespace ProjectTemplate.Web.Extensions; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides extension methods for registering and mapping application health check endpoints. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class HealthCheckExtensions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Registers baseline ASP.NET Core health check services. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="services">The service collection used to register health checks.</param> |
| | | 14 | | /// <returns>The original <see cref="IServiceCollection"/> for chaining.</returns> |
| | | 15 | | public static IServiceCollection AddApplicationHealthChecks(this IServiceCollection services) |
| | | 16 | | { |
| | 148 | 17 | | services.AddHealthChecks(); |
| | | 18 | | |
| | 148 | 19 | | return services; |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Maps baseline health check endpoints for infrastructure, reverse proxies, and hosting platforms. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="app">The web application used to map health check endpoints.</param> |
| | | 26 | | /// <returns>The original <see cref="WebApplication"/> for chaining.</returns> |
| | | 27 | | public static WebApplication MapApplicationHealthChecks(this WebApplication app) |
| | | 28 | | { |
| | 128 | 29 | | app.MapHealthChecks("/health"); |
| | | 30 | | |
| | 128 | 31 | | app.MapHealthChecks("/health/ready", new HealthCheckOptions |
| | 128 | 32 | | { |
| | 0 | 33 | | Predicate = healthCheck => healthCheck.Tags.Contains("ready") |
| | 128 | 34 | | }); |
| | | 35 | | |
| | 128 | 36 | | app.MapHealthChecks("/health/live", new HealthCheckOptions |
| | 128 | 37 | | { |
| | 0 | 38 | | Predicate = _ => false |
| | 128 | 39 | | }); |
| | | 40 | | |
| | 128 | 41 | | return app; |
| | | 42 | | } |
| | | 43 | | } |