diff --git a/backend/DIAMConfiguration/Program.cs b/backend/DIAMConfiguration/Program.cs index a3ad0d5f..723b55b0 100644 --- a/backend/DIAMConfiguration/Program.cs +++ b/backend/DIAMConfiguration/Program.cs @@ -10,6 +10,14 @@ var builder = WebApplication.CreateBuilder(args); +var name = Assembly.GetExecutingAssembly().GetName(); +var outputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"; +var path = Environment.GetEnvironmentVariable("LogFilePath") ?? "logs"; + + + + + // Add services to the container. var dbConnection = builder.Configuration.GetValue("ConfigDatabase"); @@ -24,6 +32,12 @@ }) .EnableSensitiveDataLogging(sensitiveDataLoggingEnabled: false)); +builder.Host.UseSerilog((hostContext, services, configuration) => +{ + configuration.ReadFrom.Configuration(hostContext.Configuration); +}); + + builder.Services.AddHealthChecks() .AddCheck("liveness", () => HealthCheckResult.Healthy()) .AddNpgSql(dbConnection, tags: new[] { "services" }).ForwardToPrometheus(); @@ -61,7 +75,14 @@ app.MapHealthChecks("/health/liveness"); app.MapMetrics("/health/metrics"); - +app.UseSerilogRequestLogging(options => options.EnrichDiagnosticContext = (diagnosticContext, httpContext) => +{ + //var userId = httpContext.User.GetUserId(); + //if (!userId.Equals(Guid.Empty)) + //{ + // diagnosticContext.Set("User", userId); + //} +}); app.UseHttpsRedirection(); @@ -87,3 +108,5 @@ Log.Logger.Information("### Approval Flow Configuration complete"); app.Run(); + + diff --git a/backend/DIAMConfiguration/appsettings.json b/backend/DIAMConfiguration/appsettings.json index 607ac558..41d8bb70 100644 --- a/backend/DIAMConfiguration/appsettings.json +++ b/backend/DIAMConfiguration/appsettings.json @@ -6,5 +6,39 @@ } }, "AllowedHosts": "*", - "ConfigDatabase": "Host=fedora;Port=5444;Database=diam-config;Username=diamconfig;Password=P@55w0rd" + "Serilog": { + "MinimumLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + }, + "Enrich": [ + "FromLogContext" + ], + "Filter": [ + { + "Name": "ByExcluding", + "Args": { + "expression": "RequestPath like '/health%'" + } + } + ], + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "../logs/dev/dev.log", + "rollingInterval": "Day", + "outputTemplate": "{Level:w5} [{Timestamp:yyyy-MM-dd HH:mm:ss,fff}] >> {Message}{NewLine}{Exception}" + } + }, + { + "Name": "Console", + "Args": { + "outputTemplate": "{Level:w5} [{Timestamp:yyyy-MM-dd HyH:mm:ss,fff}] >> {Message}{NewLine}{Exception}" + } + } + ] + } }