| | | 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>An interceptor that writes a log entry to an <see cref="ILogger" /> for each invocation.</summary> |
| | | 10 | | /// <seealso cref="LoggerPipelineExtensions"/> |
| | | 11 | | /// <seealso cref="LoggerInvokerBuilderExtensions"/> |
| | | 12 | | public class LoggerInterceptor : IInvoker |
| | | 13 | | { |
| | | 14 | | private readonly ILogger _logger; |
| | | 15 | | private readonly IInvoker _next; |
| | | 16 | | |
| | | 17 | | /// <summary>Constructs a logger interceptor.</summary> |
| | | 18 | | /// <param name="next">The next invoker in the invocation pipeline.</param> |
| | | 19 | | /// <param name="logger">The logger to log to.</param> |
| | 4 | 20 | | public LoggerInterceptor(IInvoker next, ILogger logger) |
| | 4 | 21 | | { |
| | 4 | 22 | | _next = next; |
| | 4 | 23 | | _logger = logger; |
| | 4 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc/> |
| | | 27 | | public async Task<IncomingResponse> InvokeAsync(OutgoingRequest request, CancellationToken cancellationToken) |
| | 5 | 28 | | { |
| | | 29 | | try |
| | 5 | 30 | | { |
| | 5 | 31 | | IncomingResponse response = await _next.InvokeAsync(request, cancellationToken).ConfigureAwait(false); |
| | | 32 | | |
| | 3 | 33 | | if (response.StatusCode == StatusCode.Ok) |
| | 2 | 34 | | { |
| | 2 | 35 | | _logger.LogInvoke( |
| | 2 | 36 | | request.ServiceAddress, |
| | 2 | 37 | | request.Operation, |
| | 2 | 38 | | response.ConnectionContext.TransportConnectionInformation.LocalNetworkAddress, |
| | 2 | 39 | | response.ConnectionContext.TransportConnectionInformation.RemoteNetworkAddress); |
| | 2 | 40 | | } |
| | | 41 | | else |
| | 1 | 42 | | { |
| | 1 | 43 | | _logger.LogInvokeError( |
| | 1 | 44 | | request.ServiceAddress, |
| | 1 | 45 | | request.Operation, |
| | 1 | 46 | | response.StatusCode, |
| | 1 | 47 | | response.ErrorMessage!, |
| | 1 | 48 | | response.ConnectionContext.TransportConnectionInformation.LocalNetworkAddress, |
| | 1 | 49 | | response.ConnectionContext.TransportConnectionInformation.RemoteNetworkAddress); |
| | 1 | 50 | | } |
| | 3 | 51 | | return response; |
| | | 52 | | } |
| | 2 | 53 | | catch (Exception exception) |
| | 2 | 54 | | { |
| | 2 | 55 | | _logger.LogInvokeException(exception, request.ServiceAddress, request.Operation); |
| | 2 | 56 | | throw; |
| | | 57 | | } |
| | 3 | 58 | | } |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary>Provides extension methods for <see cref="ILogger" />. They are used by <see cref="LoggerInterceptor"/>. |
| | | 62 | | /// </summary> |
| | | 63 | | internal static partial class LoggerInterceptorLoggerExtensions |
| | | 64 | | { |
| | | 65 | | [LoggerMessage( |
| | | 66 | | EventId = (int)LoggerInterceptorEventId.Invoke, |
| | | 67 | | EventName = nameof(LoggerInterceptorEventId.Invoke), |
| | | 68 | | Level = LogLevel.Information, |
| | | 69 | | Message = "Sent request {Operation} to {ServiceAddress} over {LocalNetworkAddress}<->{RemoteNetworkAddress} and |
| | | 70 | | internal static partial void LogInvoke( |
| | | 71 | | this ILogger logger, |
| | | 72 | | ServiceAddress serviceAddress, |
| | | 73 | | string operation, |
| | | 74 | | EndPoint? localNetworkAddress, |
| | | 75 | | EndPoint? remoteNetworkAddress); |
| | | 76 | | |
| | | 77 | | [LoggerMessage( |
| | | 78 | | EventId = (int)LoggerInterceptorEventId.InvokeError, |
| | | 79 | | EventName = nameof(LoggerInterceptorEventId.InvokeError), |
| | | 80 | | Level = LogLevel.Information, |
| | | 81 | | Message = "Sent request {Operation} to {ServiceAddress} over {LocalNetworkAddress}<->{RemoteNetworkAddress} and |
| | | 82 | | internal static partial void LogInvokeError( |
| | | 83 | | this ILogger logger, |
| | | 84 | | ServiceAddress serviceAddress, |
| | | 85 | | string operation, |
| | | 86 | | StatusCode statusCode, |
| | | 87 | | string errorMessage, |
| | | 88 | | EndPoint? localNetworkAddress, |
| | | 89 | | EndPoint? remoteNetworkAddress); |
| | | 90 | | |
| | | 91 | | [LoggerMessage( |
| | | 92 | | EventId = (int)LoggerInterceptorEventId.InvokeException, |
| | | 93 | | EventName = nameof(LoggerInterceptorEventId.InvokeException), |
| | | 94 | | Level = LogLevel.Information, |
| | | 95 | | Message = "Failed to send request {Operation} to {ServiceAddress}")] |
| | | 96 | | internal static partial void LogInvokeException( |
| | | 97 | | this ILogger logger, |
| | | 98 | | Exception exception, |
| | | 99 | | ServiceAddress serviceAddress, |
| | | 100 | | string operation); |
| | | 101 | | } |