简体   繁体   中英

Configure .net core logging to log request and response

I'm developing asp .net core 2.1 WEB-API application.

I use ILogger with configuration:

"Logging": {
"LogLevel": {
  "Default": "Debug",
  "System": "Information",
  "Microsoft": "Information"
}

And on request i see log:

info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1] Executing action method ActivationService.Controllers.ActivationController.Post (ActivationService) with arguments (ActivationService.Contracts.ActivationRequest) - Validation state: Valid

and

Executed action method ActivationService.Controllers.ActivationController.Post (ActivationService), returned result Microsoft.AspNetCore.Mvc.ObjectResult in 174605.9201ms.

Is there way to confugure asp.net to log with trace body of response and request?

Yes, you can implement logging middleware:

public class RequestResponseLoggingMiddleware
    {
        private readonly RequestDelegate next;
        private readonly ILogger logger;

        public RequestResponseLoggingMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
        {
            this.next = next;
            logger = loggerFactory.CreateLogger<RequestResponseLoggingMiddleware>();
        }

        public async Task Invoke(HttpContext context)
        {
            context.Request.EnableRewind();

            var buffer = new byte[Convert.ToInt32(context.Request.ContentLength)];
            await context.Request.Body.ReadAsync(buffer, 0, buffer.Length);
            var requestBody = Encoding.UTF8.GetString(buffer);
            context.Request.Body.Seek(0, SeekOrigin.Begin);

            logger.LogInformation(requestBody);

            var originalBodyStream = context.Response.Body;

            using (var responseBody = new MemoryStream())
            {
                context.Response.Body = responseBody;

                await next(context);

                context.Response.Body.Seek(0, SeekOrigin.Begin);
                var response = await new StreamReader(context.Response.Body).ReadToEndAsync();
                context.Response.Body.Seek(0, SeekOrigin.Begin);

                logger.LogInformation(response);
                await responseBody.CopyToAsync(originalBodyStream);
            }
        }
    }

And then add it to application Builder in Configure method:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    ....
    app.UseMiddleware<RequestResponseLoggingMiddleware>();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM