Skip to content

Commit

Permalink
fix: remove Console.WriteLine statements and replace with logging (#188)
Browse files Browse the repository at this point in the history
* fix: remove Console.WriteLine statements and replace with logging
  • Loading branch information
cprice404 authored Sep 26, 2022
1 parent b3f3b79 commit 378b90b
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Momento.Sdk/Config/Middleware/LoggingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,35 @@
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;

public LoggingMiddleware(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<LoggingMiddleware>();
}

public async Task<MiddlewareResponseState<TResponse>> WrapRequest<TRequest, TResponse>(
TRequest request,
CallOptions callOptions,
Func<TRequest, CallOptions, Task<MiddlewareResponseState<TResponse>>> continuation
)
{
Console.WriteLine($"LOGGING MIDDLEWARE WRAPPING REQUEST: {request.GetType()}");
_logger.LogDebug("Executing request of type: {}", request?.GetType());
var nextState = await continuation(request, callOptions);
Console.WriteLine($"LOGGING MIDDLEWARE WRAPPED REQUEST: {request.GetType()}");
return new MiddlewareResponseState<TResponse>(
ResponseAsync: nextState.ResponseAsync.ContinueWith(r =>
{
Console.WriteLine($"LOGGING MIDDLEWARE RESPONSE CALLBACK: {request.GetType()}");
_logger.LogDebug("Got response for request of type: {}", request?.GetType());
return r.Result;
}),
ResponseHeadersAsync: nextState.ResponseHeadersAsync,
Expand Down

0 comments on commit 378b90b

Please sign in to comment.