Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log server cancellation errors at info level #2527

Merged
merged 2 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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