NET5 Request-Response Logging

Salih Cantekin
3 min readJan 16, 2022

For one of the projects that I work on, performance was always the most important issue. Not only how fast it is, but also detailed data was important to report. What endpoints are called the most, how long it took, how many exceptions we got etc? Beyond reporting this data, preparing and calculating these metrics were also problems with no downtime on the system. I figured out the solution was using a Middleware to prepare these data and using an async centralized logging system to send the data to. A custom middleware and Serilog were my helpers in this case. The middleware part had the highest importance as it can cause problems if I made a mistake whilst developing it.

Years passed on my first Middleware experience and not I know how to collect the data. In this article, you will see how to use a NuGet package to handle this middleware situation for you, which is also developed by me.

As it can also be seen on its GitHub page, this middleware is added by using an extension method called ‘AddTBRequestResponseMiddleware’ of ‘IApplicationBuilder’. Let’s dive into this usage and its features.

By adding the TechBuddy.Middlewares.RequestResponse package you are able to use this Middleware Extension Method.

Install-Package TechBuddy.Middlewares.RequestResponse -Version 1.0.0
UseHandler

By using UseHandler method, you can easily access the properties in the context. This method is fired just before the request is completed by the middleware which collects all the information to serve you. There are several properties you might need in this context. You may use this data to do whatever you want to.

If you just want to log all the data, you can simply use UseLogger the method. By using this way, you basically tell the middleware that it can use the logging features that your system already has. Let’s say you use console logging. That would mean, your system will send all the produced logs to the console. In this case, when you use UseLogger and give it a default log provider of your system, the request and response logs would directly go to your console. You are also able to add more than one built-in place to write the logs to such as Debug, EventLog or even custom logger libraries such as Serilog.

Adding Logs
UseLogger

Once you choose to use your LoggerFactoryto write the logs to, you might want to customize the log output. If you use a logging system that supports Context Feature as Serilog does, you may want to set UseSeparateContext property to true. This will send the logs through the LoggerFactory by using LogContext. See an example for Serilog

On the other hand, you can also customize the output properties by adding what information you want to see on. To do that, you can add the properties you want to the LoggingFields list. LogingLevel property is used to send the logs to the providers by using this level. Let's say you customized your Console Logging by only showing warning and error message by setting the LogLevel to 'Warning'. In this case, your request and response logs wouldn't appear on the console unless you set the LoggingLevel to LogLevel.Warning. So you are able to customize the log level that middleware uses while sending the logs to the providers. The LoggerCategoryName is used to create the logger by giving it a name.

PS: When you use both UseHandler and UseLogger, the calling order is the Logger is ran first and then the Handler is fired.

Please don’t hesitate to contribute this package on the GitHub page if you see any bug, must-have or nice-to-have features.

Enjoy using this library :)

--

--