Esempio n. 1
0
        public async Task <(string, bool)> IsHealthy(HttpContext context, LivenessExecutionContext livenessContext, CancellationToken cancellationToken = default)
        {
            try
            {
                using (var smtpConnection = new SmtpConnection(_options))
                {
                    if (await smtpConnection.ConnectAsync())
                    {
                        if (_options.AccountOptions.login)
                        {
                            var(user, password) = _options.AccountOptions.account;

                            if (!await smtpConnection.AuthenticateAsync(user, password))
                            {
                                return($"Error login to smtp server{_options.Host}:{_options.Port} with configured credentials", false);
                            }
                        }

                        return(BeatPulseKeys.BEATPULSE_HEALTHCHECK_DEFAULT_OK_MESSAGE, true);
                    }
                    else
                    {
                        return($"Could not connect to smtp server {_options.Host}:{_options.Port} - SSL : {_options.ConnectionType}", false);
                    }
                }
            }
            catch (Exception ex)
            {
                var message = !livenessContext.IsDevelopment ? string.Format(BeatPulseKeys.BEATPULSE_HEALTHCHECK_DEFAULT_ERROR_MESSAGE, livenessContext.Name)
                  : $"Exception {ex.GetType().Name} with message ('{ex.Message}')";

                return(message, false);
            }
        }
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            try
            {
                using (var smtpConnection = new SmtpConnection(_options))
                {
                    if (await smtpConnection.ConnectAsync())
                    {
                        if (_options.AccountOptions.Login)
                        {
                            var(user, password) = _options.AccountOptions.Account;

                            if (!await smtpConnection.AuthenticateAsync(user, password).WithCancellationTokenAsync(cancellationToken))
                            {
                                return(new HealthCheckResult(context.Registration.FailureStatus, description: $"Error login to smtp server {_options.Host}:{_options.Port} with configured credentials"));
                            }
                        }

                        return(HealthCheckResult.Healthy());
                    }
                    else
                    {
                        return(new HealthCheckResult(context.Registration.FailureStatus, description: $"Could not connect to smtp server {_options.Host}:{_options.Port} - SSL : {_options.ConnectionType}"));
                    }
                }
            }
            catch (Exception ex)
            {
                return(new HealthCheckResult(context.Registration.FailureStatus, exception: ex));
            }
        }
Esempio n. 3
0
        public async Task <LivenessResult> IsHealthy(LivenessExecutionContext context, CancellationToken cancellationToken = default)
        {
            try
            {
                _logger?.LogInformation($"{nameof(SmtpLiveness)} is checking SMTP connections.");

                using (var smtpConnection = new SmtpConnection(_options))
                {
                    if (await smtpConnection.ConnectAsync())
                    {
                        if (_options.AccountOptions.Login)
                        {
                            var(user, password) = _options.AccountOptions.Account;

                            if (!await smtpConnection.AuthenticateAsync(user, password))
                            {
                                _logger?.LogWarning($"The {nameof(SmtpLiveness)} check fail with invalid login to smtp server {_options.Host}:{_options.Port} with configured credentials.");

                                return(LivenessResult.UnHealthy($"Error login to smtp server{_options.Host}:{_options.Port} with configured credentials"));
                            }
                        }

                        _logger?.LogInformation($"The {nameof(SmtpLiveness)} check success.");

                        return(LivenessResult.Healthy());
                    }
                    else
                    {
                        _logger?.LogWarning($"The {nameof(SmtpLiveness)} check fail for connecting to smtp server {_options.Host}:{_options.Port} - SSL : {_options.ConnectionType}.");

                        return(LivenessResult.UnHealthy($"Could not connect to smtp server {_options.Host}:{_options.Port} - SSL : {_options.ConnectionType}"));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger?.LogWarning($"The {nameof(SmtpLiveness)} check fail with the exception {ex.ToString()}.");

                return(LivenessResult.UnHealthy(ex));
            }
        }