Added structured logging to file and Grafana Loki

This commit is contained in:
2023-03-09 00:12:07 -06:00
parent 52273a8d12
commit 0ad6e99a8f
4 changed files with 73 additions and 5 deletions

View File

@ -0,0 +1,26 @@
using Serilog.Context;
using YABA.Common.Extensions;
namespace YABA.API.Middlewares
{
public class AddCustomLoggingPropertiesMiddleware
{
private readonly RequestDelegate _next;
public AddCustomLoggingPropertiesMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext httpContext)
{
if(httpContext.Request.Path.HasValue && httpContext.Request.Path.Value.Contains("/api"))
{
LogContext.PushProperty("UserId", httpContext.User.Identity.IsAuthenticated ? httpContext.User.Identity.GetUserId() : "Anonymous");
LogContext.PushProperty("RemoteIpAddress", httpContext.Connection.RemoteIpAddress.MapToIPv4());
}
await _next(httpContext);
}
}
}