A common way to measure the response time of ASP.NET Core actions is by using a middleware component. As seen in Chapter 3, Working with the Middleware Pipeline, these components act at the edge of the ASP.NET Core request life cycle, and they are useful for performing cross-cutting implementations. Measuring the response time of an action method falls into this implementation case. Furthermore, middleware is the first component hit by a request and the last one that can process an outgoing response. This means that we can analyze and include almost the whole life cycle of the request.
Let's take a look at an implementation of ResponseTimeMiddlewareAsync:
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace Catalog.API.Infrastructure.Middleware
{
public class ResponseTimeMiddlewareAsync {...