Skip to content

Easy way to log request and response bodies #13217

Closed
@Mikilll94

Description

@Mikilll94

Is your feature request related to a problem? Please describe.

I am trying to build a logging mechanism in my app which logs every request and response. There are many tutorials on how to do this. I think the most popular is:

https://exceptionnotfound.net/using-middleware-to-log-requests-and-responses-in-asp-net-core/

Unfortunately, I think that logging request and response bodies is too complicated because both request body and response body are Streams instead of strings. Just look at this code in this article. To achieve just simple logging we have to write a lot of difficult code. This also causes other problems which are related to Streams, i.e. Large Object Heap.

Many of these problems are described below this article in comments.

Describe the solution you'd like

I'd like to have an easy way to log request and response bodies in ASP.NET Core.
Solutions:

  1. Request and response bodies should be strings instead of streams (don't know if this is possible)
  2. Give a user some method to get request and response body as a string.

Additional context

Almost every app needs some way to log events happening in it. Middleware in ASP.NET Core seems the best place to include logging. Unfortunately, there is no official or approved solution on how to perform logging request and responses in ASP.NET Core.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions