| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | using TeleFlow.Framework.Updates; |
| | | 3 | | |
| | | 4 | | namespace TeleFlow.Framework.Middleware; |
| | | 5 | | |
| | | 6 | | public sealed class UpdateLoggingMiddleware : IUpdateMiddleware |
| | | 7 | | { |
| | 1 | 8 | | private static readonly Action<ILogger, Exception?> LogProcessingStarted = |
| | 1 | 9 | | LoggerMessage.Define( |
| | 1 | 10 | | LogLevel.Information, |
| | 1 | 11 | | new EventId(1, nameof(LogProcessingStarted)), |
| | 1 | 12 | | "Started processing update."); |
| | | 13 | | |
| | 1 | 14 | | private static readonly Action<ILogger, Exception?> LogProcessingFinished = |
| | 1 | 15 | | LoggerMessage.Define( |
| | 1 | 16 | | LogLevel.Information, |
| | 1 | 17 | | new EventId(2, nameof(LogProcessingFinished)), |
| | 1 | 18 | | "Finished processing update."); |
| | | 19 | | |
| | 1 | 20 | | private static readonly Action<ILogger, Exception?> LogProcessingFailed = |
| | 1 | 21 | | LoggerMessage.Define( |
| | 1 | 22 | | LogLevel.Error, |
| | 1 | 23 | | new EventId(3, nameof(LogProcessingFailed)), |
| | 1 | 24 | | "Failed processing update."); |
| | | 25 | | |
| | | 26 | | private readonly ILogger<UpdateLoggingMiddleware>? _logger; |
| | | 27 | | |
| | | 28 | | public UpdateLoggingMiddleware() |
| | | 29 | | { |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | public UpdateLoggingMiddleware(ILogger<UpdateLoggingMiddleware> logger) |
| | | 33 | | { |
| | 1 | 34 | | _logger = logger; |
| | 1 | 35 | | } |
| | | 36 | | |
| | | 37 | | public async Task InvokeAsync(UpdateContext context, UpdateDelegate next) |
| | | 38 | | { |
| | 1 | 39 | | ArgumentNullException.ThrowIfNull(context); |
| | 1 | 40 | | ArgumentNullException.ThrowIfNull(next); |
| | | 41 | | |
| | 1 | 42 | | if (_logger is not null) |
| | | 43 | | { |
| | 1 | 44 | | LogProcessingStarted(_logger, null); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | try |
| | | 48 | | { |
| | 1 | 49 | | await next(context).ConfigureAwait(false); |
| | | 50 | | |
| | 1 | 51 | | if (_logger is not null) |
| | | 52 | | { |
| | 1 | 53 | | LogProcessingFinished(_logger, null); |
| | | 54 | | } |
| | 1 | 55 | | } |
| | 0 | 56 | | catch (Exception exception) |
| | | 57 | | { |
| | 0 | 58 | | if (_logger is not null) |
| | | 59 | | { |
| | 0 | 60 | | LogProcessingFailed(_logger, exception); |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | throw; |
| | | 64 | | } |
| | 1 | 65 | | } |
| | | 66 | | } |