Skip to content

Commit d8a124d

Browse files
committed
Add ProfilingMiddleware and update Swagger setup
Added a new ProfilingMiddleware to log request durations in development. Updated Program.cs to include the middleware conditionally and reordered middleware for proper execution. Revised Swagger setup with a custom endpoint and cleaned up redundant code. Added necessary using directive for the new middleware.
1 parent 6c2a7fd commit d8a124d

2 files changed

Lines changed: 37 additions & 9 deletions

File tree

Middlewares/ProfilingMiddleware.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Diagnostics;
2+
3+
namespace AskAMuslimAPI.Middlewares
4+
{
5+
public class ProfilingMiddleware
6+
{
7+
private readonly RequestDelegate _next;
8+
private readonly ILogger<ProfilingMiddleware> _logger;
9+
10+
public ProfilingMiddleware(RequestDelegate next, ILogger<ProfilingMiddleware> logger)
11+
{
12+
_next = next;
13+
_logger = logger;
14+
}
15+
16+
public async Task InvokeAsync(HttpContext context)
17+
{
18+
var stopwatch = new Stopwatch();
19+
stopwatch.Start();
20+
21+
await _next(context);
22+
23+
stopwatch.Stop();
24+
_logger.LogInformation($"Request {context.Request.Path} took {stopwatch.ElapsedMilliseconds} ms");
25+
}
26+
}
27+
}

Program.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using AskAMuslimAPI.Configurations;
2+
using AskAMuslimAPI.Middlewares;
23
using AskAMuslimAPI.Services;
34
using AskAMuslimAPI.Services.Interfaces;
45
using EdufyAPI.DTOs;
@@ -188,25 +189,25 @@
188189
#region Middleware Configuration
189190
// 🔹 Configure middleware
190191

191-
//if (app.Environment.IsDevelopment())
192-
//{
192+
193+
// Swagger
193194
app.UseSwagger();
195+
app.UseSwaggerUI();
196+
194197
app.UseSwaggerUI(c =>
195198
{
196199
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Ask A Muslim API V1");
197200
});
198-
//}
199201

200-
//if (app.Environment.IsDevelopment())
201-
//{
202-
app.UseSwagger();
203-
app.UseSwaggerUI();
204-
//}
205202

206-
app.UseHttpsRedirection();
203+
204+
205+
if (app.Environment.IsDevelopment())
206+
app.UseMiddleware<ProfilingMiddleware>();
207207

208208
app.UseCors("AllowAll");
209209

210+
app.UseHttpsRedirection();
210211
// 🔹 Enable Authentication & Authorization middleware
211212
app.UseAuthentication(); // Must come before Authorization
212213
app.UseAuthorization();

0 commit comments

Comments
 (0)