ASP.NET Core 7 - how to to Set Up Custom Middleware for Request Logging with Serilog
After trying multiple solutions online, I still can't figure this out. Could someone explain I've been banging my head against this for hours..... I need some guidance on I've searched everywhere and can't find a clear answer. I'm trying to implement a custom middleware in my ASP.NET Core 7 application for logging incoming HTTP requests using Serilog. However, I'm working with an scenario where the logs are not being recorded as expected. My middleware is supposed to log the request's method and path, but it's not functioning correctly. I've set up Serilog in the `Program.cs` file as follows: ```csharp using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; var builder = WebApplication.CreateBuilder(args); Log.Logger = new LoggerConfiguration() .MinimumLevel.Information() .WriteTo.Console() .CreateLogger(); builder.Host.UseSerilog(); builder.Services.AddControllers(); var app = builder.Build(); app.UseMiddleware<RequestLoggingMiddleware>(); app.MapControllers(); app.Run(); ``` I created the middleware like this: ```csharp public class RequestLoggingMiddleware { private readonly RequestDelegate _next; public RequestLoggingMiddleware(RequestDelegate next) { _next = next; } public async Task InvokeAsync(HttpContext context) { Log.Information("Incoming request: {Method} {Path}", context.Request.Method, context.Request.Path); await _next(context); } } ``` I'm adding the middleware in the right order, but when I run the application and hit the endpoints, I don't see any logs in the console output. I've checked the Serilog configuration, and it seems to be properly set up. There are no exceptions being thrown, but the logging statements in my middleware don't appear to execute at all. I've also tried placing `Log.Information()` directly in the `Program.cs` to ensure that logging works independently: ```csharp Log.Information("Application starting..."); ``` This log appears correctly, indicating that Serilog is configured properly. Iβm wondering if thereβs something Iβm missing in the middleware setup or in the order of middleware execution. Any insights on what might be going wrong or how I can troubleshoot this scenario further? Has anyone else encountered this? The project is a desktop app built with C#. Is there a simpler solution I'm overlooking? The stack includes C# and several other technologies. Am I missing something obvious? I've been using C# for about a year now.