From f083e7a6e0207cb745f2ecf3e439236310c0827c Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Thu, 4 Jan 2024 14:37:53 -1000 Subject: [PATCH 1/8] DelegateHealthCheck /8 --- .../8.x/HealthChecksSample/HealthChecksSample.csproj | 8 ++++---- .../samples/8.x/HealthChecksSample/Snippets/Program.cs | 9 +++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) 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..d886ac19d2f7 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 @@ -166,9 +166,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); // } From e9b67e7b2491f6cad8fe67b58ec09c1bf3136d7c Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Thu, 4 Jan 2024 14:38:52 -1000 Subject: [PATCH 2/8] DelegateHealthCheck /8 --- .../HealthChecksSample/Snippets/Program.cs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) 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 d886ac19d2f7..7f47615fac0e 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 DelegateHealthCheck(_ => Task.FromResult(HealthCheckResult.Healthy(HealthyMessage))), + 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) { // From 08de566e2445bdd94738b73921ef356aa2b0afcf Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Tue, 9 Jan 2024 12:35:23 -1000 Subject: [PATCH 3/8] react to feedback --- .../8.x/HealthChecksSample/Snippets/Program.cs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) 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 7f47615fac0e..1062121c06a2 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 @@ -32,16 +32,10 @@ public static void MapHealthChecksComplete2(string[] args) var builder = WebApplication.CreateBuilder(args); builder.Services.AddHealthChecks() - .Add(new HealthCheckRegistration( - name: "SampleHealthCheck1", - instance: new DelegateHealthCheck(_ => Task.FromResult(HealthCheckResult.Healthy(HealthyMessage))), - failureStatus: null, + .AddAsyncCheck(name: "SampleHealthCheck1", + check: _ => Task.FromResult(HealthCheckResult.Healthy("HealthyMessage")), tags: null, - timeout: default) - { - Delay = TimeSpan.FromSeconds(40), - Period = TimeSpan.FromSeconds(30) - }); + timeout: default); var app = builder.Build(); From dba0fc7f966756a10b2661641e1cd8bccc093831 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Tue, 9 Jan 2024 13:23:39 -1000 Subject: [PATCH 4/8] DelegateHealthCheck /8 --- aspnetcore/host-and-deploy/health-checks.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/host-and-deploy/health-checks.md b/aspnetcore/host-and-deploy/health-checks.md index a012330e2444..a5f69b79e193 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 From 8735766283f5255456b5b3b79300d9bf82e697d4 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Tue, 9 Jan 2024 14:07:22 -1000 Subject: [PATCH 5/8] DelegateHealthCheck /8 --- aspnetcore/host-and-deploy/health-checks.md | 16 ++++++++++++---- .../8.x/HealthChecksSample/Snippets/Program.cs | 12 +++++++++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/aspnetcore/host-and-deploy/health-checks.md b/aspnetcore/host-and-deploy/health-checks.md index a5f69b79e193..cd4478cbd602 100644 --- a/aspnetcore/host-and-deploy/health-checks.md +++ b/aspnetcore/host-and-deploy/health-checks.md @@ -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 + +The [DelegateHealthCheck class](https://github.com/dotnet/aspnetcore/blob/main/src/HealthChecks/HealthChecks/src/DelegateHealthCheck.cs) enables setting [`Delay` and `Period`](https://github.com/dotnet/aspnetcore/blob/main/src/HealthChecks/Abstractions/src/HealthCheckRegistration.cs#L161-L185) on each health check individually. This is useful when you want to run some health checks more frequently 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_HealthCheckPublisherOptionsService"::: ## Dependency Injection and Health Checks 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 1062121c06a2..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 @@ -32,10 +32,16 @@ public static void MapHealthChecksComplete2(string[] args) var builder = WebApplication.CreateBuilder(args); builder.Services.AddHealthChecks() - .AddAsyncCheck(name: "SampleHealthCheck1", - check: _ => Task.FromResult(HealthCheckResult.Healthy("HealthyMessage")), + .Add(new HealthCheckRegistration( + name: "SampleHealthCheck1", + instance: new SampleHealthCheck(), + failureStatus: null, tags: null, - timeout: default); + timeout: default) + { + Delay = TimeSpan.FromSeconds(40), + Period = TimeSpan.FromSeconds(30) + }); var app = builder.Build(); From 17066cc3c734d27af634d5a502e3fa75878cec34 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Tue, 9 Jan 2024 14:13:20 -1000 Subject: [PATCH 6/8] DelegateHealthCheck /8 --- aspnetcore/host-and-deploy/health-checks.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aspnetcore/host-and-deploy/health-checks.md b/aspnetcore/host-and-deploy/health-checks.md index cd4478cbd602..e48d5e7af4c9 100644 --- a/aspnetcore/host-and-deploy/health-checks.md +++ b/aspnetcore/host-and-deploy/health-checks.md @@ -296,11 +296,11 @@ The following example registers a health check publisher as a singleton and conf ### Individual Healthchecks -The [DelegateHealthCheck class](https://github.com/dotnet/aspnetcore/blob/main/src/HealthChecks/HealthChecks/src/DelegateHealthCheck.cs) enables setting [`Delay` and `Period`](https://github.com/dotnet/aspnetcore/blob/main/src/HealthChecks/Abstractions/src/HealthCheckRegistration.cs#L161-L185) on each health check individually. This is useful when you want to run some health checks more frequently than the period set in . +The [DelegateHealthCheck class](https://github.com/dotnet/aspnetcore/blob/main/src/HealthChecks/HealthChecks/src/DelegateHealthCheck.cs) enables setting [`Delay` and `Period`](https://github.com/dotnet/aspnetcore/blob/main/src/HealthChecks/Abstractions/src/HealthCheckRegistration.cs#L161-L185) on each health check individually. This is useful when you want to run some health checks more frequently 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_HealthCheckPublisherOptionsService"::: +:::code language="csharp" source="~/host-and-deploy/health-checks/samples/8.x/HealthChecksSample/Snippets/Program.cs" id="snippet_MapHealthChecksComplete2"::: ## Dependency Injection and Health Checks From 37ec787bc755ee1948e060d5fa39bfb3964f9183 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Wed, 10 Jan 2024 09:00:51 -1000 Subject: [PATCH 7/8] react to feedback --- aspnetcore/host-and-deploy/health-checks.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/aspnetcore/host-and-deploy/health-checks.md b/aspnetcore/host-and-deploy/health-checks.md index e48d5e7af4c9..c6439d9fa766 100644 --- a/aspnetcore/host-and-deploy/health-checks.md +++ b/aspnetcore/host-and-deploy/health-checks.md @@ -296,7 +296,8 @@ The following example registers a health check publisher as a singleton and conf ### Individual Healthchecks -The [DelegateHealthCheck class](https://github.com/dotnet/aspnetcore/blob/main/src/HealthChecks/HealthChecks/src/DelegateHealthCheck.cs) enables setting [`Delay` and `Period`](https://github.com/dotnet/aspnetcore/blob/main/src/HealthChecks/Abstractions/src/HealthCheckRegistration.cs#L161-L185) on each health check individually. This is useful when you want to run some health checks more frequently than the period set in . +[`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 more frequently than the period set in . + The following code sets the `Delay` and `Period` for the `SampleHealthCheck1`: From 556e757800c37d2c3b88425e1cb55c8d1f6088b6 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Wed, 10 Jan 2024 09:06:31 -1000 Subject: [PATCH 8/8] react to feedback --- aspnetcore/host-and-deploy/health-checks.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/aspnetcore/host-and-deploy/health-checks.md b/aspnetcore/host-and-deploy/health-checks.md index c6439d9fa766..7315541b60d3 100644 --- a/aspnetcore/host-and-deploy/health-checks.md +++ b/aspnetcore/host-and-deploy/health-checks.md @@ -296,8 +296,7 @@ The following example registers a health check publisher as a singleton and conf ### 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 more frequently than the period set in . - +[`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`: