Skip to content

Commit d75e77b

Browse files
committed
Some peer feedback addressed
1 parent a2a74fc commit d75e77b

File tree

4 files changed

+63
-47
lines changed

4 files changed

+63
-47
lines changed

docs/core/diagnostics/diagnostic-libraries.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Diagnostic libraries
33
description: An overview of the available diagnostic libraries in .NET.
4-
ms.date: 10/16/2023
4+
ms.date: 10/20/2023
55
ms.topic: overview
66
---
77

docs/core/diagnostics/snippets/exception-summary/Program.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
// Get the exception summarizer.
1313
var summarizer = provider.GetRequiredService<IExceptionSummarizer>();
1414

15-
foreach (var exception in new Exception[]
16-
{
17-
new OperationCanceledException("Operation cancelled..."),
18-
new TaskCanceledException("Task cancelled..."),
19-
new SocketException(10_024, "Too many sockets open..."),
20-
new WebException("Keep alive failure...",
21-
WebExceptionStatus.KeepAliveFailure)
22-
})
15+
// Define exceptions to summarize.
16+
Exception[] exceptions =
17+
[
18+
new OperationCanceledException("Operation cancelled..."),
19+
new TaskCanceledException("Task cancelled..."),
20+
new SocketException(10_024, "Too many sockets open..."),
21+
new WebException("Keep alive failure...",
22+
WebExceptionStatus.KeepAliveFailure)
23+
];
24+
25+
foreach (var exception in exceptions)
2326
{
2427
// Summarize the exception.
2528
var summary = summarizer.Summarize(exception);
@@ -28,9 +31,3 @@
2831
}
2932

3033
Console.ReadLine();
31-
32-
// Sample output:
33-
// OperationCanceledException:TaskTimeout:Unknown
34-
// TaskCanceledException:TaskTimeout:Unknown
35-
// SocketException:TooManyOpenSockets:Unknown
36-
// WebException:KeepAliveFailure:Unknown
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Runtime.CompilerServices;
2+
using Microsoft.Extensions.Diagnostics.HealthChecks;
3+
using Microsoft.Extensions.Hosting;
4+
using Microsoft.Extensions.Logging;
5+
6+
internal class ExampleLifecycle(
7+
HealthCheckService healthCheckService,
8+
ILogger<ExampleLifecycle> logger) : IHostedLifecycleService
9+
{
10+
Task IHostedService.StartAsync(CancellationToken cancellationToken) =>
11+
CheckHealthAsync(cancellationToken: cancellationToken);
12+
13+
Task IHostedLifecycleService.StartedAsync(CancellationToken cancellationToken) =>
14+
CheckHealthAsync(cancellationToken: cancellationToken);
15+
16+
Task IHostedLifecycleService.StartingAsync(CancellationToken cancellationToken) =>
17+
CheckHealthAsync(cancellationToken: cancellationToken);
18+
19+
Task IHostedService.StopAsync(CancellationToken cancellationToken) =>
20+
CheckHealthAsync(cancellationToken: cancellationToken);
21+
22+
Task IHostedLifecycleService.StoppedAsync(CancellationToken cancellationToken) =>
23+
CheckHealthAsync(cancellationToken: cancellationToken);
24+
25+
Task IHostedLifecycleService.StoppingAsync(CancellationToken cancellationToken) =>
26+
CheckHealthAsync(cancellationToken: cancellationToken);
27+
28+
public Task PostStartedAndPreStoppingAsync() => CheckHealthAsync();
29+
30+
private async Task CheckHealthAsync(
31+
[CallerMemberName] string eventName = "",
32+
CancellationToken cancellationToken = default)
33+
{
34+
var result =
35+
await healthCheckService.CheckHealthAsync(cancellationToken);
36+
37+
logger.LogInformation(
38+
"{EventName}: {Status} {TotalDuration}",
39+
eventName, result.Status, result.TotalDuration);
40+
}
41+
}

docs/core/diagnostics/snippets/lifetime-health-checks/Program.cs

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,22 @@
55
var builder = Host.CreateApplicationBuilder(args);
66

77
var healthChecksBuilder = builder.Services
8+
.AddSingleton<ExampleLifecycle>()
9+
.AddHostedService<ExampleLifecycle>()
810
.AddHealthChecks()
911
.AddApplicationLifecycleHealthCheck();
1012

13+
// You could use the healthChecksBuilder instance to add more checks...
14+
1115
var app = builder.Build();
1216

13-
var lifetime = app.Services.GetRequiredService<IHostApplicationLifetime>();
14-
var healthCheckService = app.Services.GetRequiredService<HealthCheckService>();
17+
var example = app.Services.GetRequiredService<ExampleLifecycle>();
1518

16-
void ReportHealthCheck(string eventName)
19+
async Task DelayAndRepostAsync()
1720
{
18-
// ⚠️ Normally, you would await the result of CheckHealthAsync, but this
19-
// function is called in a synchronous context, so we block instead...
20-
var result = healthCheckService!.CheckHealthAsync().GetAwaiter().GetResult();
21-
22-
Console.WriteLine($"{eventName}: {result.Status} {result.TotalDuration}");
21+
// Ensure app started...
22+
await Task.Delay(500);
23+
await example.PostStartedAndPreStoppingAsync();
2324
}
2425

25-
lifetime.ApplicationStarted.Register(() => ReportHealthCheck("Started"));
26-
lifetime.ApplicationStopping.Register(() => ReportHealthCheck("Stopping"));
27-
lifetime.ApplicationStopped.Register(() => ReportHealthCheck("Stopped"));
28-
29-
app.Run();
30-
31-
// Sample output:
32-
// info: Microsoft.Hosting.Lifetime[0]
33-
// Application started. Press Ctrl+C to shut down.
34-
// info: Microsoft.Hosting.Lifetime[0]
35-
// Hosting environment: Production
36-
// info: Microsoft.Hosting.Lifetime[0]
37-
// Content root path: .\lifecycle-health-checks\bin\Debug\net8.0
38-
// Started: Healthy 00:00:00.0159571
39-
// info: Microsoft.Hosting.Lifetime[0]
40-
// Application is shutting down...
41-
// fail: Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService[103]
42-
// Health check ApplicationLifecycle with status
43-
// Unhealthy completed after 0.0067ms with message 'Stopping'
44-
// Stopping: Unhealthy 00:00:00.0307552
45-
// fail: Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService[103]
46-
// Health check ApplicationLifecycle with status
47-
// Unhealthy completed after 0.0052ms with message 'Stopping'
48-
// Stopped: Unhealthy 00:00:00.0015680
26+
await Task.WhenAll(DelayAndRepostAsync(), app.RunAsync());

0 commit comments

Comments
 (0)