-
Notifications
You must be signed in to change notification settings - Fork 8
/
LoggingMiddleware.cs
48 lines (44 loc) · 1.7 KB
/
LoggingMiddleware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using Grpc.Core;
using System.Threading.Tasks;
using Grpc.Core.Interceptors;
using Microsoft.Extensions.Logging;
namespace Momento.Sdk.Config.Middleware
{
/// <summary>
/// Basic middleware that logs at debug level for each request/response. This
/// is mostly provided as an example of how to implement middlewares.
/// </summary>
public class LoggingMiddleware : IMiddleware
{
private readonly ILogger _logger;
/// <summary>
///
/// </summary>
/// <param name="loggerFactory"></param>
public LoggingMiddleware(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<LoggingMiddleware>();
}
/// <inheritdoc/>
public async Task<MiddlewareResponseState<TResponse>> WrapRequest<TRequest, TResponse>(
TRequest request,
CallOptions callOptions,
Func<TRequest, CallOptions, Task<MiddlewareResponseState<TResponse>>> continuation
) where TRequest : class where TResponse : class
{
_logger.LogDebug("Executing request of type: {}", request.GetType());
var nextState = await continuation(request, callOptions);
return new MiddlewareResponseState<TResponse>(
ResponseAsync: nextState.ResponseAsync.ContinueWith(r =>
{
_logger.LogDebug("Got response for request of type: {}", request.GetType());
return r.Result;
}),
ResponseHeadersAsync: nextState.ResponseHeadersAsync,
GetStatus: nextState.GetStatus,
GetTrailers: nextState.GetTrailers
);
}
}
}