diff --git a/YABA.API/Middlewares/AddCustomLoggingPropertiesMiddleware.cs b/YABA.API/Middlewares/AddCustomLoggingPropertiesMiddleware.cs new file mode 100644 index 0000000..4681507 --- /dev/null +++ b/YABA.API/Middlewares/AddCustomLoggingPropertiesMiddleware.cs @@ -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); + } + } +} diff --git a/YABA.API/Program.cs b/YABA.API/Program.cs index bcfce83..aa78dfa 100644 --- a/YABA.API/Program.cs +++ b/YABA.API/Program.cs @@ -13,6 +13,7 @@ using YABA.Data.Configuration; using YABA.Data.Context; using YABA.Service.Configuration; using Serilog; +using Microsoft.AspNetCore.HttpOverrides; var builder = WebApplication.CreateBuilder(args); var configuration = builder.Configuration; @@ -49,6 +50,7 @@ builder.Services.AddHttpContextAccessor(); builder.Services.AddServiceProjectDependencyInjectionConfiguration(configuration); builder.Services.AddDataProjectDependencyInjectionConfiguration(configuration); builder.Services.AddControllers().AddNewtonsoftJson(); +builder.Services.AddHealthChecks(); // Add AutoMapper profiles var mapperConfiguration = new MapperConfiguration(mapperConfiguration => @@ -78,8 +80,14 @@ builder.Services.AddSwaggerGen( } ); +builder.Services.Configure(options => +{ + options.ForwardedHeaders = + ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; +}); + // Add Serilog -Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).CreateLogger(); +Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).Enrich.FromLogContext().CreateLogger(); builder.Host.UseSerilog(); var app = builder.Build(); @@ -100,6 +108,7 @@ if (app.Environment.IsDevelopment()) app.UseHttpsRedirection(); +app.UseForwardedHeaders(); app.UseAuthentication(); app.UseAuthorization(); @@ -107,6 +116,7 @@ app.MapControllers(); // Add custom middlewares app.UseMiddleware(); +app.UseMiddleware(); app.UseCors(x => x .AllowAnyOrigin() @@ -115,5 +125,6 @@ app.UseCors(x => x var webClientUrl = configuration.GetSection("WebClient").GetValue("Url"); app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().WithOrigins(webClientUrl)); +app.MapHealthChecks("/Pulse"); app.Run(); diff --git a/YABA.API/YABA.API.csproj b/YABA.API/YABA.API.csproj index 6ef1e72..0fb4de1 100644 --- a/YABA.API/YABA.API.csproj +++ b/YABA.API/YABA.API.csproj @@ -11,6 +11,7 @@ + @@ -20,8 +21,12 @@ + + + + @@ -33,4 +38,8 @@ + + + + diff --git a/YABA.API/appsettings.json b/YABA.API/appsettings.json index 97d4479..136f450 100644 --- a/YABA.API/appsettings.json +++ b/YABA.API/appsettings.json @@ -7,16 +7,38 @@ } }, "Serilog": { - "MinimumLevel": "Information", + "MinimumLevel": "Information", + "Using": [ + "Serilog.Sinks.Grafana.Loki" + ], "WriteTo": [ { "Name": "File", "Args": { - "path": "Logs/log-.txt", - "rollingInterval": "Day" + "path": "Logs/log.json", + "rollingInterval": "Day", + "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog" + } + }, + { + "Name": "GrafanaLoki", + "Args": { + "uri": "https://loki.iwanaga.moe", + "labels": [ + { + "key": "job", + "value": "YABA.API" + }, + { + "key": "environment", + "value": "localhost" + } + ], + "propertiesAsLabels": [ "job", "environment" ] } } - ] + ], + "Enrich": ["FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId"] }, "WebClient": { "Url": "https://localhost:3000"