diff --git a/src/Microservices/Common/ClassifiedAds.Infrastructure/Web/Middleware/IApplicationBuilderExtensions.cs b/src/Microservices/Common/ClassifiedAds.Infrastructure/Web/Middleware/IApplicationBuilderExtensions.cs index 52f75c989..bf5798022 100644 --- a/src/Microservices/Common/ClassifiedAds.Infrastructure/Web/Middleware/IApplicationBuilderExtensions.cs +++ b/src/Microservices/Common/ClassifiedAds.Infrastructure/Web/Middleware/IApplicationBuilderExtensions.cs @@ -38,5 +38,11 @@ public static IApplicationBuilder UseGlobalExceptionHandlerMiddleware(this IAppl app.UseMiddleware(options); return app; } + + public static IApplicationBuilder UseLoggingStatusCodeMiddleware(this IApplicationBuilder app) + { + app.UseMiddleware(); + return app; + } } } diff --git a/src/Microservices/Common/ClassifiedAds.Infrastructure/Web/Middleware/LoggingStatusCodeMiddleware.cs b/src/Microservices/Common/ClassifiedAds.Infrastructure/Web/Middleware/LoggingStatusCodeMiddleware.cs new file mode 100644 index 000000000..820625860 --- /dev/null +++ b/src/Microservices/Common/ClassifiedAds.Infrastructure/Web/Middleware/LoggingStatusCodeMiddleware.cs @@ -0,0 +1,38 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using System.Linq; +using System.Security.Claims; +using System.Threading.Tasks; + +namespace ClassifiedAds.Infrastructure.Web.Middleware +{ + public class LoggingStatusCodeMiddleware + { + private readonly RequestDelegate _next; + private readonly ILogger _logger; + + public LoggingStatusCodeMiddleware(RequestDelegate next, ILogger logger) + { + _next = next; + _logger = logger; + } + + public async Task InvokeAsync(HttpContext context) + { + await _next(context); + + var statusCode = context.Response.StatusCode; + var path = context.Request.Path; + var method = context.Request.Method; + var userId = context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? context.User.FindFirst("sub")?.Value; + var remoteIp = context.Connection.RemoteIpAddress; + + var statusCodes = new[] { StatusCodes.Status401Unauthorized, StatusCodes.Status403Forbidden }; + + if (statusCodes.Contains(statusCode)) + { + _logger.LogWarning($"StatusCode: {statusCode}, UserId: {userId}, Path: {path}, Method: {method}, IP: {remoteIp}"); + } + } + } +} diff --git a/src/ModularMonolith/ClassifiedAds.Infrastructure/Web/Middleware/IApplicationBuilderExtensions.cs b/src/ModularMonolith/ClassifiedAds.Infrastructure/Web/Middleware/IApplicationBuilderExtensions.cs index 52f75c989..bf5798022 100644 --- a/src/ModularMonolith/ClassifiedAds.Infrastructure/Web/Middleware/IApplicationBuilderExtensions.cs +++ b/src/ModularMonolith/ClassifiedAds.Infrastructure/Web/Middleware/IApplicationBuilderExtensions.cs @@ -38,5 +38,11 @@ public static IApplicationBuilder UseGlobalExceptionHandlerMiddleware(this IAppl app.UseMiddleware(options); return app; } + + public static IApplicationBuilder UseLoggingStatusCodeMiddleware(this IApplicationBuilder app) + { + app.UseMiddleware(); + return app; + } } } diff --git a/src/ModularMonolith/ClassifiedAds.Infrastructure/Web/Middleware/LoggingStatusCodeMiddleware.cs b/src/ModularMonolith/ClassifiedAds.Infrastructure/Web/Middleware/LoggingStatusCodeMiddleware.cs new file mode 100644 index 000000000..820625860 --- /dev/null +++ b/src/ModularMonolith/ClassifiedAds.Infrastructure/Web/Middleware/LoggingStatusCodeMiddleware.cs @@ -0,0 +1,38 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using System.Linq; +using System.Security.Claims; +using System.Threading.Tasks; + +namespace ClassifiedAds.Infrastructure.Web.Middleware +{ + public class LoggingStatusCodeMiddleware + { + private readonly RequestDelegate _next; + private readonly ILogger _logger; + + public LoggingStatusCodeMiddleware(RequestDelegate next, ILogger logger) + { + _next = next; + _logger = logger; + } + + public async Task InvokeAsync(HttpContext context) + { + await _next(context); + + var statusCode = context.Response.StatusCode; + var path = context.Request.Path; + var method = context.Request.Method; + var userId = context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? context.User.FindFirst("sub")?.Value; + var remoteIp = context.Connection.RemoteIpAddress; + + var statusCodes = new[] { StatusCodes.Status401Unauthorized, StatusCodes.Status403Forbidden }; + + if (statusCodes.Contains(statusCode)) + { + _logger.LogWarning($"StatusCode: {statusCode}, UserId: {userId}, Path: {path}, Method: {method}, IP: {remoteIp}"); + } + } + } +} diff --git a/src/Monolith/ClassifiedAds.Infrastructure/Web/Middleware/IApplicationBuilderExtensions.cs b/src/Monolith/ClassifiedAds.Infrastructure/Web/Middleware/IApplicationBuilderExtensions.cs index 52f75c989..bf5798022 100644 --- a/src/Monolith/ClassifiedAds.Infrastructure/Web/Middleware/IApplicationBuilderExtensions.cs +++ b/src/Monolith/ClassifiedAds.Infrastructure/Web/Middleware/IApplicationBuilderExtensions.cs @@ -38,5 +38,11 @@ public static IApplicationBuilder UseGlobalExceptionHandlerMiddleware(this IAppl app.UseMiddleware(options); return app; } + + public static IApplicationBuilder UseLoggingStatusCodeMiddleware(this IApplicationBuilder app) + { + app.UseMiddleware(); + return app; + } } } diff --git a/src/Monolith/ClassifiedAds.Infrastructure/Web/Middleware/LoggingStatusCodeMiddleware.cs b/src/Monolith/ClassifiedAds.Infrastructure/Web/Middleware/LoggingStatusCodeMiddleware.cs new file mode 100644 index 000000000..820625860 --- /dev/null +++ b/src/Monolith/ClassifiedAds.Infrastructure/Web/Middleware/LoggingStatusCodeMiddleware.cs @@ -0,0 +1,38 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using System.Linq; +using System.Security.Claims; +using System.Threading.Tasks; + +namespace ClassifiedAds.Infrastructure.Web.Middleware +{ + public class LoggingStatusCodeMiddleware + { + private readonly RequestDelegate _next; + private readonly ILogger _logger; + + public LoggingStatusCodeMiddleware(RequestDelegate next, ILogger logger) + { + _next = next; + _logger = logger; + } + + public async Task InvokeAsync(HttpContext context) + { + await _next(context); + + var statusCode = context.Response.StatusCode; + var path = context.Request.Path; + var method = context.Request.Method; + var userId = context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? context.User.FindFirst("sub")?.Value; + var remoteIp = context.Connection.RemoteIpAddress; + + var statusCodes = new[] { StatusCodes.Status401Unauthorized, StatusCodes.Status403Forbidden }; + + if (statusCodes.Contains(statusCode)) + { + _logger.LogWarning($"StatusCode: {statusCode}, UserId: {userId}, Path: {path}, Method: {method}, IP: {remoteIp}"); + } + } + } +} diff --git a/src/Monolith/ClassifiedAds.WebAPI/Startup.cs b/src/Monolith/ClassifiedAds.WebAPI/Startup.cs index e9e55ff7c..c289349cf 100644 --- a/src/Monolith/ClassifiedAds.WebAPI/Startup.cs +++ b/src/Monolith/ClassifiedAds.WebAPI/Startup.cs @@ -216,6 +216,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseDebuggingMiddleware(); + app.UseLoggingStatusCodeMiddleware(); + if (env.IsDevelopment()) { app.UseDeveloperExceptionPage();