| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Extensions.DependencyInjection; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | using System.Net; |
| | | 6 | | |
| | | 7 | | namespace IceRpc.Logger; |
| | | 8 | | |
| | | 9 | | /// <summary>A middleware that writes a log entry to an <see cref="ILogger" /> for each dispatch.</summary> |
| | | 10 | | /// <seealso cref="LoggerRouterExtensions"/> |
| | | 11 | | /// <seealso cref="LoggerDispatcherBuilderExtensions"/> |
| | | 12 | | public class LoggerMiddleware : IDispatcher |
| | | 13 | | { |
| | | 14 | | private readonly IDispatcher _next; |
| | | 15 | | private readonly ILogger _logger; |
| | | 16 | | |
| | | 17 | | /// <summary>Constructs a logger middleware.</summary> |
| | | 18 | | /// <param name="next">The next dispatcher in the dispatch pipeline.</param> |
| | | 19 | | /// <param name="logger">The logger to log to.</param> |
| | 3 | 20 | | public LoggerMiddleware(IDispatcher next, ILogger logger) |
| | 3 | 21 | | { |
| | 3 | 22 | | _next = next; |
| | 3 | 23 | | _logger = logger; |
| | 3 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc/> |
| | | 27 | | public ValueTask<OutgoingResponse> DispatchAsync(IncomingRequest request, CancellationToken cancellationToken) |
| | 3 | 28 | | { |
| | 3 | 29 | | return _logger.IsEnabled(LogLevel.Information) ? |
| | 3 | 30 | | PerformDispatchAsync() : |
| | 3 | 31 | | _next.DispatchAsync(request, cancellationToken); |
| | | 32 | | |
| | | 33 | | async ValueTask<OutgoingResponse> PerformDispatchAsync() |
| | 3 | 34 | | { |
| | | 35 | | try |
| | 3 | 36 | | { |
| | 3 | 37 | | OutgoingResponse response = await _next.DispatchAsync(request, cancellationToken).ConfigureAwait(false); |
| | | 38 | | |
| | 2 | 39 | | if (response.StatusCode == StatusCode.Ok) |
| | 1 | 40 | | { |
| | 1 | 41 | | _logger.LogDispatch( |
| | 1 | 42 | | request.Path, |
| | 1 | 43 | | request.Operation, |
| | 1 | 44 | | request.ConnectionContext.TransportConnectionInformation.LocalNetworkAddress, |
| | 1 | 45 | | request.ConnectionContext.TransportConnectionInformation.RemoteNetworkAddress); |
| | 1 | 46 | | } |
| | | 47 | | else |
| | 1 | 48 | | { |
| | 1 | 49 | | _logger.LogDispatchError( |
| | 1 | 50 | | request.Path, |
| | 1 | 51 | | request.Operation, |
| | 1 | 52 | | request.ConnectionContext.TransportConnectionInformation.LocalNetworkAddress, |
| | 1 | 53 | | request.ConnectionContext.TransportConnectionInformation.RemoteNetworkAddress, |
| | 1 | 54 | | response.StatusCode, |
| | 1 | 55 | | response.ErrorMessage!); |
| | 1 | 56 | | } |
| | 2 | 57 | | return response; |
| | | 58 | | } |
| | 1 | 59 | | catch (Exception exception) |
| | 1 | 60 | | { |
| | 1 | 61 | | _logger.LogDispatchException( |
| | 1 | 62 | | exception, |
| | 1 | 63 | | request.Path, |
| | 1 | 64 | | request.Operation); |
| | 1 | 65 | | throw; |
| | | 66 | | } |
| | 2 | 67 | | } |
| | 3 | 68 | | } |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary>Provides extension methods for <see cref="ILogger" />. They are used by <see cref="LoggerMiddleware" />. |
| | | 72 | | /// </summary> |
| | | 73 | | internal static partial class LoggerMiddlewareLoggerExtensions |
| | | 74 | | { |
| | | 75 | | [LoggerMessage( |
| | | 76 | | EventId = (int)LoggerMiddlewareEventId.Dispatch, |
| | | 77 | | EventName = nameof(LoggerMiddlewareEventId.Dispatch), |
| | | 78 | | Level = LogLevel.Information, |
| | | 79 | | Message = "Dispatch of {Operation} to {Path} over {LocalNetworkAddress}<->{RemoteNetworkAddress} returned a resp |
| | | 80 | | internal static partial void LogDispatch( |
| | | 81 | | this ILogger logger, |
| | | 82 | | string path, |
| | | 83 | | string operation, |
| | | 84 | | EndPoint? localNetworkAddress, |
| | | 85 | | EndPoint? remoteNetworkAddress); |
| | | 86 | | |
| | | 87 | | [LoggerMessage( |
| | | 88 | | EventId = (int)LoggerMiddlewareEventId.DispatchError, |
| | | 89 | | EventName = nameof(LoggerMiddlewareEventId.DispatchError), |
| | | 90 | | Level = LogLevel.Information, |
| | | 91 | | Message = "Dispatch of {Operation} to {Path} over {LocalNetworkAddress}<->{RemoteNetworkAddress} returned a resp |
| | | 92 | | internal static partial void LogDispatchError( |
| | | 93 | | this ILogger logger, |
| | | 94 | | string path, |
| | | 95 | | string operation, |
| | | 96 | | EndPoint? localNetworkAddress, |
| | | 97 | | EndPoint? remoteNetworkAddress, |
| | | 98 | | StatusCode statusCode, |
| | | 99 | | string errorMessage); |
| | | 100 | | |
| | | 101 | | [LoggerMessage( |
| | | 102 | | EventId = (int)LoggerMiddlewareEventId.DispatchException, |
| | | 103 | | EventName = nameof(LoggerMiddlewareEventId.DispatchException), |
| | | 104 | | Level = LogLevel.Information, |
| | | 105 | | Message = "Failed to dispatch {Operation} to {Path}")] |
| | | 106 | | internal static partial void LogDispatchException( |
| | | 107 | | this ILogger logger, |
| | | 108 | | Exception exception, |
| | | 109 | | string path, |
| | | 110 | | string operation); |
| | | 111 | | } |