| | | 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> |
| | 3 | 20 | | public LoggerInterceptor(IInvoker next, ILogger logger) |
| | 3 | 21 | | { |
| | 3 | 22 | | _next = next; |
| | 3 | 23 | | _logger = logger; |
| | 3 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc/> |
| | | 27 | | public async Task<IncomingResponse> InvokeAsync(OutgoingRequest request, CancellationToken cancellationToken) |
| | 4 | 28 | | { |
| | | 29 | | try |
| | 4 | 30 | | { |
| | 4 | 31 | | IncomingResponse response = await _next.InvokeAsync(request, cancellationToken).ConfigureAwait(false); |
| | | 32 | | |
| | 2 | 33 | | _logger.LogInvoke( |
| | 2 | 34 | | request.ServiceAddress, |
| | 2 | 35 | | request.Operation, |
| | 2 | 36 | | response.StatusCode, |
| | 2 | 37 | | response.ConnectionContext.TransportConnectionInformation.LocalNetworkAddress, |
| | 2 | 38 | | response.ConnectionContext.TransportConnectionInformation.RemoteNetworkAddress); |
| | 2 | 39 | | return response; |
| | | 40 | | } |
| | 2 | 41 | | catch (Exception exception) |
| | 2 | 42 | | { |
| | 2 | 43 | | _logger.LogInvokeException(exception, request.ServiceAddress, request.Operation); |
| | 2 | 44 | | throw; |
| | | 45 | | } |
| | 2 | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary>Provides extension methods for <see cref="ILogger" />. They are used by <see cref="LoggerInterceptor"/>. |
| | | 50 | | /// </summary> |
| | | 51 | | internal static partial class LoggerInterceptorLoggerExtensions |
| | | 52 | | { |
| | | 53 | | [LoggerMessage( |
| | | 54 | | EventId = (int)LoggerInterceptorEventId.Invoke, |
| | | 55 | | EventName = nameof(LoggerInterceptorEventId.Invoke), |
| | | 56 | | Level = LogLevel.Information, |
| | | 57 | | Message = "Sent request {Operation} to {ServiceAddress} over {LocalNetworkAddress}<->{RemoteNetworkAddress} and |
| | | 58 | | internal static partial void LogInvoke( |
| | | 59 | | this ILogger logger, |
| | | 60 | | ServiceAddress serviceAddress, |
| | | 61 | | string operation, |
| | | 62 | | StatusCode statusCode, |
| | | 63 | | EndPoint? localNetworkAddress, |
| | | 64 | | EndPoint? remoteNetworkAddress); |
| | | 65 | | |
| | | 66 | | [LoggerMessage( |
| | | 67 | | EventId = (int)LoggerInterceptorEventId.InvokeException, |
| | | 68 | | EventName = nameof(LoggerInterceptorEventId.InvokeException), |
| | | 69 | | Level = LogLevel.Information, |
| | | 70 | | Message = "Failed to send request {Operation} to {ServiceAddress}")] |
| | | 71 | | internal static partial void LogInvokeException( |
| | | 72 | | this ILogger logger, |
| | | 73 | | Exception exception, |
| | | 74 | | ServiceAddress serviceAddress, |
| | | 75 | | string operation); |
| | | 76 | | } |