Skip to content

Commit dde2e76

Browse files
authored
Fix duplicate log IDs (#589)
1 parent 47e1c79 commit dde2e76

20 files changed

+536
-583
lines changed

src/Grpc.AspNetCore.Server/Internal/CallHandlers/ServerCallHandlerBase.cs

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ public Task HandleCallAsync(HttpContext httpContext)
5858
{
5959
if (GrpcProtocolHelpers.IsInvalidContentType(httpContext, out var error))
6060
{
61-
Log.UnsupportedRequestContentType(Logger, httpContext.Request.ContentType);
61+
GrpcServerLog.UnsupportedRequestContentType(Logger, httpContext.Request.ContentType);
6262

6363
GrpcProtocolHelpers.BuildHttpErrorResponse(httpContext.Response, StatusCodes.Status415UnsupportedMediaType, StatusCode.Internal, error);
6464
return Task.CompletedTask;
6565
}
6666
if (httpContext.Request.Protocol != GrpcProtocolConstants.Http2Protocol &&
6767
httpContext.Request.Protocol != GrpcProtocolConstants.Http20Protocol)
6868
{
69-
Log.UnsupportedRequestProtocol(Logger, httpContext.Request.Protocol);
69+
GrpcServerLog.UnsupportedRequestProtocol(Logger, httpContext.Request.Protocol);
7070

7171
var protocolError = $"Request protocol '{httpContext.Request.Protocol}' is not supported.";
7272
GrpcProtocolHelpers.BuildHttpErrorResponse(httpContext.Response, StatusCodes.Status426UpgradeRequired, StatusCode.Internal, protocolError);
@@ -138,36 +138,9 @@ protected void DisableMinRequestBodyDataRateAndMaxRequestBodySize(HttpContext ht
138138
{
139139
// IsReadOnly could be true if middleware has already started reading the request body
140140
// In that case we can't disable the max request body size for the request stream
141-
Log.UnableToDisableMaxRequestBodySize(Logger);
141+
GrpcServerLog.UnableToDisableMaxRequestBodySize(Logger);
142142
}
143143
}
144144
}
145-
146-
private static class Log
147-
{
148-
private static readonly Action<ILogger, Exception?> _unableToDisableMaxRequestBodySize =
149-
LoggerMessage.Define(LogLevel.Debug, new EventId(1, "UnableToDisableMaxRequestBodySizeLimit"), "Unable to disable the max request body size limit.");
150-
151-
private static readonly Action<ILogger, string?, Exception?> _unsupportedRequestContentType =
152-
LoggerMessage.Define<string?>(LogLevel.Information, new EventId(2, "UnsupportedRequestContentType"), "Request content-type of '{ContentType}' is not supported.");
153-
154-
private static readonly Action<ILogger, string?, Exception?> _unsupportedRequestProtocol =
155-
LoggerMessage.Define<string?>(LogLevel.Information, new EventId(3, "UnsupportedRequestProtocol"), "Request protocol of '{Protocol}' is not supported.");
156-
157-
public static void UnableToDisableMaxRequestBodySize(ILogger logger)
158-
{
159-
_unableToDisableMaxRequestBodySize(logger, null);
160-
}
161-
162-
public static void UnsupportedRequestContentType(ILogger logger, string? contentType)
163-
{
164-
_unsupportedRequestContentType(logger, contentType, null);
165-
}
166-
167-
public static void UnsupportedRequestProtocol(ILogger logger, string? protocol)
168-
{
169-
_unsupportedRequestProtocol(logger, protocol, null);
170-
}
171-
}
172145
}
173146
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using System;
20+
using Grpc.Core;
21+
using Microsoft.Extensions.Logging;
22+
23+
namespace Grpc.AspNetCore.Server.Internal
24+
{
25+
internal static class GrpcServerLog
26+
{
27+
private static readonly Action<ILogger, Exception?> _unableToDisableMaxRequestBodySize =
28+
LoggerMessage.Define(LogLevel.Debug, new EventId(1, "UnableToDisableMaxRequestBodySizeLimit"), "Unable to disable the max request body size limit.");
29+
30+
private static readonly Action<ILogger, string?, Exception?> _unsupportedRequestContentType =
31+
LoggerMessage.Define<string?>(LogLevel.Information, new EventId(2, "UnsupportedRequestContentType"), "Request content-type of '{ContentType}' is not supported.");
32+
33+
private static readonly Action<ILogger, string?, Exception?> _unsupportedRequestProtocol =
34+
LoggerMessage.Define<string?>(LogLevel.Information, new EventId(3, "UnsupportedRequestProtocol"), "Request protocol of '{Protocol}' is not supported.");
35+
36+
private static readonly Action<ILogger, TimeSpan, Exception?> _deadlineExceeded =
37+
LoggerMessage.Define<TimeSpan>(LogLevel.Debug, new EventId(4, "DeadlineExceeded"), "Request with timeout of {Timeout} has exceeded its deadline.");
38+
39+
private static readonly Action<ILogger, string, Exception?> _invalidTimeoutIgnored =
40+
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(5, "InvalidTimeoutIgnored"), "Invalid grpc-timeout header value '{Timeout}' has been ignored.");
41+
42+
private static readonly Action<ILogger, string, Exception?> _errorExecutingServiceMethod =
43+
LoggerMessage.Define<string>(LogLevel.Error, new EventId(6, "ErrorExecutingServiceMethod"), "Error when executing service method '{ServiceMethod}'.");
44+
45+
private static readonly Action<ILogger, StatusCode, Exception?> _rpcConnectionError =
46+
LoggerMessage.Define<StatusCode>(LogLevel.Information, new EventId(7, "RpcConnectionError"), "Error status code '{StatusCode}' raised.");
47+
48+
private static readonly Action<ILogger, string, Exception?> _encodingNotInAcceptEncoding =
49+
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(8, "EncodingNotInAcceptEncoding"), "Request grpc-encoding header value '{GrpcEncoding}' is not in grpc-accept-encoding.");
50+
51+
private static readonly Action<ILogger, Exception?> _deadlineCancellationError =
52+
LoggerMessage.Define(LogLevel.Error, new EventId(9, "DeadlineCancellationError"), "Error occurred while trying to cancel the request due to deadline exceeded.");
53+
54+
private static readonly Action<ILogger, Exception?> _readingMessage =
55+
LoggerMessage.Define(LogLevel.Debug, new EventId(10, "ReadingMessage"), "Reading message.");
56+
57+
private static readonly Action<ILogger, Exception?> _noMessageReturned =
58+
LoggerMessage.Define(LogLevel.Trace, new EventId(11, "NoMessageReturned"), "No message returned.");
59+
60+
private static readonly Action<ILogger, int, Type, Exception?> _deserializingMessage =
61+
LoggerMessage.Define<int, Type>(LogLevel.Trace, new EventId(12, "DeserializingMessage"), "Deserializing {MessageLength} byte message to '{MessageType}'.");
62+
63+
private static readonly Action<ILogger, Exception?> _receivedMessage =
64+
LoggerMessage.Define(LogLevel.Trace, new EventId(13, "ReceivedMessage"), "Received message.");
65+
66+
private static readonly Action<ILogger, Exception?> _errorReadingMessage =
67+
LoggerMessage.Define(LogLevel.Error, new EventId(14, "ErrorReadingMessage"), "Error reading message.");
68+
69+
private static readonly Action<ILogger, Exception?> _sendingMessage =
70+
LoggerMessage.Define(LogLevel.Debug, new EventId(15, "SendingMessage"), "Sending message.");
71+
72+
private static readonly Action<ILogger, Exception?> _messageSent =
73+
LoggerMessage.Define(LogLevel.Trace, new EventId(16, "MessageSent"), "Message sent.");
74+
75+
private static readonly Action<ILogger, Exception?> _errorSendingMessage =
76+
LoggerMessage.Define(LogLevel.Error, new EventId(17, "ErrorSendingMessage"), "Error sending message.");
77+
78+
private static readonly Action<ILogger, Type, int, Exception?> _serializedMessage =
79+
LoggerMessage.Define<Type, int>(LogLevel.Trace, new EventId(18, "SerializedMessage"), "Serialized '{MessageType}' to {MessageLength} byte message.");
80+
81+
private static readonly Action<ILogger, string, Exception?> _compressingMessage =
82+
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(19, "CompressingMessage"), "Compressing message with '{MessageEncoding}' encoding.");
83+
84+
private static readonly Action<ILogger, string, Exception?> _decompressingMessage =
85+
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(20, "DecompressingMessage"), "Decompressing message with '{MessageEncoding}' encoding.");
86+
87+
public static void DeadlineExceeded(ILogger logger, TimeSpan timeout)
88+
{
89+
_deadlineExceeded(logger, timeout, null);
90+
}
91+
92+
public static void InvalidTimeoutIgnored(ILogger logger, string timeout)
93+
{
94+
_invalidTimeoutIgnored(logger, timeout, null);
95+
}
96+
97+
public static void ErrorExecutingServiceMethod(ILogger logger, string serviceMethod, Exception ex)
98+
{
99+
_errorExecutingServiceMethod(logger, serviceMethod, ex);
100+
}
101+
102+
public static void RpcConnectionError(ILogger logger, StatusCode statusCode, Exception ex)
103+
{
104+
_rpcConnectionError(logger, statusCode, ex);
105+
}
106+
107+
public static void EncodingNotInAcceptEncoding(ILogger logger, string grpcEncoding)
108+
{
109+
_encodingNotInAcceptEncoding(logger, grpcEncoding, null);
110+
}
111+
112+
public static void DeadlineCancellationError(ILogger logger, Exception ex)
113+
{
114+
_deadlineCancellationError(logger, ex);
115+
}
116+
117+
public static void UnableToDisableMaxRequestBodySize(ILogger logger)
118+
{
119+
_unableToDisableMaxRequestBodySize(logger, null);
120+
}
121+
122+
public static void UnsupportedRequestContentType(ILogger logger, string? contentType)
123+
{
124+
_unsupportedRequestContentType(logger, contentType, null);
125+
}
126+
127+
public static void UnsupportedRequestProtocol(ILogger logger, string? protocol)
128+
{
129+
_unsupportedRequestProtocol(logger, protocol, null);
130+
}
131+
132+
public static void ReadingMessage(ILogger logger)
133+
{
134+
_readingMessage(logger, null);
135+
}
136+
137+
public static void NoMessageReturned(ILogger logger)
138+
{
139+
_noMessageReturned(logger, null);
140+
}
141+
142+
public static void DeserializingMessage(ILogger logger, int messageLength, Type messageType)
143+
{
144+
_deserializingMessage(logger, messageLength, messageType, null);
145+
}
146+
147+
public static void ReceivedMessage(ILogger logger)
148+
{
149+
_receivedMessage(logger, null);
150+
}
151+
152+
public static void ErrorReadingMessage(ILogger logger, Exception ex)
153+
{
154+
_errorReadingMessage(logger, ex);
155+
}
156+
157+
public static void SendingMessage(ILogger logger)
158+
{
159+
_sendingMessage(logger, null);
160+
}
161+
162+
public static void MessageSent(ILogger logger)
163+
{
164+
_messageSent(logger, null);
165+
}
166+
167+
public static void ErrorSendingMessage(ILogger logger, Exception ex)
168+
{
169+
_errorSendingMessage(logger, ex);
170+
}
171+
172+
public static void SerializedMessage(ILogger logger, Type messageType, int messageLength)
173+
{
174+
_serializedMessage(logger, messageType, messageLength, null);
175+
}
176+
177+
public static void CompressingMessage(ILogger logger, string messageEncoding)
178+
{
179+
_compressingMessage(logger, messageEncoding, null);
180+
}
181+
182+
public static void DecompressingMessage(ILogger logger, string messageEncoding)
183+
{
184+
_decompressingMessage(logger, messageEncoding, null);
185+
}
186+
}
187+
}

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

Lines changed: 0 additions & 78 deletions
This file was deleted.

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ private void ProcessHandlerError(Exception ex, string method)
152152
{
153153
if (ex is RpcException rpcException)
154154
{
155-
Log.RpcConnectionError(Logger, rpcException.StatusCode, ex);
155+
GrpcServerLog.RpcConnectionError(Logger, rpcException.StatusCode, ex);
156156

157157
// There are two sources of metadata entries on the server-side:
158158
// 1. serverCallContext.ResponseTrailers
@@ -168,7 +168,7 @@ private void ProcessHandlerError(Exception ex, string method)
168168
}
169169
else
170170
{
171-
Log.ErrorExecutingServiceMethod(Logger, method, ex);
171+
GrpcServerLog.ErrorExecutingServiceMethod(Logger, method, ex);
172172

173173
var message = ErrorMessageHelper.BuildErrorMessage("Exception was thrown by handler.", ex, ServiceOptions.EnableDetailedErrors);
174174
_status = new Status(StatusCode.Unknown, message);
@@ -422,7 +422,7 @@ private TimeSpan GetTimeout()
422422
return timeout;
423423
}
424424

425-
Log.InvalidTimeoutIgnored(Logger, values);
425+
GrpcServerLog.InvalidTimeoutIgnored(Logger, values);
426426
}
427427

428428
return TimeSpan.Zero;
@@ -432,7 +432,7 @@ private async Task DeadlineExceededAsync()
432432
{
433433
try
434434
{
435-
Log.DeadlineExceeded(Logger, GetTimeout());
435+
GrpcServerLog.DeadlineExceeded(Logger, GetTimeout());
436436
GrpcEventSource.Log.CallDeadlineExceeded();
437437

438438
var status = new Status(StatusCode.DeadlineExceeded, "Deadline Exceeded");
@@ -465,7 +465,7 @@ private async Task DeadlineExceededAsync()
465465
}
466466
catch (Exception ex)
467467
{
468-
Log.DeadlineCancellationError(Logger, ex);
468+
GrpcServerLog.DeadlineCancellationError(Logger, ex);
469469
}
470470
}
471471

@@ -528,7 +528,7 @@ internal void ValidateAcceptEncodingContainsResponseEncoding()
528528

529529
if (!IsEncodingInRequestAcceptEncoding(ResponseGrpcEncoding))
530530
{
531-
Log.EncodingNotInAcceptEncoding(Logger, ResponseGrpcEncoding);
531+
GrpcServerLog.EncodingNotInAcceptEncoding(Logger, ResponseGrpcEncoding);
532532
}
533533
}
534534
}

0 commit comments

Comments
 (0)