Skip to content

Commit 3ed9fb9

Browse files
authored
Log server cancellation errors at info level (#2527)
1 parent 5322d6b commit 3ed9fb9

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/Grpc.AspNetCore.Server/Internal/GrpcServerLog.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region Copyright notice and license
1+
#region Copyright notice and license
22

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

107+
private static readonly Action<ILogger, string, Exception?> _serviceMethodCanceled =
108+
LoggerMessage.Define<string>(LogLevel.Information, new EventId(28, "ServiceMethodCanceled"), "Service method '{ServiceMethod}' canceled.");
109+
107110
internal static void DeadlineStopped(ILogger logger)
108111
{
109112
_deadlineStopped(logger, null);
@@ -238,4 +241,9 @@ public static void DeadlineTimerRescheduled(ILogger logger, TimeSpan remaining)
238241
{
239242
_deadlineTimerRescheduled(logger, remaining, null);
240243
}
244+
245+
public static void ServiceMethodCanceled(ILogger logger, string serviceMethod, Exception ex)
246+
{
247+
_serviceMethodCanceled(logger, serviceMethod, ex);
248+
}
241249
}

src/Grpc.AspNetCore.Server/Internal/HttpContextServerCallContext.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,16 @@ private void ProcessHandlerError(Exception ex, string method)
193193
}
194194
else
195195
{
196-
GrpcServerLog.ErrorExecutingServiceMethod(Logger, method, ex);
196+
if (ex is OperationCanceledException or IOException && CancellationTokenCore.IsCancellationRequested)
197+
{
198+
// Request cancellation can cause OCE and IOException.
199+
// When the request has been canceled log these error types at the info-level to avoid creating error-level noise.
200+
GrpcServerLog.ServiceMethodCanceled(Logger, method, ex);
201+
}
202+
else
203+
{
204+
GrpcServerLog.ErrorExecutingServiceMethod(Logger, method, ex);
205+
}
197206

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

test/FunctionalTests/Client/StreamingTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ async Task<DataComplete> ClientStreamedData(IAsyncStreamReader<DataMessage> requ
429429
AssertHasLog(LogLevel.Information, "GrpcStatusError", "Call failed with gRPC error status. Status code: 'Cancelled', Message: ''.");
430430

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

0 commit comments

Comments
 (0)