| | 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> |
| 2 | 20 | | public LoggerMiddleware(IDispatcher next, ILogger logger) |
| 2 | 21 | | { |
| 2 | 22 | | _next = next; |
| 2 | 23 | | _logger = logger; |
| 2 | 24 | | } |
| | 25 | |
|
| | 26 | | /// <inheritdoc/> |
| | 27 | | public ValueTask<OutgoingResponse> DispatchAsync(IncomingRequest request, CancellationToken cancellationToken) |
| 2 | 28 | | { |
| 2 | 29 | | return _logger.IsEnabled(LogLevel.Information) ? |
| 2 | 30 | | PerformDispatchAsync() : |
| 2 | 31 | | _next.DispatchAsync(request, cancellationToken); |
| | 32 | |
|
| | 33 | | async ValueTask<OutgoingResponse> PerformDispatchAsync() |
| 2 | 34 | | { |
| | 35 | | try |
| 2 | 36 | | { |
| 2 | 37 | | OutgoingResponse response = await _next.DispatchAsync(request, cancellationToken).ConfigureAwait(false); |
| | 38 | |
|
| 1 | 39 | | _logger.LogDispatch( |
| 1 | 40 | | request.Path, |
| 1 | 41 | | request.Operation, |
| 1 | 42 | | request.ConnectionContext.TransportConnectionInformation.LocalNetworkAddress, |
| 1 | 43 | | request.ConnectionContext.TransportConnectionInformation.RemoteNetworkAddress, |
| 1 | 44 | | response.StatusCode); |
| 1 | 45 | | return response; |
| | 46 | | } |
| 1 | 47 | | catch (Exception exception) |
| 1 | 48 | | { |
| 1 | 49 | | _logger.LogDispatchException( |
| 1 | 50 | | exception, |
| 1 | 51 | | request.Path, |
| 1 | 52 | | request.Operation); |
| 1 | 53 | | throw; |
| | 54 | | } |
| 1 | 55 | | } |
| 2 | 56 | | } |
| | 57 | | } |
| | 58 | |
|
| | 59 | | /// <summary>Provides extension methods for <see cref="ILogger" />. They are used by <see cref="LoggerMiddleware" />. |
| | 60 | | /// </summary> |
| | 61 | | internal static partial class LoggerMiddlewareLoggerExtensions |
| | 62 | | { |
| | 63 | | [LoggerMessage( |
| | 64 | | EventId = (int)LoggerMiddlewareEventId.Dispatch, |
| | 65 | | EventName = nameof(LoggerMiddlewareEventId.Dispatch), |
| | 66 | | Level = LogLevel.Information, |
| | 67 | | Message = "Dispatch of {Operation} to {Path} over {LocalNetworkAddress}<->{RemoteNetworkAddress} returned a resp |
| | 68 | | internal static partial void LogDispatch( |
| | 69 | | this ILogger logger, |
| | 70 | | string path, |
| | 71 | | string operation, |
| | 72 | | EndPoint? localNetworkAddress, |
| | 73 | | EndPoint? remoteNetworkAddress, |
| | 74 | | StatusCode statusCode); |
| | 75 | |
|
| | 76 | | [LoggerMessage( |
| | 77 | | EventId = (int)LoggerMiddlewareEventId.DispatchException, |
| | 78 | | EventName = nameof(LoggerMiddlewareEventId.DispatchException), |
| | 79 | | Level = LogLevel.Information, |
| | 80 | | Message = "Failed to dispatch {Operation} to {Path}")] |
| | 81 | | internal static partial void LogDispatchException( |
| | 82 | | this ILogger logger, |
| | 83 | | Exception exception, |
| | 84 | | string path, |
| | 85 | | string operation); |
| | 86 | | } |