Exemplo n.º 1
0
        /// <summary>
        /// Adds a new health check with the specified name and implementation.
        /// </summary>
        /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
        /// <param name="name">The name of the health check.</param>
        /// <param name="tags">A list of tags that can be used to filter health checks.</param>
        /// <param name="check">A delegate that provides the health check implementation.</param>
        /// <param name="timeout">An optional <see cref="TimeSpan"/> representing the timeout of the check.</param>
        /// <returns>The <see cref="IHealthChecksBuilder"/>.</returns>
        public static IHealthChecksBuilder AddCheck(
            this IHealthChecksBuilder builder,
            string name,
            Func <HealthCheckResult> check,
            IEnumerable <string>?tags = null,
            TimeSpan?timeout          = default)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (check == null)
            {
                throw new ArgumentNullException(nameof(check));
            }

            var instance = new DelegateHealthCheck((ct) => Task.FromResult(check()));

            return(builder.Add(new HealthCheckRegistration(name, instance, failureStatus: null, tags, timeout)));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds a new health check with the specified name and implementation.
        /// </summary>
        /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
        /// <param name="name">The name of the health check.</param>
        /// <param name="tags">A list of tags that can be used to filter health checks.</param>
        /// <param name="check">A delegate that provides the health check implementation.</param>
        /// <returns>The <see cref="IHealthChecksBuilder"/>.</returns>
        public static IHealthChecksBuilder AddAsyncCheck(
            this IHealthChecksBuilder builder,
            string name,
            Func <Task <HealthCheckResult> > check,
            IEnumerable <string> tags = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (check == null)
            {
                throw new ArgumentNullException(nameof(check));
            }

            var instance = new DelegateHealthCheck((ct) => check());

            return(builder.Add(new HealthCheckRegistration(name, instance, failureStatus: null, tags)));
        }
        public void TestDelegation()
        {
            var healthCheck = new DelegateHealthCheck("Test", () => Task.FromResult(HealthCheckStatus.Passed()));
            Assert.Equal("Test", healthCheck.Name);

            var result = healthCheck.Check().Result;
            Assert.False(result.HasFailed);
        }
        public void TestDelegation()
        {
            var healthCheck = new DelegateHealthCheck("Test", () => Task.FromResult(HealthCheckStatus.Passed()));

            Assert.Equal("Test", healthCheck.Name);

            var result = healthCheck.Check().Result;

            Assert.False(result.HasFailed);
        }
        public void AddCheck_Instance()
        {
            // Arrange
            var instance = new DelegateHealthCheck((_) =>
            {
                return Task.FromResult(HealthCheckResult.Healthy());
            });

            var services = CreateServices();
            services.AddHealthChecks().AddCheck("test", failureStatus: HealthStatus.Degraded,tags: new[] { "tag", }, instance: instance);

            var serviceProvider = services.BuildServiceProvider();

            // Act
            var options = serviceProvider.GetRequiredService<IOptions<HealthCheckServiceOptions>>().Value;

            // Assert
            var registration = Assert.Single(options.Registrations);
            Assert.Equal("test", registration.Name);
            Assert.Equal(HealthStatus.Degraded, registration.FailureStatus);
            Assert.Equal<string>(new[] { "tag", }, registration.Tags);
            Assert.Same(instance, registration.Factory(serviceProvider));
        }