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?

3
  • 1
    Take a look to this question: stackoverflow.com/questions/44498802/… Commented Sep 22, 2017 at 10:30
  • @jmunoa7 Thanks, my problem was that I have left an else at the end of the method. Commented Sep 23, 2017 at 13:21
  • In ASP.NET Core 6.0 Preview you seems to already are able to inject HttpLogging. Commented Aug 29, 2021 at 17:19

1 Answer 1

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);
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.