-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from abpframework/HealthCheck
Add `HealthChecks`.
- Loading branch information
Showing
4 changed files
with
114 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Volo.Abp.DependencyInjection; | ||
using Volo.Abp.Identity; | ||
using IdentityRole = Microsoft.AspNetCore.Identity.IdentityRole; | ||
|
||
namespace CmsKitDemo.HealthChecks; | ||
|
||
public class CmsKitDemoDatabaseCheck : IHealthCheck, ITransientDependency | ||
{ | ||
protected readonly IIdentityRoleRepository IdentityRoleRepository; | ||
|
||
public CmsKitDemoDatabaseCheck(IIdentityRoleRepository identityRoleRepository) | ||
{ | ||
IdentityRoleRepository = identityRoleRepository; | ||
} | ||
|
||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
await IdentityRoleRepository.GetListAsync(sorting: nameof(IdentityRole.Id), maxResultCount: 1, cancellationToken: cancellationToken); | ||
|
||
return HealthCheckResult.Healthy($"Could connect to database and get record."); | ||
} | ||
catch (Exception e) | ||
{ | ||
return HealthCheckResult.Unhealthy($"Error when trying to get database record. ", e); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/CmsKitDemo/HealthChecks/HealthChecksBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using HealthChecks.UI.Client; | ||
using Microsoft.AspNetCore.Diagnostics.HealthChecks; | ||
|
||
namespace CmsKitDemo.HealthChecks; | ||
|
||
public static class HealthChecksBuilderExtensions | ||
{ | ||
public static void AddCmsKitDemoHealthChecks(this IServiceCollection services) | ||
{ | ||
// Add your health checks here | ||
var healthChecksBuilder = services.AddHealthChecks(); | ||
healthChecksBuilder.AddCheck<CmsKitDemoDatabaseCheck>("CmsKitDemo DbContext Check", tags: new string[] { "database" }); | ||
|
||
services.ConfigureHealthCheckEndpoint("/health-status"); | ||
|
||
// If you don't want to add HealthChecksUI, remove following configurations. | ||
var configuration = services.GetConfiguration(); | ||
var healthCheckUrl = configuration["App:HealthCheckUrl"]; | ||
|
||
if (string.IsNullOrEmpty(healthCheckUrl)) | ||
{ | ||
healthCheckUrl = "/health-status"; | ||
} | ||
|
||
var healthChecksUiBuilder = services.AddHealthChecksUI(settings => | ||
{ | ||
settings.AddHealthCheckEndpoint("CmsKitDemo Health Status", healthCheckUrl); | ||
}); | ||
|
||
// Set your HealthCheck UI Storage here | ||
healthChecksUiBuilder.AddInMemoryStorage(); | ||
|
||
services.MapHealthChecksUiEndpoints(options => | ||
{ | ||
options.UIPath = "/health-ui"; | ||
options.ApiPath = "/health-api"; | ||
}); | ||
} | ||
|
||
private static IServiceCollection ConfigureHealthCheckEndpoint(this IServiceCollection services, string path) | ||
{ | ||
services.Configure<AbpEndpointRouterOptions>(options => | ||
{ | ||
options.EndpointConfigureActions.Add(endpointContext => | ||
{ | ||
endpointContext.Endpoints.MapHealthChecks( | ||
new PathString(path.EnsureStartsWith('/')), | ||
new HealthCheckOptions | ||
{ | ||
Predicate = _ => true, | ||
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse, | ||
AllowCachingResponses = false, | ||
}); | ||
}); | ||
}); | ||
|
||
return services; | ||
} | ||
|
||
private static IServiceCollection MapHealthChecksUiEndpoints(this IServiceCollection services, Action<global::HealthChecks.UI.Configuration.Options>? setupOption = null) | ||
{ | ||
services.Configure<AbpEndpointRouterOptions>(routerOptions => | ||
{ | ||
routerOptions.EndpointConfigureActions.Add(endpointContext => | ||
{ | ||
endpointContext.Endpoints.MapHealthChecksUI(setupOption); | ||
}); | ||
}); | ||
|
||
return services; | ||
} | ||
} |