public static IHealthChecksBuilder AddDependencies(
            this IHealthChecksBuilder builder,
            List <Dependency> dependencies)
        {
            foreach (var dependencia in dependencies)
            {
                string nomeDependencia = dependencia.Name.ToLower();

                if (nomeDependencia.StartsWith("sqlserver-"))
                {
                    builder = builder.AddSqlServer(dependencia.ConnectionString, name: dependencia.Name);
                }
                else if (nomeDependencia.StartsWith("mongodb-"))
                {
                    builder = builder.AddMongoDb(dependencia.ConnectionString, name: dependencia.Name);
                }
                else if (nomeDependencia.StartsWith("redis-"))
                {
                    builder = builder.AddRedis(dependencia.ConnectionString, name: dependencia.Name);
                }
                else if (nomeDependencia.StartsWith("postgres-"))
                {
                    builder = builder.AddNpgSql(dependencia.ConnectionString, name: dependencia.Name);
                }
                else if (nomeDependencia.StartsWith("mysql-"))
                {
                    builder = builder.AddMySql(dependencia.ConnectionString, name: dependencia.Name);
                }
                else if (nomeDependencia.StartsWith("url-"))
                {
                    builder = builder.AddUrlGroup(new Uri(dependencia.Url), name: dependencia.Name);
                }
                else if (nomeDependencia.StartsWith("rabbitmq-"))
                {
                    builder = builder.AddRabbitMQ(dependencia.ConnectionString, name: dependencia.Name);
                }
                else if (nomeDependencia.StartsWith("azureservicebusqueue-"))
                {
                    builder = builder.AddAzureServiceBusQueue(dependencia.ConnectionString, queueName: dependencia.QueueName, name: dependencia.Name);
                }
                else if (nomeDependencia.StartsWith("azureblobstorage-"))
                {
                    builder = builder.AddAzureBlobStorage(dependencia.ConnectionString, name: dependencia.Name);
                }
                else if (nomeDependencia.StartsWith("documentdb-"))
                {
                    builder = builder.AddDocumentDb(
                        docdb => {
                        docdb.UriEndpoint = dependencia.UriEndpoint;
                        docdb.PrimaryKey  = dependencia.PrimaryKey;
                    });
                }
            }

            return(builder);
        }
 /// <summary>
 /// Add a health check for Redis services.
 /// </summary>
 /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
 /// <param name="redisConnectionString">The Redis connection string to be used.</param>
 /// <param name="name">The health check name. Optional. If <c>null</c> the type name 'redis' will be used for the name.</param>
 /// <param name="failureStatus">
 /// The <see cref="HealthStatus"/> that should be reported when the health check fails. Optional. If <c>null</c> then
 /// the default status of <see cref="HealthStatus.Unhealthy"/> will be reported.
 /// </param>
 /// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param>
 /// <param name="timeout">An optional <see cref="TimeSpan"/> representing the timeout of the check.</param>
 /// <returns>The specified <paramref name="builder"/>.</returns>
 public static IHealthChecksBuilder AddRedis(
     this IHealthChecksBuilder builder,
     string redisConnectionString,
     string?name = default,
     HealthStatus?failureStatus = default,
     IEnumerable <string>?tags  = default,
     TimeSpan?timeout           = default)
 {
     return(builder.AddRedis(_ => redisConnectionString, name, failureStatus, tags, timeout));
 }
Esempio n. 3
0
        private static void DoAddIDistributedCache(IServiceCollection services, RedisServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime, IHealthChecksBuilder healthChecksBuilder = null)
        {
            Type interfaceType  = RedisTypeLocator.MicrosoftInterface;
            Type connectionType = RedisTypeLocator.MicrosoftImplementation;
            Type optionsType    = RedisTypeLocator.MicrosoftOptions;

            RedisCacheConnectorOptions   redisConfig = new RedisCacheConnectorOptions(config);
            RedisServiceConnectorFactory factory     = new RedisServiceConnectorFactory(info, redisConfig, connectionType, optionsType, null);

            services.Add(new ServiceDescriptor(interfaceType, factory.Create, contextLifetime));
            services.Add(new ServiceDescriptor(connectionType, factory.Create, contextLifetime));
            if (healthChecksBuilder == null)
            {
                services.Add(new ServiceDescriptor(typeof(IHealthContributor), ctx => new RedisHealthContributor(factory, connectionType, ctx.GetService <ILogger <RedisHealthContributor> >()), ServiceLifetime.Singleton));
            }
            else
            {
                healthChecksBuilder.AddRedis(redisConfig.ToString());
            }
        }
Esempio n. 4
0
        private static void DoAddConnectionMultiplexer(IServiceCollection services, RedisServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime, IHealthChecksBuilder healthChecksBuilder)
        {
            Type       redisInterface      = RedisTypeLocator.StackExchangeInterface;
            Type       redisImplementation = RedisTypeLocator.StackExchangeImplementation;
            Type       redisOptions        = RedisTypeLocator.StackExchangeOptions;
            MethodInfo initializer         = RedisTypeLocator.StackExchangeInitializer;

            RedisCacheConnectorOptions   redisConfig = new RedisCacheConnectorOptions(config);
            RedisServiceConnectorFactory factory     = new RedisServiceConnectorFactory(info, redisConfig, redisImplementation, redisOptions, initializer ?? null);

            services.Add(new ServiceDescriptor(redisInterface, factory.Create, contextLifetime));
            services.Add(new ServiceDescriptor(redisImplementation, factory.Create, contextLifetime));
            if (healthChecksBuilder == null)
            {
                services.Add(new ServiceDescriptor(typeof(IHealthContributor), ctx => new RedisHealthContributor(factory, redisImplementation, ctx.GetService <ILogger <RedisHealthContributor> >()), ServiceLifetime.Singleton));
            }
            else
            {
                var str = factory.GetConnectionString();
                healthChecksBuilder.AddRedis(str);
            }
        }
 public static IHealthChecksBuilder AddCustomRedis(this IHealthChecksBuilder builder, IConfiguration configuration)
 {
     return(builder.AddRedis(configuration.GetRedisConnectionString()));
 }
        /// <summary>
        /// 建立HealthChecks服务
        /// </summary>
        /// <param name="builder">HealthChecks服务创建者</param>
        /// <param name="configuration">应用程序配置</param>
        /// <returns></returns>
        protected virtual IHealthChecksBuilder BuildHealthChecks(IHealthChecksBuilder builder, IConfiguration configuration)
        {
            //system
            long providerMemory    = configuration["OSharp:HealthChecks:PrivateMemory"].CastTo(1000_000_000L);
            long virtualMemorySize = configuration["OSharp:HealthChecks:VirtualMemorySize"].CastTo(1000_000_000L);
            long workingSet        = configuration["OSharp:HealthChecks:WorkingSet"].CastTo(1000_000_000L);

            builder.AddPrivateMemoryHealthCheck(providerMemory);        //最大私有内存
            builder.AddVirtualMemorySizeHealthCheck(virtualMemorySize); //最大虚拟内存
            builder.AddWorkingSetHealthCheck(workingSet);               //最大工作内存

            OsharpOptions options = configuration.GetOsharpOptions();

            //数据库
            foreach (var pair in options.DbContexts.OrderBy(m => m.Value.DatabaseType))
            {
                string connectionString = pair.Value.ConnectionString;
                switch (pair.Value.DatabaseType)
                {
                case DatabaseType.SqlServer:
                    builder.AddSqlServer(connectionString, null, pair.Key);
                    break;

                case DatabaseType.Sqlite:
                    builder.AddSqlite(connectionString, name: pair.Key);
                    break;

                case DatabaseType.MySql:
                    builder.AddMySql(connectionString, pair.Key);
                    break;

                case DatabaseType.PostgreSql:
                    builder.AddNpgSql(connectionString, name: pair.Key);
                    break;

                case DatabaseType.Oracle:
                    builder.AddOracle(connectionString, name: pair.Key);
                    break;

                default:
                    throw new ArgumentOutOfRangeException($"OSharpOptions中 {pair.Value.DatabaseType} 不受支持");
                }
            }

            //SMTP
            if (options.MailSender != null)
            {
                var smtp = options.MailSender;
                builder.AddSmtpHealthCheck(smtpOptions =>
                {
                    smtpOptions.Host = smtp.Host;
                    smtpOptions.LoginWith(smtp.UserName, smtp.Password);
                });
            }

            //Redis
            if (options.Redis != null && options.Redis.Enabled)
            {
                var redis = options.Redis;
                builder.AddRedis(redis.Configuration);
            }

            //Hangfire
            if (configuration["OSharp:Hangfire:Enabled"].CastTo(false))
            {
                builder.AddHangfire(hangfireOptions =>
                {
                    hangfireOptions.MinimumAvailableServers = 1;
                });
            }

            return(builder);
        }