trenoncourt / serilog-enrichers-aspnetcore-httpcontext

Enriches Serilog events with Aspnetcore HttpContext.
MIT License
38 stars 17 forks source link

Capturing the response #1

Closed Ricky-G closed 6 years ago

Ricky-G commented 6 years ago

Right now the enricher captures the request in the property "body" which is the request body how about adding the response body as well? any thoughts?

context.Response.OnStarting(state => { var httpContext = (HttpContext)state; //something like the below capture and store the body // httpContext.Response.Body return Task.CompletedTask; }, context);

trenoncourt commented 6 years ago

Hi @Ricky-G ,

We cannot enrich the logs with the repsponse body because at the moment the log is written, the body is not yet rendered.

I will add a second repo (serilog-aspnetcore-middleware) that will work differently. It will add a log at the end of each request with all the necessary information.

Ricky-G commented 6 years ago

@trenoncourt I've written something like that, its a middleware that gets run at the start, but the response is always coming out as weird characters. Would be keen to take a look at your implementation of the middleware, did you manage to get the response body logging?

trenoncourt commented 6 years ago

@Ricky-G ,

Here is the middleware repository: https://github.com/trenoncourt/serilog-aspnetcore-middleware

The response body seems to be well treated as you can see in the sample picture.