Exemplo n.º 1
0
        /// <summary>
        /// Add a health check for the SMTP connection.
        /// </summary>
        /// <param name="healthChecksBuilder">The <see cref="IHealthChecksBuilder"/> instance.</param>
        /// <param name="smtpSettings">The SMTP connection settings to use for the health check.</param>
        /// <param name="tags">An optional list of tags used to filter sets of health checks. The default is <see cref="HealthCheck.DefaultTags"/>.</param>
        /// <param name="timeout">An optional timeout value. The default is <see cref="HealthCheck.DefaultTimeout"/>.</param>
        /// <exception cref="ArgumentNullException"><paramref name="healthChecksBuilder"/> is <see langref="null"/>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="smtpSettings"/> is <see langref="null"/>.</exception>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IHealthChecksBuilder AddSmtpHealthCheck(
            this IHealthChecksBuilder healthChecksBuilder,
            SmtpSettings smtpSettings,
            IEnumerable <string>?tags = null,
            TimeSpan?timeout          = null)
        {
            if (healthChecksBuilder is null)
            {
                throw new ArgumentNullException(nameof(healthChecksBuilder));
            }

            if (smtpSettings is null)
            {
                throw new ArgumentNullException(nameof(smtpSettings));
            }

            healthChecksBuilder.AddSmtpHealthCheck(
                smtp =>
            {
                smtp.AllowInvalidRemoteCertificates = smtpSettings.AllowInvalidCertificate.GetValueOrDefault();
                smtp.ConnectionType = SmtpConnectionType.TLS;
                smtp.Host           = smtpSettings.Host;
                smtp.Port           = smtpSettings.Port;
            },
                HealthCheck.Name,
                HealthStatus.Degraded,
                tags ?? HealthCheck.DefaultTags,
                timeout ?? HealthCheck.DefaultTimeout);

            return(healthChecksBuilder);
        }
Exemplo n.º 2
0
        private static IHealthChecksBuilder AddSMTPCheck(this IHealthChecksBuilder builder, IConfiguration configuration)
        {
            var smtpSettings = ConfigurationHandler.GetSmtpSettings(configuration);

            if (string.IsNullOrWhiteSpace(smtpSettings?.Smtp?.Network?.Host))
            {
                logger.Info("SMTP Check: No valid Host found. Skipping check.");
                return(builder);
            }

            var enableSSL = false;

            if (bool.TryParse(configuration[$"configuration:appSettings:EnableSSL:value"], out var parsedBool))
            {
                enableSSL = parsedBool;
            }

            return(builder.AddSmtpHealthCheck(setup =>
            {
                //SSL on by default
                setup.Host = smtpSettings.Smtp.Network.Host;
                setup.Port = smtpSettings.Smtp.Network.Port;
                setup.ConnectionType = enableSSL ? SmtpConnectionType.SSL : SmtpConnectionType.AUTO;
                setup.AllowInvalidRemoteCertificates = true;
                setup.LoginWith(smtpSettings.Smtp.Network.UserName, smtpSettings.Smtp.Network.Password);
            }));
        }
Exemplo n.º 3
0
        public static IHealthChecksBuilder AddSmtpHealthCheck(this IHealthChecksBuilder builder, SmtpSettings smtpSettings)
        {
            builder.AddSmtpHealthCheck(setup =>
            {
                setup.Host           = smtpSettings.MailServer;
                setup.Port           = smtpSettings.Port;
                setup.ConnectionType = SmtpConnectionType.TLS;
                setup.LoginWith(smtpSettings.UserName, smtpSettings.Password);
                setup.AllowInvalidRemoteCertificates = true;
            }, tags: new[] { "smtp" }, failureStatus: HealthStatus.Degraded);

            return(builder);
        }
        /// <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);
        }