Exemplo n.º 1
0
        private void LogMessage(string message, LogLevel level = LogLevel.Debug, params object[] args)
        {
            switch (level)
            {
            case LogLevel.Debug:
                _logger.LogDebug(message, args);
                break;

            case LogLevel.Information:
                _logger.LogInformation(message, args);
                break;

            case LogLevel.Warning:
                _logger.LogWarning(message, args);
                break;

            case LogLevel.Error:
                _logger.LogError(message, args);
                break;

            case LogLevel.Critical:
                _logger.LogCritical(message, args);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(level), level, null);
            }
        }
Exemplo n.º 2
0
        private async Task SendAppriseNotification(SARotateConfig yamlConfigContent, string logMessage, LogLevel logLevel = LogLevel.Debug)
        {
            if (yamlConfigContent.NotificationConfig.AppriseNotificationsErrorsOnly && yamlConfigContent.NotificationConfig.AppriseServices.Any() && logLevel < LogLevel.Error)
            {
                LogMessage($"apprise notification not sent due to errors_only notifications: {logMessage}", logLevel);
            }
            else if (yamlConfigContent.NotificationConfig.AppriseServices.Any(svc => !string.IsNullOrWhiteSpace(svc)))
            {
                string appriseCommand    = $"apprise -vv ";
                string escapedLogMessage = logMessage.Replace("'", "´").Replace("\"", "´");
                appriseCommand += $"-b '{escapedLogMessage}' ";

                foreach (var appriseService in yamlConfigContent.NotificationConfig.AppriseServices.Where(svc => !string.IsNullOrWhiteSpace(svc)))
                {
                    appriseCommand += $"'{appriseService}' ";
                }

                (string result, int exitCode) = await appriseCommand.Bash();

                if (exitCode != (int)ExitCode.Success)
                {
                    LogMessage($"Unable to send apprise notification: {logMessage}", LogLevel.Error);
                    LogMessage($"Apprise failure: {result}");
                }
                else
                {
                    LogMessage($"sent apprise notification: {logMessage}");
                }
            }
        }