Skip to content

Commit 3f93a6b

Browse files
committed
Update
1 parent eb02e6c commit 3f93a6b

File tree

3 files changed

+177
-306
lines changed

3 files changed

+177
-306
lines changed

test/Grpc.Net.Client.Tests/AsyncUnaryCallTests.cs

Lines changed: 0 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -321,157 +321,4 @@ static async Task MakeGrpcCallAsync(ILogger logger, CallInvoker invoker, Respons
321321
TaskScheduler.UnobservedTaskException -= onUnobservedTaskException;
322322
}
323323
}
324-
325-
private class ClientLoggerInterceptor : Interceptor
326-
{
327-
private readonly ILogger<ClientLoggerInterceptor> _logger;
328-
329-
public ClientLoggerInterceptor(ILoggerFactory loggerFactory)
330-
{
331-
_logger = loggerFactory.CreateLogger<ClientLoggerInterceptor>();
332-
}
333-
334-
public override TResponse BlockingUnaryCall<TRequest, TResponse>(
335-
TRequest request,
336-
ClientInterceptorContext<TRequest, TResponse> context,
337-
BlockingUnaryCallContinuation<TRequest, TResponse> continuation)
338-
{
339-
LogCall(context.Method);
340-
AddCallerMetadata(ref context);
341-
342-
try
343-
{
344-
return continuation(request, context);
345-
}
346-
catch (Exception ex)
347-
{
348-
LogError(ex);
349-
throw;
350-
}
351-
}
352-
353-
public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(
354-
TRequest request,
355-
ClientInterceptorContext<TRequest, TResponse> context,
356-
AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
357-
{
358-
LogCall(context.Method);
359-
AddCallerMetadata(ref context);
360-
361-
try
362-
{
363-
var call = continuation(request, context);
364-
365-
return new AsyncUnaryCall<TResponse>(HandleResponse(call.ResponseAsync), call.ResponseHeadersAsync, call.GetStatus, call.GetTrailers, call.Dispose);
366-
}
367-
catch (Exception ex)
368-
{
369-
LogError(ex);
370-
throw;
371-
}
372-
}
373-
374-
private async Task<TResponse> HandleResponse<TResponse>(Task<TResponse> t)
375-
{
376-
try
377-
{
378-
var response = await t;
379-
_logger.LogInformation($"Response received: {response}");
380-
return response;
381-
}
382-
catch (Exception ex)
383-
{
384-
LogError(ex);
385-
throw;
386-
}
387-
}
388-
389-
public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(
390-
ClientInterceptorContext<TRequest, TResponse> context,
391-
AsyncClientStreamingCallContinuation<TRequest, TResponse> continuation)
392-
{
393-
LogCall(context.Method);
394-
AddCallerMetadata(ref context);
395-
396-
try
397-
{
398-
return continuation(context);
399-
}
400-
catch (Exception ex)
401-
{
402-
LogError(ex);
403-
throw;
404-
}
405-
}
406-
407-
public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(
408-
TRequest request,
409-
ClientInterceptorContext<TRequest, TResponse> context,
410-
AsyncServerStreamingCallContinuation<TRequest, TResponse> continuation)
411-
{
412-
LogCall(context.Method);
413-
AddCallerMetadata(ref context);
414-
415-
try
416-
{
417-
return continuation(request, context);
418-
}
419-
catch (Exception ex)
420-
{
421-
LogError(ex);
422-
throw;
423-
}
424-
}
425-
426-
public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(
427-
ClientInterceptorContext<TRequest, TResponse> context,
428-
AsyncDuplexStreamingCallContinuation<TRequest, TResponse> continuation)
429-
{
430-
LogCall(context.Method);
431-
AddCallerMetadata(ref context);
432-
433-
try
434-
{
435-
return continuation(context);
436-
}
437-
catch (Exception ex)
438-
{
439-
LogError(ex);
440-
throw;
441-
}
442-
}
443-
444-
private void LogCall<TRequest, TResponse>(Method<TRequest, TResponse> method)
445-
where TRequest : class
446-
where TResponse : class
447-
{
448-
_logger.LogInformation($"Starting call. Name: {method.Name}. Type: {method.Type}. Request: {typeof(TRequest)}. Response: {typeof(TResponse)}");
449-
}
450-
451-
private void AddCallerMetadata<TRequest, TResponse>(ref ClientInterceptorContext<TRequest, TResponse> context)
452-
where TRequest : class
453-
where TResponse : class
454-
{
455-
var headers = context.Options.Headers;
456-
457-
// Call doesn't have a headers collection to add to.
458-
// Need to create a new context with headers for the call.
459-
if (headers == null)
460-
{
461-
headers = new Metadata();
462-
var options = context.Options.WithHeaders(headers);
463-
context = new ClientInterceptorContext<TRequest, TResponse>(context.Method, context.Host, options);
464-
}
465-
466-
// Add caller metadata to call headers
467-
headers.Add("caller-user", Environment.UserName);
468-
headers.Add("caller-machine", Environment.MachineName);
469-
headers.Add("caller-os", Environment.OSVersion.ToString());
470-
}
471-
472-
private void LogError(Exception ex)
473-
{
474-
_logger.LogError(ex, $"Call error: {ex.Message}");
475-
}
476-
}
477324
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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 Grpc.Core;
20+
using Grpc.Core.Interceptors;
21+
using Microsoft.Extensions.Logging;
22+
23+
namespace Grpc.Net.Client.Tests.Infrastructure;
24+
25+
public sealed class ClientLoggerInterceptor : Interceptor
26+
{
27+
private readonly ILogger<ClientLoggerInterceptor> _logger;
28+
29+
public ClientLoggerInterceptor(ILoggerFactory loggerFactory)
30+
{
31+
_logger = loggerFactory.CreateLogger<ClientLoggerInterceptor>();
32+
}
33+
34+
public override TResponse BlockingUnaryCall<TRequest, TResponse>(
35+
TRequest request,
36+
ClientInterceptorContext<TRequest, TResponse> context,
37+
BlockingUnaryCallContinuation<TRequest, TResponse> continuation)
38+
{
39+
LogCall(context.Method);
40+
AddCallerMetadata(ref context);
41+
42+
try
43+
{
44+
return continuation(request, context);
45+
}
46+
catch (Exception ex)
47+
{
48+
LogError(ex);
49+
throw;
50+
}
51+
}
52+
53+
public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(
54+
TRequest request,
55+
ClientInterceptorContext<TRequest, TResponse> context,
56+
AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
57+
{
58+
LogCall(context.Method);
59+
AddCallerMetadata(ref context);
60+
61+
try
62+
{
63+
var call = continuation(request, context);
64+
65+
return new AsyncUnaryCall<TResponse>(HandleResponse(call.ResponseAsync), call.ResponseHeadersAsync, call.GetStatus, call.GetTrailers, call.Dispose);
66+
}
67+
catch (Exception ex)
68+
{
69+
LogError(ex);
70+
throw;
71+
}
72+
}
73+
74+
private async Task<TResponse> HandleResponse<TResponse>(Task<TResponse> t)
75+
{
76+
try
77+
{
78+
var response = await t;
79+
_logger.LogInformation($"Response received: {response}");
80+
return response;
81+
}
82+
catch (Exception ex)
83+
{
84+
LogError(ex);
85+
throw;
86+
}
87+
}
88+
89+
public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(
90+
ClientInterceptorContext<TRequest, TResponse> context,
91+
AsyncClientStreamingCallContinuation<TRequest, TResponse> continuation)
92+
{
93+
LogCall(context.Method);
94+
AddCallerMetadata(ref context);
95+
96+
try
97+
{
98+
return continuation(context);
99+
}
100+
catch (Exception ex)
101+
{
102+
LogError(ex);
103+
throw;
104+
}
105+
}
106+
107+
public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(
108+
TRequest request,
109+
ClientInterceptorContext<TRequest, TResponse> context,
110+
AsyncServerStreamingCallContinuation<TRequest, TResponse> continuation)
111+
{
112+
LogCall(context.Method);
113+
AddCallerMetadata(ref context);
114+
115+
try
116+
{
117+
return continuation(request, context);
118+
}
119+
catch (Exception ex)
120+
{
121+
LogError(ex);
122+
throw;
123+
}
124+
}
125+
126+
public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(
127+
ClientInterceptorContext<TRequest, TResponse> context,
128+
AsyncDuplexStreamingCallContinuation<TRequest, TResponse> continuation)
129+
{
130+
LogCall(context.Method);
131+
AddCallerMetadata(ref context);
132+
133+
try
134+
{
135+
return continuation(context);
136+
}
137+
catch (Exception ex)
138+
{
139+
LogError(ex);
140+
throw;
141+
}
142+
}
143+
144+
private void LogCall<TRequest, TResponse>(Method<TRequest, TResponse> method)
145+
where TRequest : class
146+
where TResponse : class
147+
{
148+
_logger.LogInformation($"Starting call. Name: {method.Name}. Type: {method.Type}. Request: {typeof(TRequest)}. Response: {typeof(TResponse)}");
149+
}
150+
151+
private void AddCallerMetadata<TRequest, TResponse>(ref ClientInterceptorContext<TRequest, TResponse> context)
152+
where TRequest : class
153+
where TResponse : class
154+
{
155+
var headers = context.Options.Headers;
156+
157+
// Call doesn't have a headers collection to add to.
158+
// Need to create a new context with headers for the call.
159+
if (headers == null)
160+
{
161+
headers = new Metadata();
162+
var options = context.Options.WithHeaders(headers);
163+
context = new ClientInterceptorContext<TRequest, TResponse>(context.Method, context.Host, options);
164+
}
165+
166+
// Add caller metadata to call headers
167+
headers.Add("caller-user", Environment.UserName);
168+
headers.Add("caller-machine", Environment.MachineName);
169+
headers.Add("caller-os", Environment.OSVersion.ToString());
170+
}
171+
172+
private void LogError(Exception ex)
173+
{
174+
_logger.LogError(ex, $"Call error: {ex.Message}");
175+
}
176+
}
177+

0 commit comments

Comments
 (0)