-
Notifications
You must be signed in to change notification settings - Fork 558
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de6fe6a
commit f753eb8
Showing
7 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...ervices/Common/ClassifiedAds.Infrastructure/Web/Middleware/LoggingStatusCodeMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...odularMonolith/ClassifiedAds.Infrastructure/Web/Middleware/LoggingStatusCodeMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/Monolith/ClassifiedAds.Infrastructure/Web/Middleware/LoggingStatusCodeMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters