| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Transports; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | |
| | | 6 | | namespace IceRpc.Internal; |
| | | 7 | | |
| | | 8 | | /// <summary>Implements <see cref="ITaskExceptionObserver" /> using a <see cref="ILogger" />.</summary> |
| | | 9 | | internal class LogTaskExceptionObserver : ITaskExceptionObserver |
| | | 10 | | { |
| | | 11 | | private readonly ILogger _logger; |
| | | 12 | | |
| | | 13 | | [System.Diagnostics.CodeAnalysis.SuppressMessage( |
| | | 14 | | "Performance", |
| | | 15 | | "CA1873:Avoid potentially expensive logging", |
| | | 16 | | Justification = "GetLogLevel is a trivial switch on the exception type and is required to categorize the log.")] |
| | | 17 | | public void DispatchFailed( |
| | | 18 | | IncomingRequest request, |
| | | 19 | | TransportConnectionInformation connectionInformation, |
| | | 20 | | Exception exception) => |
| | 1 | 21 | | _logger.LogDispatchFailed( |
| | 1 | 22 | | GetLogLevel(exception), |
| | 1 | 23 | | request.Operation, |
| | 1 | 24 | | request.Path, |
| | 1 | 25 | | connectionInformation.RemoteNetworkAddress, |
| | 1 | 26 | | exception); |
| | | 27 | | |
| | | 28 | | [System.Diagnostics.CodeAnalysis.SuppressMessage( |
| | | 29 | | "Performance", |
| | | 30 | | "CA1873:Avoid potentially expensive logging", |
| | | 31 | | Justification = "GetLogLevel is a trivial switch on the exception type and is required to categorize the log.")] |
| | | 32 | | public void DispatchRefused(TransportConnectionInformation connectionInformation, Exception exception) => |
| | 0 | 33 | | _logger.LogDispatchRefused(GetLogLevel(exception), connectionInformation.RemoteNetworkAddress, exception); |
| | | 34 | | |
| | | 35 | | [System.Diagnostics.CodeAnalysis.SuppressMessage( |
| | | 36 | | "Performance", |
| | | 37 | | "CA1873:Avoid potentially expensive logging", |
| | | 38 | | Justification = "GetLogLevel is a trivial switch on the exception type and is required to categorize the log.")] |
| | | 39 | | public void RequestPayloadContinuationFailed( |
| | | 40 | | OutgoingRequest request, |
| | | 41 | | TransportConnectionInformation connectionInformation, |
| | | 42 | | Exception exception) => |
| | 0 | 43 | | _logger.LogRequestPayloadContinuationFailed( |
| | 0 | 44 | | GetLogLevel(exception), |
| | 0 | 45 | | request.Operation, |
| | 0 | 46 | | request.ServiceAddress.Path, |
| | 0 | 47 | | connectionInformation.RemoteNetworkAddress, |
| | 0 | 48 | | exception); |
| | | 49 | | |
| | 18 | 50 | | internal LogTaskExceptionObserver(ILogger logger) => _logger = logger; |
| | | 51 | | |
| | | 52 | | private static LogLevel GetLogLevel(Exception exception) => |
| | 1 | 53 | | exception switch |
| | 1 | 54 | | { |
| | 1 | 55 | | // expected during shutdown for example |
| | 1 | 56 | | OperationCanceledException => LogLevel.Trace, |
| | 1 | 57 | | |
| | 1 | 58 | | // usually expected, e.g. peer aborts connection |
| | 0 | 59 | | IceRpcException => LogLevel.Debug, |
| | 1 | 60 | | |
| | 1 | 61 | | // unexpected: from the application code (like a payload read exception) or bug in IceRpc |
| | 0 | 62 | | _ => LogLevel.Warning |
| | 1 | 63 | | }; |
| | | 64 | | } |