// enhance-your-grpc-client-logs-with-a-generic-logging-interceptor // https://anthonygiretti.com/2022/08/08/net-6-enhance-your-grpc-client-logs-with-a-generic-logging-interceptor/ using Grpc.Core; using Grpc.Core.Interceptors; using Microsoft.Extensions.Logging; namespace EonaCat.gRPC.Client.Helpers; public class TracerInterceptor : Interceptor { private readonly ILogger _logger; public TracerInterceptor(ILoggerFactory logger) { _logger = logger.CreateLogger(); } public override AsyncClientStreamingCall AsyncClientStreamingCall( ClientInterceptorContext context, AsyncClientStreamingCallContinuation continuation) where TRequest : class where TResponse : class { _logger.LogDebug($"Calling {context.Method.Name} {context.Method.Type} method at {DateTime.UtcNow} UTC from machine {Environment.MachineName}"); var continued = continuation(context); return continued; } public override AsyncDuplexStreamingCall AsyncDuplexStreamingCall( ClientInterceptorContext context, AsyncDuplexStreamingCallContinuation continuation) where TRequest : class where TResponse : class { _logger.LogDebug($"Calling {context.Method.Name} {context.Method.Type} method at {DateTime.UtcNow} UTC from machine {Environment.MachineName}"); var continued = continuation(context); return continued; } public override AsyncServerStreamingCall AsyncServerStreamingCall( TRequest request, ClientInterceptorContext context, AsyncServerStreamingCallContinuation continuation) where TRequest : class where TResponse : class { _logger.LogDebug($"Calling {context.Method.Name} {context.Method.Type} method. Payload received: {request.GetType()} : {request}"); _logger.LogDebug($"Calling {context.Method.Name} {context.Method.Type} method at {DateTime.UtcNow} UTC from machine {Environment.MachineName}"); var continued = continuation(request, context); return continued; } public override AsyncUnaryCall AsyncUnaryCall( TRequest request, ClientInterceptorContext context, AsyncUnaryCallContinuation continuation) where TRequest : class where TResponse : class { _logger.LogDebug($"Calling {context.Method.Name} {context.Method.Type} method. Payload received: {request.GetType()} : {request}"); _logger.LogDebug($"Calling {context.Method.Name} {context.Method.Type} method at {DateTime.UtcNow} UTC from machine {Environment.MachineName}"); var continued = continuation(request, context); return continued; } }