Skip to content

Commit

Permalink
Log server cancellation errors at info level (#2527)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Sep 7, 2024
1 parent 5322d6b commit 3ed9fb9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/Grpc.AspNetCore.Server/Internal/GrpcServerLog.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region Copyright notice and license
#region Copyright notice and license

// Copyright 2019 The gRPC Authors
//
Expand Down Expand Up @@ -104,6 +104,9 @@ internal static class GrpcServerLog
private static readonly Action<ILogger, Exception?> _deadlineStopped =
LoggerMessage.Define(LogLevel.Trace, new EventId(27, "DeadlineStopped"), "Request deadline stopped.");

private static readonly Action<ILogger, string, Exception?> _serviceMethodCanceled =
LoggerMessage.Define<string>(LogLevel.Information, new EventId(28, "ServiceMethodCanceled"), "Service method '{ServiceMethod}' canceled.");

internal static void DeadlineStopped(ILogger logger)
{
_deadlineStopped(logger, null);
Expand Down Expand Up @@ -238,4 +241,9 @@ public static void DeadlineTimerRescheduled(ILogger logger, TimeSpan remaining)
{
_deadlineTimerRescheduled(logger, remaining, null);
}

public static void ServiceMethodCanceled(ILogger logger, string serviceMethod, Exception ex)
{
_serviceMethodCanceled(logger, serviceMethod, ex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,16 @@ private void ProcessHandlerError(Exception ex, string method)
}
else
{
GrpcServerLog.ErrorExecutingServiceMethod(Logger, method, ex);
if (ex is OperationCanceledException or IOException && CancellationTokenCore.IsCancellationRequested)
{
// Request cancellation can cause OCE and IOException.
// When the request has been canceled log these error types at the info-level to avoid creating error-level noise.
GrpcServerLog.ServiceMethodCanceled(Logger, method, ex);
}
else
{
GrpcServerLog.ErrorExecutingServiceMethod(Logger, method, ex);
}

var message = ErrorMessageHelper.BuildErrorMessage("Exception was thrown by handler.", ex, Options.EnableDetailedErrors);

Expand Down
2 changes: 1 addition & 1 deletion test/FunctionalTests/Client/StreamingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ async Task<DataComplete> ClientStreamedData(IAsyncStreamReader<DataMessage> requ
AssertHasLog(LogLevel.Information, "GrpcStatusError", "Call failed with gRPC error status. Status code: 'Cancelled', Message: ''.");

await TestHelpers.AssertIsTrueRetryAsync(
() => HasLog(LogLevel.Error, "ErrorExecutingServiceMethod", "Error when executing service method 'ClientStreamedDataTimeout'."),
() => HasLog(LogLevel.Information, "ServiceMethodCanceled", "Service method 'ClientStreamedDataTimeout' canceled."),
"Wait for server error so it doesn't impact other tests.").DefaultTimeout();
}

Expand Down

0 comments on commit 3ed9fb9

Please sign in to comment.