|
1 |
| -namespace NET6CustomLibrary.Extensions; |
| 1 | +using HealthChecks.UI.Client; |
| 2 | + |
| 3 | +namespace NET6CustomLibrary.Extensions; |
2 | 4 |
|
3 | 5 | public static class DependencyInjection
|
4 | 6 | {
|
@@ -348,6 +350,92 @@ public static IEndpointRouteBuilder AddDatabaseHealthChecks(this IEndpointRouteB
|
348 | 350 | }
|
349 | 351 | #endregion
|
350 | 352 |
|
| 353 | + #region "HEALTH CHECKS WITH UI" |
| 354 | + public static IServiceCollection AddHealthChecksSQLite<TDbContext>(this IServiceCollection services, string webAddressGroup, string webAddressTitle, string sqliteConnString) where TDbContext : DbContext |
| 355 | + { |
| 356 | + services.AddHealthChecks() |
| 357 | + .AddDbContextCheck<TDbContext>(name: "Application DB Context", failureStatus: HealthStatus.Degraded) |
| 358 | + .AddUrlGroup(new Uri(webAddressGroup), name: webAddressTitle, failureStatus: HealthStatus.Degraded) |
| 359 | + .AddSqlite(sqliteConnString); |
| 360 | + |
| 361 | + services.AddHealthChecksUI(setupSettings: setup => |
| 362 | + { |
| 363 | + setup.AddHealthCheckEndpoint("Health Check", $"/healthz"); |
| 364 | + }).AddInMemoryStorage(); |
| 365 | + |
| 366 | + return services; |
| 367 | + } |
| 368 | + |
| 369 | + public static IServiceCollection AddHealthChecksSQLServer<TDbContext>(this IServiceCollection services, string webAddressGroup, string webAddressTitle, string sqliteConnString) where TDbContext : DbContext |
| 370 | + { |
| 371 | + services.AddHealthChecks() |
| 372 | + .AddDbContextCheck<TDbContext>(name: "Application DB Context", failureStatus: HealthStatus.Degraded) |
| 373 | + .AddUrlGroup(new Uri(webAddressGroup), name: webAddressTitle, failureStatus: HealthStatus.Degraded) |
| 374 | + .AddSqlServer(sqliteConnString); |
| 375 | + |
| 376 | + services.AddHealthChecksUI(setupSettings: setup => |
| 377 | + { |
| 378 | + setup.AddHealthCheckEndpoint("Health Check", $"/healthz"); |
| 379 | + }).AddInMemoryStorage(); |
| 380 | + |
| 381 | + return services; |
| 382 | + } |
| 383 | + |
| 384 | + public static IServiceCollection AddHealthChecksMySQL<TDbContext>(this IServiceCollection services, string webAddressGroup, string webAddressTitle, string sqliteConnString) where TDbContext : DbContext |
| 385 | + { |
| 386 | + services.AddHealthChecks() |
| 387 | + .AddDbContextCheck<TDbContext>(name: "Application DB Context", failureStatus: HealthStatus.Degraded) |
| 388 | + .AddUrlGroup(new Uri(webAddressGroup), name: webAddressTitle, failureStatus: HealthStatus.Degraded) |
| 389 | + .AddMySql(sqliteConnString); |
| 390 | + |
| 391 | + services.AddHealthChecksUI(setupSettings: setup => |
| 392 | + { |
| 393 | + setup.AddHealthCheckEndpoint("Health Check", $"/healthz"); |
| 394 | + }).AddInMemoryStorage(); |
| 395 | + |
| 396 | + return services; |
| 397 | + } |
| 398 | + |
| 399 | + public static IServiceCollection AddHealthChecksPostgreSQL<TDbContext>(this IServiceCollection services, string webAddressGroup, string webAddressTitle, string sqliteConnString) where TDbContext : DbContext |
| 400 | + { |
| 401 | + services.AddHealthChecks() |
| 402 | + .AddDbContextCheck<TDbContext>(name: "Application DB Context", failureStatus: HealthStatus.Degraded) |
| 403 | + .AddUrlGroup(new Uri(webAddressGroup), name: webAddressTitle, failureStatus: HealthStatus.Degraded) |
| 404 | + .AddNpgSql(sqliteConnString); |
| 405 | + |
| 406 | + services.AddHealthChecksUI(setupSettings: setup => |
| 407 | + { |
| 408 | + setup.AddHealthCheckEndpoint("Health Check", $"/healthz"); |
| 409 | + }).AddInMemoryStorage(); |
| 410 | + |
| 411 | + return services; |
| 412 | + } |
| 413 | + |
| 414 | + public static WebApplication UseHealthChecksConfigure(this WebApplication app) |
| 415 | + { |
| 416 | + app.UseHealthChecks("/healthz", new HealthCheckOptions |
| 417 | + { |
| 418 | + Predicate = _ => true, |
| 419 | + ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse, |
| 420 | + ResultStatusCodes = |
| 421 | + { |
| 422 | + [HealthStatus.Healthy] = StatusCodes.Status200OK, |
| 423 | + [HealthStatus.Degraded] = StatusCodes.Status500InternalServerError, |
| 424 | + [HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable, |
| 425 | + }, |
| 426 | + }).UseHealthChecksUI(setup => |
| 427 | + { |
| 428 | + setup.ApiPath = "/healthcheck"; |
| 429 | + setup.UIPath = "/healthcheck-ui"; |
| 430 | + |
| 431 | + //https://github.com/Amitpnk/Onion-architecture-ASP.NET-Core/blob/develop/src/OA/Customization/custom.css |
| 432 | + //setup.AddCustomStylesheet("Customization/custom.css"); |
| 433 | + }); |
| 434 | + |
| 435 | + return app; |
| 436 | + } |
| 437 | + #endregion |
| 438 | + |
351 | 439 | #region "SEND EMAIL"
|
352 | 440 | public static IServiceCollection AddMailKitEmailSenderService(this IServiceCollection services, IConfiguration configuration)
|
353 | 441 | {
|
|
0 commit comments