In this section, we will create our own middleware for our existing application. In this middleware, we will log all requests and responses. Let's go through the following steps:
- Open Visual Studio.
- Open an existing project of the Product APIs by clicking File | Open | Project/Solution (or pressing Ctrl + Shift + O), as shown in the following screenshot:
- Locate your solution folder and click Open, as shown in the following screenshot:
- Open the solution explorer, add a new folder, and name it Middleware by right-clicking on the project name, as shown in the following screenshot:
- Right-click on the Middleware folder and select Add | New Item.
- From the web templates, select the Middleware Class and name the new file FlixOneStoreLoggerMiddleware. Then, click Add, as shown in the following screenshot:
Your folder hierarchy should be like the one shown in the following screenshot:
Look at the following code snippet of our FlixOneStoreLoggerMiddleware class:
private readonly RequestDelegate _next;
private readonly ILogger<FlixOneLoggerMiddleware> _logger;
public FlixOneLoggerMiddleware(RequestDelegate next, ILogger<FlixOneLoggerMiddleware> logger)
{
_next = next;
_logger = logger;
}
In the preceding code, we are simply taking advantage of the inbuilt DI using the RequestDelegate to create our custom middleware.
The following code shows us how we should be wiring up all requests and responses for the log:
public async Task Invoke(HttpContext httpContext)
{
_logger.LogInformation(await
GetFormatedRequest(httpContext.Request));
var originalBodyStream = httpContext.Response.Body;
using (var responseBody = new MemoryStream())
{
httpContext.Response.Body = responseBody;
await _next(httpContext);
_logger.LogInformation(await
GetFormatedResponse(httpContext.Response));
await responseBody.CopyToAsync(originalBodyStream);
}
}
Refer to the Request delegate section in this chapter, where we looked at middleware. In the preceding code, we are simply logging the request and response with the help of the ILogger generic type. The await _next(httpContext); line continues with the request pipeline.
Open the Setup.cs file and add the following code in the Configure method:
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
//custom middleware
app.UseFlixOneLoggerMiddleware();
In the preceding code, we take advantage of ILoggerFactory and add Console and Debug to log the requests and responses. The UseFlixOneLoggerMiddleware method is actually an extension method. For this, add the following code to the FlixOneStoreLoggerExtension class:
public static class FlixOneStoreLoggerExtension
{
public static IApplicationBuilder UseFlixOneLoggerMiddleware
(this IApplicationBuilder applicationBuilder)
{
return applicationBuilder.UseMiddleware<FlixOneLoggerMiddleware>();
}
}
Now, whenever any request comes to our product APIs, the log should appear, as shown in the following screenshot:
In this section, we created a custom middleware and then logged all requests and responses.