diff --git a/aspnetcore/host-and-deploy/health-checks.md b/aspnetcore/host-and-deploy/health-checks.md index a012330e2444..7315541b60d3 100644 --- a/aspnetcore/host-and-deploy/health-checks.md +++ b/aspnetcore/host-and-deploy/health-checks.md @@ -5,7 +5,7 @@ description: Learn how to set up health checks for ASP.NET Core infrastructure, monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.custom: mvc -ms.date: 1/1/2024 +ms.date: 1/11/2024 uid: host-and-deploy/health-checks --- # Health checks in ASP.NET Core @@ -289,10 +289,18 @@ The following example registers a health check publisher as a singleton and conf :::code language="csharp" source="~/host-and-deploy/health-checks/samples/8.x/HealthChecksSample/Snippets/Program.cs" id="snippet_HealthCheckPublisherOptionsService"::: -> [!NOTE] -> [`AspNetCore.Diagnostics.HealthChecks`](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks) includes publishers for several systems, including [Application Insights](/azure/application-insights/app-insights-overview). -> -> [`AspNetCore.Diagnostics.HealthChecks`](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks) isn't maintained or supported by Microsoft. +[`AspNetCore.Diagnostics.HealthChecks`](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks): + +* Includes publishers for several systems, including [Application Insights](/azure/application-insights/app-insights-overview). +* Is ***not*** maintained or supported by Microsoft. + +### Individual Healthchecks + +[`Delay` and `Period`](https://github.com/dotnet/aspnetcore/blob/main/src/HealthChecks/Abstractions/src/HealthCheckRegistration.cs#L161-L185) can be set on each each individually. This is useful when you want to run some health checks at a different rate than the period set in . + +The following code sets the `Delay` and `Period` for the `SampleHealthCheck1`: + +:::code language="csharp" source="~/host-and-deploy/health-checks/samples/8.x/HealthChecksSample/Snippets/Program.cs" id="snippet_MapHealthChecksComplete2"::: ## Dependency Injection and Health Checks diff --git a/aspnetcore/host-and-deploy/health-checks/samples/8.x/HealthChecksSample/HealthChecksSample.csproj b/aspnetcore/host-and-deploy/health-checks/samples/8.x/HealthChecksSample/HealthChecksSample.csproj index 81341b8b6d7b..4bf7189c1440 100644 --- a/aspnetcore/host-and-deploy/health-checks/samples/8.x/HealthChecksSample/HealthChecksSample.csproj +++ b/aspnetcore/host-and-deploy/health-checks/samples/8.x/HealthChecksSample/HealthChecksSample.csproj @@ -1,15 +1,15 @@ - net7.0 + net8.0 enable enable - - - + + + diff --git a/aspnetcore/host-and-deploy/health-checks/samples/8.x/HealthChecksSample/Snippets/Program.cs b/aspnetcore/host-and-deploy/health-checks/samples/8.x/HealthChecksSample/Snippets/Program.cs index 8e658fd52242..15b6eb6cd8f5 100644 --- a/aspnetcore/host-and-deploy/health-checks/samples/8.x/HealthChecksSample/Snippets/Program.cs +++ b/aspnetcore/host-and-deploy/health-checks/samples/8.x/HealthChecksSample/Snippets/Program.cs @@ -26,6 +26,31 @@ public static void MapHealthChecksComplete(string[] args) // } + public static void MapHealthChecksComplete2(string[] args) + { + // + var builder = WebApplication.CreateBuilder(args); + + builder.Services.AddHealthChecks() + .Add(new HealthCheckRegistration( + name: "SampleHealthCheck1", + instance: new SampleHealthCheck(), + failureStatus: null, + tags: null, + timeout: default) + { + Delay = TimeSpan.FromSeconds(40), + Period = TimeSpan.FromSeconds(30) + }); + + var app = builder.Build(); + + app.MapHealthChecks("/healthz"); + + app.Run(); + // + } + public static void AddHealthChecks(WebApplicationBuilder builder) { // @@ -166,9 +191,14 @@ public static void AddHealthChecksUsingDependencyInjection(WebApplicationBuilder public static void AddHealthChecksSqlServer(WebApplicationBuilder builder) { // + var conStr = builder.Configuration.GetConnectionString("DefaultConnection"); + if (string.IsNullOrEmpty(conStr)) + { + throw new InvalidOperationException( + "Could not find a connection string named 'DefaultConnection'."); + } builder.Services.AddHealthChecks() - .AddSqlServer( - builder.Configuration.GetConnectionString("DefaultConnection")); + .AddSqlServer(conStr); // }