Skip to content

Commit

Permalink
Logging Status Code Middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Nov 24, 2021
1 parent de6fe6a commit f753eb8
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,11 @@ public static IApplicationBuilder UseGlobalExceptionHandlerMiddleware(this IAppl
app.UseMiddleware<GlobalExceptionHandlerMiddleware>(options);
return app;
}

public static IApplicationBuilder UseLoggingStatusCodeMiddleware(this IApplicationBuilder app)
{
app.UseMiddleware<LoggingStatusCodeMiddleware>();
return app;
}
}
}
Original file line number Diff line number Diff line change
@@ -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<LoggingStatusCodeMiddleware> _logger;

public LoggingStatusCodeMiddleware(RequestDelegate next, ILogger<LoggingStatusCodeMiddleware> 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}");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,11 @@ public static IApplicationBuilder UseGlobalExceptionHandlerMiddleware(this IAppl
app.UseMiddleware<GlobalExceptionHandlerMiddleware>(options);
return app;
}

public static IApplicationBuilder UseLoggingStatusCodeMiddleware(this IApplicationBuilder app)
{
app.UseMiddleware<LoggingStatusCodeMiddleware>();
return app;
}
}
}
Original file line number Diff line number Diff line change
@@ -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<LoggingStatusCodeMiddleware> _logger;

public LoggingStatusCodeMiddleware(RequestDelegate next, ILogger<LoggingStatusCodeMiddleware> 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}");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,11 @@ public static IApplicationBuilder UseGlobalExceptionHandlerMiddleware(this IAppl
app.UseMiddleware<GlobalExceptionHandlerMiddleware>(options);
return app;
}

public static IApplicationBuilder UseLoggingStatusCodeMiddleware(this IApplicationBuilder app)
{
app.UseMiddleware<LoggingStatusCodeMiddleware>();
return app;
}
}
}
Original file line number Diff line number Diff line change
@@ -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<LoggingStatusCodeMiddleware> _logger;

public LoggingStatusCodeMiddleware(RequestDelegate next, ILogger<LoggingStatusCodeMiddleware> 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}");
}
}
}
}
2 changes: 2 additions & 0 deletions src/Monolith/ClassifiedAds.WebAPI/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseDebuggingMiddleware();

app.UseLoggingStatusCodeMiddleware();

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
Expand Down

0 comments on commit f753eb8

Please sign in to comment.