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

Move static Log classes out of generic types #1253

Merged
merged 2 commits into from
Apr 1, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
using System.Threading;
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Shared;

namespace Grpc.AspNetCore.Server.Internal
{
internal class HttpContextStreamReader<TRequest> : IAsyncStreamReader<TRequest> where TRequest : class
{
private static readonly Task<bool> True = Task.FromResult(true);
private static readonly Task<bool> False = Task.FromResult(false);

private readonly HttpContextServerCallContext _serverCallContext;
private readonly Func<DeserializationContext, TRequest> _deserializer;
private bool _completed;
Expand Down Expand Up @@ -69,7 +67,9 @@ async Task<bool> MoveNextAsync(ValueTask<TRequest?> readStreamTask)
return MoveNextAsync(request);
}

return ProcessPayload(request.Result) ? True : False;
return ProcessPayload(request.Result)
? CommonGrpcProtocolHelpers.TrueTask
: CommonGrpcProtocolHelpers.FalseTask;
}

private bool ProcessPayload(TRequest? request)
Expand Down
29 changes: 15 additions & 14 deletions src/Grpc.AspNetCore.Server/Internal/ServerCallHandlerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Log = Grpc.AspNetCore.Server.Internal.ServerCallHandlerFactoryLog;

namespace Grpc.AspNetCore.Server.Internal
{
Expand Down Expand Up @@ -148,24 +149,24 @@ public RequestDelegate CreateUnimplementedService()
return Task.CompletedTask;
};
}
}

private static class Log
{
private static readonly Action<ILogger, string, Exception?> _serviceUnimplemented =
LoggerMessage.Define<string>(LogLevel.Information, new EventId(1, "ServiceUnimplemented"), "Service '{ServiceName}' is unimplemented.");
internal static class ServerCallHandlerFactoryLog
{
private static readonly Action<ILogger, string, Exception?> _serviceUnimplemented =
LoggerMessage.Define<string>(LogLevel.Information, new EventId(1, "ServiceUnimplemented"), "Service '{ServiceName}' is unimplemented.");

private static readonly Action<ILogger, string, Exception?> _methodUnimplemented =
LoggerMessage.Define<string>(LogLevel.Information, new EventId(2, "MethodUnimplemented"), "Method '{MethodName}' is unimplemented.");
private static readonly Action<ILogger, string, Exception?> _methodUnimplemented =
LoggerMessage.Define<string>(LogLevel.Information, new EventId(2, "MethodUnimplemented"), "Method '{MethodName}' is unimplemented.");

public static void ServiceUnimplemented(ILogger logger, string serviceName)
{
_serviceUnimplemented(logger, serviceName, null);
}
public static void ServiceUnimplemented(ILogger logger, string serviceName)
{
_serviceUnimplemented(logger, serviceName, null);
}

public static void MethodUnimplemented(ILogger logger, string methodName)
{
_methodUnimplemented(logger, methodName, null);
}
public static void MethodUnimplemented(ILogger logger, string methodName)
{
_methodUnimplemented(logger, methodName, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Diagnostics.CodeAnalysis;
using Grpc.Shared.Server;
using Microsoft.Extensions.Logging;
using Log = Grpc.AspNetCore.Server.Model.Internal.BinderServiceMethodProviderLog;

namespace Grpc.AspNetCore.Server.Model.Internal
{
Expand Down Expand Up @@ -62,16 +63,16 @@ public void OnServiceMethodDiscovery(ServiceMethodProviderContext<TService> cont
Log.BindMethodNotFound(_logger, typeof(TService));
}
}
}

private static class Log
{
private static readonly Action<ILogger, Type, Exception?> _bindMethodNotFound =
LoggerMessage.Define<Type>(LogLevel.Debug, new EventId(1, "BindMethodNotFound"), "Could not find bind method for {ServiceType}.");
internal static class BinderServiceMethodProviderLog
{
private static readonly Action<ILogger, Type, Exception?> _bindMethodNotFound =
LoggerMessage.Define<Type>(LogLevel.Debug, new EventId(1, "BindMethodNotFound"), "Could not find bind method for {ServiceType}.");

public static void BindMethodNotFound(ILogger logger, Type serviceType)
{
_bindMethodNotFound(logger, serviceType, null);
}
public static void BindMethodNotFound(ILogger logger, Type serviceType)
{
_bindMethodNotFound(logger, serviceType, null);
}
}
}
41 changes: 21 additions & 20 deletions src/Grpc.AspNetCore.Server/Model/Internal/ServiceRouteBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Patterns;
using Microsoft.Extensions.Logging;
using Log = Grpc.AspNetCore.Server.Model.Internal.ServiceRouteBuilderLog;

namespace Grpc.AspNetCore.Server.Model.Internal
{
Expand Down Expand Up @@ -184,32 +185,32 @@ private GrpcUnimplementedConstraint()
{
}
}
}

private static class Log
{
private static readonly Action<ILogger, string, string, MethodType, string, Exception?> _addedServiceMethod =
LoggerMessage.Define<string, string, MethodType, string>(LogLevel.Trace, new EventId(1, "AddedServiceMethod"), "Added gRPC method '{MethodName}' to service '{ServiceName}'. Method type: '{MethodType}', route pattern: '{RoutePattern}'.");
internal static class ServiceRouteBuilderLog
{
private static readonly Action<ILogger, string, string, MethodType, string, Exception?> _addedServiceMethod =
LoggerMessage.Define<string, string, MethodType, string>(LogLevel.Trace, new EventId(1, "AddedServiceMethod"), "Added gRPC method '{MethodName}' to service '{ServiceName}'. Method type: '{MethodType}', route pattern: '{RoutePattern}'.");

private static readonly Action<ILogger, Type, Exception?> _discoveringServiceMethods =
LoggerMessage.Define<Type>(LogLevel.Trace, new EventId(2, "DiscoveringServiceMethods"), "Discovering gRPC methods for {ServiceType}.");
private static readonly Action<ILogger, Type, Exception?> _discoveringServiceMethods =
LoggerMessage.Define<Type>(LogLevel.Trace, new EventId(2, "DiscoveringServiceMethods"), "Discovering gRPC methods for {ServiceType}.");

private static readonly Action<ILogger, Type, Exception?> _noServiceMethodsDiscovered =
LoggerMessage.Define<Type>(LogLevel.Debug, new EventId(3, "NoServiceMethodsDiscovered"), "No gRPC methods discovered for {ServiceType}.");
private static readonly Action<ILogger, Type, Exception?> _noServiceMethodsDiscovered =
LoggerMessage.Define<Type>(LogLevel.Debug, new EventId(3, "NoServiceMethodsDiscovered"), "No gRPC methods discovered for {ServiceType}.");

public static void AddedServiceMethod(ILogger logger, string methodName, string serviceName, MethodType methodType, string routePattern)
{
_addedServiceMethod(logger, methodName, serviceName, methodType, routePattern, null);
}
public static void AddedServiceMethod(ILogger logger, string methodName, string serviceName, MethodType methodType, string routePattern)
{
_addedServiceMethod(logger, methodName, serviceName, methodType, routePattern, null);
}

public static void DiscoveringServiceMethods(ILogger logger, Type serviceType)
{
_discoveringServiceMethods(logger, serviceType, null);
}
public static void DiscoveringServiceMethods(ILogger logger, Type serviceType)
{
_discoveringServiceMethods(logger, serviceType, null);
}

public static void NoServiceMethodsDiscovered(ILogger logger, Type serviceType)
{
_noServiceMethodsDiscovered(logger, serviceType, null);
}
public static void NoServiceMethodsDiscovered(ILogger logger, Type serviceType)
{
_noServiceMethodsDiscovered(logger, serviceType, null);
}
}
}
41 changes: 21 additions & 20 deletions src/Grpc.Net.Client/Internal/ClientStreamWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Threading.Tasks;
using Grpc.Core;
using Microsoft.Extensions.Logging;
using Log = Grpc.Net.Client.Internal.ClientStreamWriterBaseLog;

namespace Grpc.Net.Client.Internal
{
Expand Down Expand Up @@ -69,32 +70,32 @@ protected bool IsWriteInProgressUnsynchronized
return writeTask != null && !writeTask.IsCompleted;
}
}
}

protected static class Log
{
private static readonly Action<ILogger, Exception?> _completingClientStream =
LoggerMessage.Define(LogLevel.Debug, new EventId(1, "CompletingClientStream"), "Completing client stream.");
internal static class ClientStreamWriterBaseLog
{
private static readonly Action<ILogger, Exception?> _completingClientStream =
LoggerMessage.Define(LogLevel.Debug, new EventId(1, "CompletingClientStream"), "Completing client stream.");

private static readonly Action<ILogger, Exception?> _writeMessageError =
LoggerMessage.Define(LogLevel.Error, new EventId(2, "WriteMessageError"), "Error writing message.");
private static readonly Action<ILogger, Exception?> _writeMessageError =
LoggerMessage.Define(LogLevel.Error, new EventId(2, "WriteMessageError"), "Error writing message.");

private static readonly Action<ILogger, Exception?> _completeClientStreamError =
LoggerMessage.Define(LogLevel.Error, new EventId(3, "CompleteClientStreamError"), "Error completing client stream.");
private static readonly Action<ILogger, Exception?> _completeClientStreamError =
LoggerMessage.Define(LogLevel.Error, new EventId(3, "CompleteClientStreamError"), "Error completing client stream.");

public static void CompletingClientStream(ILogger logger)
{
_completingClientStream(logger, null);
}
public static void CompletingClientStream(ILogger logger)
{
_completingClientStream(logger, null);
}

public static void WriteMessageError(ILogger logger, Exception ex)
{
_writeMessageError(logger, ex);
}
public static void WriteMessageError(ILogger logger, Exception ex)
{
_writeMessageError(logger, ex);
}

public static void CompleteClientStreamError(ILogger logger, Exception ex)
{
_completeClientStreamError(logger, ex);
}
public static void CompleteClientStreamError(ILogger logger, Exception ex)
{
_completeClientStreamError(logger, ex);
}
}
}
21 changes: 10 additions & 11 deletions src/Grpc.Net.Client/Internal/HttpContentClientStreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using Grpc.Core;
using Grpc.Shared;
using Microsoft.Extensions.Logging;
using Log = Grpc.Net.Client.Internal.HttpContentClientStreamReaderLog;

namespace Grpc.Net.Client.Internal
{
Expand All @@ -35,8 +36,6 @@ internal class HttpContentClientStreamReader<TRequest, TResponse> : IAsyncStream
// Getting logger name from generic type is slow. Cached copy.
private const string LoggerName = "Grpc.Net.Client.Internal.HttpContentClientStreamReader";

private static readonly Task<bool> FinishedTask = Task.FromResult(false);

private readonly GrpcCall<TRequest, TResponse> _call;
private readonly ILogger _logger;
private readonly object _moveNextLock;
Expand Down Expand Up @@ -90,7 +89,7 @@ public Task<bool> MoveNext(CancellationToken cancellationToken)
if (status.StatusCode == StatusCode.OK)
{
// Response is finished and it was successful so just return false
return FinishedTask;
return CommonGrpcProtocolHelpers.FalseTask;
}
else
{
Expand Down Expand Up @@ -247,16 +246,16 @@ private bool IsMoveNextInProgressUnsynchronized
return moveNextTask != null && !moveNextTask.IsCompleted;
}
}
}

private static class Log
{
private static readonly Action<ILogger, Exception> _readMessageError =
LoggerMessage.Define(LogLevel.Error, new EventId(1, "ReadMessageError"), "Error reading message.");
internal static class HttpContentClientStreamReaderLog
{
private static readonly Action<ILogger, Exception> _readMessageError =
LoggerMessage.Define(LogLevel.Error, new EventId(1, "ReadMessageError"), "Error reading message.");

public static void ReadMessageError(ILogger logger, Exception ex)
{
_readMessageError(logger, ex);
}
public static void ReadMessageError(ILogger logger, Exception ex)
{
_readMessageError(logger, ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Shared;
using Log = Grpc.Net.Client.Internal.ClientStreamWriterBaseLog;

#if NETSTANDARD2_0
using ValueTask = System.Threading.Tasks.Task;
Expand Down
1 change: 1 addition & 0 deletions src/Grpc.Net.Client/Internal/Retry/HedgingCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Shared;
using Log = Grpc.Net.Client.Internal.Retry.RetryCallBaseLog;

namespace Grpc.Net.Client.Internal.Retry
{
Expand Down
1 change: 1 addition & 0 deletions src/Grpc.Net.Client/Internal/Retry/RetryCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Shared;
using Log = Grpc.Net.Client.Internal.Retry.RetryCallBaseLog;

namespace Grpc.Net.Client.Internal.Retry
{
Expand Down
Loading