-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHealthChecksBuilderExtensions.cs
More file actions
30 lines (25 loc) · 1.83 KB
/
HealthChecksBuilderExtensions.cs
File metadata and controls
30 lines (25 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Netcorext.Diagnostics.HealthChecks.MemoryCache;
namespace Microsoft.Extensions.DependencyInjection;
public static class HealthChecksBuilderExtensions
{
private const string NAME = "MemoryCache";
public static IHealthChecksBuilder AddMemoryCache(this IHealthChecksBuilder builder, Func<IServiceProvider, MemoryCacheHealthCheckerOptions> factory, string name = default, HealthStatus? failureStatus = default, IEnumerable<string> tags = default, TimeSpan? timeout = default)
{
return AddMemoryCache<MemoryCacheHealthChecker>(builder, factory, name, failureStatus, tags, timeout);
}
public static IHealthChecksBuilder AddMemoryCache<THealthChecker>(this IHealthChecksBuilder builder, Func<IServiceProvider, MemoryCacheHealthCheckerOptions> factory, string name = default, HealthStatus? failureStatus = default, IEnumerable<string> tags = default, TimeSpan? timeout = default)
{
if (factory == null) throw new ArgumentNullException(nameof(factory));
return builder.Add(new HealthCheckRegistration(name ?? NAME + "-" + typeof(THealthChecker).Name,
provider =>
{
var memoryCache = provider.GetRequiredService<IMemoryCache>();
return (IHealthCheck)Activator.CreateInstance(typeof(THealthChecker), factory.Invoke(provider), memoryCache);
},
failureStatus,
tags,
timeout));
}
}