Mar 8, 2021

3 min read

API Request and Response Logging Middleware using .NET 5 (C#)

What is middleware?

Middleware is an old IT concept that “sits” on top of several applications to handle requests and responses. Middleware is used when you have to handle situations that occur for every request/ response and should not be handled on the core backend site. .NET allows us to have multiples middlewares pieced together, which allows you as a developer to “plugin” your custom middleware.


Just create a new ASP.NET Core Web API or ASP.NET Core Web project that targets the .NET 5 framework. The example will also work with .NET Core 3.x

Creating the middleware

Create a new class called “HttpLoggingMiddleware” and copy over the following code

Final steps

Your middleware has to be configured in your Startup.cs.


In this article, we created a simple but powerful middleware that logs requests and responses that will work out-of-the-box in your application. Furthermore, it gives you a basic understanding of what you can further do with middleware in .NET.