Skip to content

Commit 00953e8

Browse files
committed
Add HealthChecks.
1 parent 7c9bb65 commit 00953e8

File tree

4 files changed

+114
-2
lines changed

4 files changed

+114
-2
lines changed

src/CmsKitDemo/CmsKitDemo.csproj

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<ItemGroup>
1111
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
1212
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
13+
<PackageReference Include="AspNetCore.HealthChecks.UI" Version="7.0.2" />
14+
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="7.0.2" />
15+
<PackageReference Include="AspNetCore.HealthChecks.UI.InMemory.Storage" Version="7.0.0" />
1316
</ItemGroup>
1417

1518
<ItemGroup>
@@ -84,7 +87,7 @@
8487
<PackageReference Include="Volo.CmsKit.HttpApi" Version="7.4.0-rc.2" />
8588
<PackageReference Include="Volo.CmsKit.Web" Version="7.4.0-rc.2" />
8689
</ItemGroup>
87-
90+
8891
<ItemGroup>
8992
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.1">
9093
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>

src/CmsKitDemo/CmsKitDemoModule.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.Extensions.DependencyInjection.Extensions;
33
using Microsoft.OpenApi.Models;
44
using CmsKitDemo.Data;
5+
using CmsKitDemo.HealthChecks;
56
using CmsKitDemo.Localization;
67
using CmsKitDemo.Menus;
78
using OpenIddict.Validation.AspNetCore;
@@ -173,6 +174,7 @@ public override void ConfigureServices(ServiceConfigurationContext context)
173174
ConfigureEfCore(context);
174175
ConfigureRazorPages();
175176
ConfigureCmsKit(context);
177+
ConfigureHealthChecks(context);
176178
}
177179

178180
private void ConfigureAuthentication(ServiceConfigurationContext context)
@@ -358,7 +360,7 @@ private void ConfigureCmsKit(ServiceConfigurationContext context)
358360
Configure<CmsKitCommentOptions>(options =>
359361
{
360362
options.EntityTypes.Add(new CommentEntityTypeDefinition(CmsKitDemoConsts.ImageGalleryEntityType));
361-
options.IsRecaptchaEnabled = true;
363+
options.IsRecaptchaEnabled = true;
362364
});
363365
}
364366

@@ -369,6 +371,11 @@ public override async Task OnPreApplicationInitializationAsync(ApplicationInitia
369371
await context.ServiceProvider.GetRequiredService<CmsKitDemoDbMigrationService>().MigrateAsync(connString);
370372
}
371373

374+
private void ConfigureHealthChecks(ServiceConfigurationContext context)
375+
{
376+
context.Services.AddCmsKitDemoHealthChecks();
377+
}
378+
372379
public override void OnApplicationInitialization(ApplicationInitializationContext context)
373380
{
374381
var app = context.GetApplicationBuilder();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.Extensions.Diagnostics.HealthChecks;
2+
using Volo.Abp.DependencyInjection;
3+
using Volo.Abp.Identity;
4+
using IdentityRole = Microsoft.AspNetCore.Identity.IdentityRole;
5+
6+
namespace CmsKitDemo.HealthChecks;
7+
8+
public class CmsKitDemoDatabaseCheck : IHealthCheck, ITransientDependency
9+
{
10+
protected readonly IIdentityRoleRepository IdentityRoleRepository;
11+
12+
public CmsKitDemoDatabaseCheck(IIdentityRoleRepository identityRoleRepository)
13+
{
14+
IdentityRoleRepository = identityRoleRepository;
15+
}
16+
17+
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
18+
{
19+
try
20+
{
21+
await IdentityRoleRepository.GetListAsync(sorting: nameof(IdentityRole.Id), maxResultCount: 1, cancellationToken: cancellationToken);
22+
23+
return HealthCheckResult.Healthy($"Could connect to database and get record.");
24+
}
25+
catch (Exception e)
26+
{
27+
return HealthCheckResult.Unhealthy($"Error when trying to get database record. ", e);
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using HealthChecks.UI.Client;
2+
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
3+
4+
namespace CmsKitDemo.HealthChecks;
5+
6+
public static class HealthChecksBuilderExtensions
7+
{
8+
public static void AddCmsKitDemoHealthChecks(this IServiceCollection services)
9+
{
10+
// Add your health checks here
11+
var healthChecksBuilder = services.AddHealthChecks();
12+
healthChecksBuilder.AddCheck<CmsKitDemoDatabaseCheck>("CmsKitDemo DbContext Check", tags: new string[] { "database" });
13+
14+
services.ConfigureHealthCheckEndpoint("/health-status");
15+
16+
// If you don't want to add HealthChecksUI, remove following configurations.
17+
var configuration = services.GetConfiguration();
18+
var healthCheckUrl = configuration["App:HealthCheckUrl"];
19+
20+
if (string.IsNullOrEmpty(healthCheckUrl))
21+
{
22+
healthCheckUrl = "/health-status";
23+
}
24+
25+
var healthChecksUiBuilder = services.AddHealthChecksUI(settings =>
26+
{
27+
settings.AddHealthCheckEndpoint("CmsKitDemo Health Status", healthCheckUrl);
28+
});
29+
30+
// Set your HealthCheck UI Storage here
31+
healthChecksUiBuilder.AddInMemoryStorage();
32+
33+
services.MapHealthChecksUiEndpoints(options =>
34+
{
35+
options.UIPath = "/health-ui";
36+
options.ApiPath = "/health-api";
37+
});
38+
}
39+
40+
private static IServiceCollection ConfigureHealthCheckEndpoint(this IServiceCollection services, string path)
41+
{
42+
services.Configure<AbpEndpointRouterOptions>(options =>
43+
{
44+
options.EndpointConfigureActions.Add(endpointContext =>
45+
{
46+
endpointContext.Endpoints.MapHealthChecks(
47+
new PathString(path.EnsureStartsWith('/')),
48+
new HealthCheckOptions
49+
{
50+
Predicate = _ => true,
51+
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse,
52+
AllowCachingResponses = false,
53+
});
54+
});
55+
});
56+
57+
return services;
58+
}
59+
60+
private static IServiceCollection MapHealthChecksUiEndpoints(this IServiceCollection services, Action<global::HealthChecks.UI.Configuration.Options>? setupOption = null)
61+
{
62+
services.Configure<AbpEndpointRouterOptions>(routerOptions =>
63+
{
64+
routerOptions.EndpointConfigureActions.Add(endpointContext =>
65+
{
66+
endpointContext.Endpoints.MapHealthChecksUI(setupOption);
67+
});
68+
});
69+
70+
return services;
71+
}
72+
}

0 commit comments

Comments
 (0)