private async Task FlushAndRestartTimerAsync(EmailLoggerFlushReason reason) { if (!_running) { return; } _updateTimer.Stop(); await FlushAsync(reason); _updateTimer.Start(); }
private async Task FlushAsync(EmailLoggerFlushReason reason) { if (_flushInProgress) { return; } _flushInProgress = true; string logMessage; string subject; var body = string.Empty; MailPriority priority; if (reason == EmailLoggerFlushReason.LoggerStarted) { subject = $"{Subject} (Started)"; body = $"Logger started\nLevel: {Level}\nFlush level: {FlushLevel}\nQueue size: {MaxQueueSize}"; priority = MailPriority.Low; logMessage = "Email logger starting"; } else if (_messageQueue.IsEmpty) { if (!SendEmptyReports) { logMessage = "Skipping mail send"; Logger.LogEntry("MAIL LOG", LogLevel.Debug, logMessage); return; } logMessage = "Sending empty email"; subject = $"{Subject} (No new entries)"; body = $"No new entries of level {Level} and higher"; priority = MailPriority.Low; } else { var counter = 0; LogEntryEventArgs temp; while (_messageQueue.TryDequeue(out temp)) { counter++; body += $"\n\n{temp.Representation}"; } logMessage = $"Sending email with {counter} entries"; var alarmStr = string.Empty; switch (reason) { case EmailLoggerFlushReason.ImportantMessage: alarmStr = ", ALARM CRIT LEVEL"; break; case EmailLoggerFlushReason.QueueFull: alarmStr = ", ALARM QUEUE FULL"; break; case EmailLoggerFlushReason.ForceFlush: alarmStr = ", FORCE FLUSH"; break; case EmailLoggerFlushReason.LoggerStarted: case EmailLoggerFlushReason.RegularPeriodical: // No additional parts break; } priority = reason == EmailLoggerFlushReason.RegularPeriodical ? MailPriority.Normal : MailPriority.High; subject = $"{Subject} ({counter} new entries{alarmStr})"; } Logger.LogEntry("MAIL LOG", LogLevel.Debug, logMessage); await SendEmailAsync(subject, body, priority); _flushInProgress = false; }