0

I would like to log incoming requests with method, path, body and other information. My problem is that after logging what I need the controller get only a null variable as the method parameter. I'm using .net core 1.1, IIoggerFactory, Serilog extension, and the following middleware method:

public async Task Invoke(HttpContext context)
    {  
        StringValues sessionId;
        var session = string.Empty;
        if (context.Request.Headers.TryGetValue("X-SessionID", out sessionId))
        {
            session = sessionId.FirstOrDefault();
            if (session != null) 
            {
                var requestBodyStream = new MemoryStream();
                var originalRequestBody = context.Request.Body;

                await context.Request.Body.CopyToAsync(requestBodyStream);
                requestBodyStream.Seek(0, SeekOrigin.Begin);

                var url = UriHelper.GetDisplayUrl(context.Request);
                var requestBodyText = new StreamReader(requestBodyStream).ReadToEnd();
                _logger.LogDebug($"{session} {context.Request.Method} {url} {requestBodyText}");

                requestBodyStream.Seek(0, SeekOrigin.Begin);
                context.Request.Body = requestBodyStream;

                await _next(context);
                context.Request.Body = originalRequestBody;
            }
        }
        await _next(context);
    }

How can I log the requests and keep the request body for the controller after the middleware?

Perrier
  • 2,753
  • 5
  • 33
  • 53
  • 1
    Take a look to this question: https://stackoverflow.com/questions/44498802/asp-net-core-modify-substitute-a-request-body – jmunoa7 Sep 22 '17 at 10:30
  • @jmunoa7 Thanks, my problem was that I have left an else at the end of the method. – Perrier Sep 23 '17 at 13:21
  • In **ASP.NET Core 6.0 Preview** you seems to already are able to inject [HttpLogging](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-logging/?view=aspnetcore-6.0&viewFallbackFrom=aspnetcore-5.0). – Sviatoslav V. Aug 29 '21 at 17:19

1 Answers1

0

The solution to log incoming request was fine I just forgot an else at the end of the method. Working code:

public async Task Invoke(HttpContext context)
    {  
        StringValues sessionId;
        var session = string.Empty;
        if (context.Request.Headers.TryGetValue("X-SessionID", out sessionId))
        {
            session = sessionId.FirstOrDefault();
            if (session != null) 
            {
                var requestBodyStream = new MemoryStream();
                var originalRequestBody = context.Request.Body;

                await context.Request.Body.CopyToAsync(requestBodyStream);
                requestBodyStream.Seek(0, SeekOrigin.Begin);

                var url = UriHelper.GetDisplayUrl(context.Request);
                var requestBodyText = new StreamReader(requestBodyStream).ReadToEnd();
                _logger.LogInformation($"{session} {context.Request.Method} {url} {requestBodyText}");

                requestBodyStream.Seek(0, SeekOrigin.Begin);
                context.Request.Body = requestBodyStream;

                await _next.Invoke(context);
                context.Request.Body = originalRequestBody;
            }
        } else {
            await _next.Invoke(context);
        }
    }
Perrier
  • 2,753
  • 5
  • 33
  • 53