I want to add a processing time middleware to my ASP.NET Core WebApi like this
public class ProcessingTimeMiddleware
{
private readonly RequestDelegate
Alternatively you can also add a middleware directly in the Startup.cs Configure method.
app.Use(
next =>
{
return async context =>
{
var stopWatch = new Stopwatch();
stopWatch.Start();
context.Response.OnStarting(
() =>
{
stopWatch.Stop();
context.Response.Headers.Add("X-ResponseTime-Ms", stopWatch.ElapsedMilliseconds.ToString());
return Task.CompletedTask;
});
await next(context);
};
});
app.UseMvc();