Skip to content

Commit

Permalink
Add Builder Function for HealthChecks
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmanuelthiel committed Nov 27, 2023
1 parent ed56937 commit 0ebb412
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
21 changes: 7 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,11 @@ You can add additional health checks to the default setup.
- [Entity Framework Core DbContext probes](https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-6.0#entity-framework-core-dbcontext-probe)

```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddDefaultSetup(_options);

// Health Checks
services
.AddHealthChecks()
.AddCheck("MyHealthCheck", () => HealthCheckResult.Healthy("Everything is fine.")
.AddCheck("MyOtherHealthCheck", MyHealthChecker) // Implement IHealthCheck
.AddSqlServer("<MY_CONNECTION_STRING>")
.AddDbContextCheck<SampleDbContext>();

// ...
}
_options.ConfigureHealthChecks(builder => {
builder
.AddCheck("MyHealthCheck", () => HealthCheckResult.Healthy("Everything is fine.")
.AddCheck("MyOtherHealthCheck", MyHealthChecker) // Implement IHealthCheck
.AddSqlServer("<MY_CONNECTION_STRING>")
.AddDbContextCheck<SampleDbContext>();
});
```
4 changes: 3 additions & 1 deletion src/Wemogy.AspNet/Startup/StartupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public static void AddDefaultSetup(this IServiceCollection serviceCollection, St

serviceCollection.AddDefaultControllers(options.DaprEnvironment != null, options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes);

serviceCollection.AddHealthChecks();
// Health checks
var healthChecksBuilder = serviceCollection.AddHealthChecks();
options.ConfigureHealthCheckBuilder?.Invoke(healthChecksBuilder);

serviceCollection.AddDefaultRouting();
}
Expand Down
8 changes: 8 additions & 0 deletions src/Wemogy.AspNet/Startup/StartupOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Wemogy.AspNet.Dapr;
using Wemogy.AspNet.Monitoring;
using Wemogy.AspNet.Swagger;
Expand All @@ -15,6 +16,7 @@ public class StartupOptions
internal MonitoringEnvironment? MonitoringEnvironment { get; private set; }
internal DaprEnvironment? DaprEnvironment { get; private set; }
internal HashSet<Type> Middlewares { get; private set; }
internal Action<IHealthChecksBuilder>? ConfigureHealthCheckBuilder { get; private set; }

/// <summary>
/// Sets the <see cref="Microsoft.AspNetCore.Mvc.MvcOptions.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes"/> property for all contollers.
Expand Down Expand Up @@ -68,4 +70,10 @@ public StartupOptions AddMiddleware<TMiddleware>()
Middlewares.Add(typeof(TMiddleware));
return this;
}

public StartupOptions ConfigureHealthChecks(Action<IHealthChecksBuilder> configure)
{
ConfigureHealthCheckBuilder = configure;
return this;
}
}

0 comments on commit 0ebb412

Please sign in to comment.