namespace EonaCat.gRPC.Api.Middleware.Interceptors; public class LoggerInterceptor : Interceptor { private readonly ILogger _logger; public LoggerInterceptor(ILogger logger) { _logger = logger; } public override async Task UnaryServerHandler( TRequest request, ServerCallContext context, UnaryServerMethod continuation) { LogCall(context); try { return await continuation(request, context); } catch (SqlException e) { _logger.LogError(e, $"An SQL error occurred when calling {context.Method}"); Status status = e.Number == -2 ? new Status(StatusCode.DeadlineExceeded, $"SQL timeout: {e.Message}") : new Status(StatusCode.Internal, $"SQL error: {e.Message}"); throw new RpcException(status, e.Message); } catch (RpcException e) { _logger.LogError(e, $"gRPC error when calling {context.Method}: {e.Status.Detail}"); throw; } catch (Exception e) { _logger.LogError(e, $"An error occurred when calling {context.Method}"); throw new RpcException(Status.DefaultCancelled, e.Message); } } private void LogCall(ServerCallContext context) { var httpContext = context.GetHttpContext(); _logger.LogDebug($"Starting call. Request: {httpContext.Request.Path}"); } }