| | | 1 | | namespace Anichron.Worker.Scheduling; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Logs a poll loop's outage once: the first failure as an error with its exception, each following one at debug, |
| | | 5 | | /// and the first success after it as a recovery. |
| | | 6 | | /// </summary> |
| | | 7 | | /// <param name="logger">The poll loop's own logger.</param> |
| | | 8 | | /// <param name="operation">What the loop does, as the start of a sentence.</param> |
| | 42 | 9 | | internal sealed partial class OutageLog(ILogger logger, string operation) |
| | | 10 | | { |
| | | 11 | | private bool failing; |
| | | 12 | | |
| | | 13 | | public void Succeeded() |
| | 68 | 14 | | { |
| | 68 | 15 | | if (!failing) |
| | 63 | 16 | | return; |
| | | 17 | | |
| | 5 | 18 | | failing = false; |
| | 5 | 19 | | Log.Recovered(logger, operation); |
| | 68 | 20 | | } |
| | | 21 | | |
| | | 22 | | public void Failed(Exception ex) |
| | 11 | 23 | | { |
| | 11 | 24 | | if (failing) |
| | 4 | 25 | | Log.StillFailing(logger, operation); |
| | | 26 | | else |
| | 7 | 27 | | Log.Failed(logger, operation, ex); |
| | 11 | 28 | | failing = true; |
| | 11 | 29 | | } |
| | | 30 | | |
| | | 31 | | private static partial class Log |
| | | 32 | | { |
| | | 33 | | [LoggerMessage(Level = LogLevel.Error, Message = "{Operation} failed; will retry at the next poll.")] |
| | | 34 | | public static partial void Failed(ILogger logger, string operation, Exception ex); |
| | | 35 | | |
| | | 36 | | [LoggerMessage(Level = LogLevel.Debug, Message = "{Operation} failed again; will retry at the next poll.")] |
| | | 37 | | public static partial void StillFailing(ILogger logger, string operation); |
| | | 38 | | |
| | | 39 | | [LoggerMessage(Level = LogLevel.Information, Message = "{Operation} works again.")] |
| | | 40 | | public static partial void Recovered(ILogger logger, string operation); |
| | | 41 | | } |
| | | 42 | | } |