Пример #1
0
        /// <summary>
        /// Adds a cache status wrapper to the contained registrations
        /// </summary>
        /// <param name="builder">The health check builder</param>
        /// <param name="cachedBuilder">The builder that will register health checks to be cached</param>
        /// <param name="propertiesBuilder">The cache properties builder</param>
        /// <returns>The builder after changes</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IHealthCheckBuilder AddCached(this IHealthCheckBuilder builder,
                                                    Action <IHealthCheckBuilder> cachedBuilder, Func <IServiceProvider, CachedHealthCheckProperties> propertiesBuilder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (cachedBuilder == null)
            {
                throw new ArgumentNullException(nameof(cachedBuilder));
            }
            if (propertiesBuilder == null)
            {
                throw new ArgumentNullException(nameof(propertiesBuilder));
            }

            var cfg = new HealthCheckBuilder();

            cachedBuilder(cfg);

            foreach (var descriptor in cfg.Descriptors)
            {
                builder.Add(
                    p => new CachedHealthCheck(
                        descriptor.Factory(p),
                        p.GetRequiredService <IMemoryCache>(),
                        propertiesBuilder(p),
                        p.GetService <ILogger <CachedHealthCheck> >()),
                    descriptor.Lifetime);
            }

            return(builder);
        }
Пример #2
0
        /// <summary>
        /// Adds the health check factory to the <see cref="IHealthCheckBuilder.Descriptors"/> collection.
        /// </summary>
        /// <param name="builder">The health check builder</param>
        /// <param name="factory">The health check factory</param>
        /// <param name="lifetime">The service lifetime</param>
        /// <returns>The builder after changes</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IHealthCheckBuilder Add(this IHealthCheckBuilder builder,
                                              Func <IServiceProvider, IHealthCheck> factory, ServiceLifetime lifetime = ServiceLifetime.Scoped)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Add(new HealthCheckServiceDescriptor(factory, lifetime));
            return(builder);
        }
Пример #3
0
        /// <summary>
        /// Adds a <see cref="HttpHealthCheck"/> to the services.
        /// </summary>
        /// <param name="builder">The health check builder</param>
        /// <param name="propertiesBuilder">The health check properties builder</param>
        /// <returns>The builder after changes</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IHealthCheckBuilder AddHttp(this IHealthCheckBuilder builder,
                                                  Func <IServiceProvider, HttpHealthCheckProperties> propertiesBuilder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (propertiesBuilder == null)
            {
                throw new ArgumentNullException(nameof(propertiesBuilder));
            }

            return(builder.Add(
                       p => new HttpHealthCheck(propertiesBuilder(p), p.GetService <ILogger <HttpHealthCheck> >())));
        }
Пример #4
0
        /// <summary>
        /// Adds a <see cref="HttpHealthCheck"/> to the services.
        /// </summary>
        /// <param name="builder">The health check builder</param>
        /// <param name="properties">The health check properties</param>
        /// <returns>The builder after changes</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IHealthCheckBuilder AddHttp(this IHealthCheckBuilder builder, HttpHealthCheckProperties properties)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (properties == null)
            {
                throw new ArgumentNullException(nameof(properties));
            }

            return(builder.Add(
                       p => new HttpHealthCheck(properties, p.GetService <ILogger <HttpHealthCheck> >()),
                       ServiceLifetime.Singleton));
        }
Пример #5
0
        /// <summary>
        /// Adds a <see cref="DelegatingHealthCheck"/> to the services.
        /// </summary>
        /// <param name="builder">The health check builder</param>
        /// <param name="name">The health check name</param>
        /// <param name="action">The action to execute to get the health check status</param>
        /// <param name="required">Is the health check required?</param>
        /// <param name="tags">The collection of tags</param>
        /// <returns>The builder after changes</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IHealthCheckBuilder AddDelegate(this IHealthCheckBuilder builder,
                                                      string name, Func <IServiceProvider, CancellationToken, Task <HealthCheckStatus> > action,
                                                      bool required = false, params string[] tags)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            return(builder.Add(p =>
                               new DelegatingHealthCheck(
                                   new DelegatingHealthCheckProperties(name, ct => action(p, ct), required, tags),
                                   p.GetService <ILogger <DelegatingHealthCheck> >())));
        }
Пример #6
0
        /// <summary>
        /// Adds a <see cref="SqlHealthCheck"/> to the services.
        /// </summary>
        /// <param name="builder">The health check builder</param>
        /// <param name="name">The health check name</param>
        /// <param name="connectionBuilder">The connection builder function</param>
        /// <param name="sql">The SQL to be executed agains the database. If null or empty, the connection will only be open.</param>
        /// <param name="required">Is the health check required?</param>
        /// <param name="tags">The collection of tags</param>
        /// <returns>The builder after changes</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IHealthCheckBuilder AddSql(this IHealthCheckBuilder builder,
                                                 string name, Func <IServiceProvider, DbConnection> connectionBuilder, string sql = null,
                                                 bool required = false, params string[] tags)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (connectionBuilder == null)
            {
                throw new ArgumentNullException(nameof(connectionBuilder));
            }

            return(builder.Add(p =>
                               new SqlHealthCheck(
                                   new SqlHealthCheckProperties(name, () => connectionBuilder(p), sql, required, tags),
                                   p.GetService <ILogger <SqlHealthCheck> >())));
        }
Пример #7
0
 public static IHealthCheckBuilder AddUrlCheck(this IHealthCheckBuilder builder, string message, string url) =>
 builder.Add(sp => new UrlCheck(sp.GetRequiredService <IHealthCheckHttpClient>(), message, url));
Пример #8
0
 public static IHealthCheckBuilder AddSelfCheck(this IHealthCheckBuilder builder, string message) =>
 builder.Add(sp => new SelfCheck(message));
Пример #9
0
 public static IHealthCheckBuilder AddOrleansClientCheck(this IHealthCheckBuilder builder, string clusterId) =>
 builder.Add(sp => new OrleansClientCheck(() => sp.GetRequiredService <IGrainFactory>()));
Пример #10
0
 /// <summary>
 /// Checks if the tables specified are accessible on the database using the connection string given.
 /// </summary>
 /// <param name="builder">Health check builder</param>
 /// <param name="connectionString">Connection string to the Sqlite database</param>
 /// <param name="tables">Tables to check for existence</param>
 /// <returns></returns>
 public static IHealthCheckBuilder AddSqliteCheck(this IHealthCheckBuilder builder, string connectionString, params string[] tables) =>
 builder.Add(sp => new SqliteCheck(connectionString, tables, sp.GetRequiredService <ILogger <SqliteCheck> >()));