Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 658ce8e

Browse files
committed
Implementazione HealthChecks per differenti database #32
closes #32
1 parent 8bece08 commit 658ce8e

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

src/NET6CustomLibrary/Extensions/DependencyInjection.cs

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace NET6CustomLibrary.Extensions;
1+
using HealthChecks.UI.Client;
2+
3+
namespace NET6CustomLibrary.Extensions;
24

35
public static class DependencyInjection
46
{
@@ -348,6 +350,92 @@ public static IEndpointRouteBuilder AddDatabaseHealthChecks(this IEndpointRouteB
348350
}
349351
#endregion
350352

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+
351439
#region "SEND EMAIL"
352440
public static IServiceCollection AddMailKitEmailSenderService(this IServiceCollection services, IConfiguration configuration)
353441
{

src/NET6CustomLibrary/NET6CustomLibrary.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
</PropertyGroup>
1717

1818
<ItemGroup>
19+
<PackageReference Include="AspNetCore.HealthChecks.MySql" Version="6.0.2" />
20+
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="6.0.2" />
21+
<PackageReference Include="AspNetCore.HealthChecks.Sqlite" Version="6.0.2" />
22+
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="6.0.2" />
23+
<PackageReference Include="AspNetCore.HealthChecks.UI" Version="6.0.5" />
24+
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="6.0.5" />
25+
<PackageReference Include="AspNetCore.HealthChecks.UI.InMemory.Storage" Version="6.0.5" />
26+
<PackageReference Include="AspNetCore.HealthChecks.Uris" Version="6.0.3" />
1927
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
2028
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
2129
<PackageReference Include="MailKit" Version="4.0.0" />
@@ -31,6 +39,8 @@
3139
<PrivateAssets>all</PrivateAssets>
3240
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3341
</PackageReference>
42+
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="6.0.16" />
43+
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="6.0.16" />
3444
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
3545
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="6.0.0" />
3646
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />

0 commit comments

Comments
 (0)