diff --git a/src/ZB.MOM.WW.MxGateway.Server/GatewayApplication.cs b/src/ZB.MOM.WW.MxGateway.Server/GatewayApplication.cs index bd88648..90e3f61 100644 --- a/src/ZB.MOM.WW.MxGateway.Server/GatewayApplication.cs +++ b/src/ZB.MOM.WW.MxGateway.Server/GatewayApplication.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Hosting.StaticWebAssets; +using Serilog; using ZB.MOM.WW.MxGateway.Contracts; using ZB.MOM.WW.MxGateway.Server.Alarms; using ZB.MOM.WW.MxGateway.Server.Configuration; @@ -11,6 +12,7 @@ using ZB.MOM.WW.MxGateway.Server.Security.Authentication; using ZB.MOM.WW.MxGateway.Server.Security.Authorization; using ZB.MOM.WW.MxGateway.Server.Sessions; using ZB.MOM.WW.MxGateway.Server.Workers; +using ZB.MOM.WW.Telemetry.Serilog; namespace ZB.MOM.WW.MxGateway.Server; @@ -31,7 +33,10 @@ public static class GatewayApplication WebApplicationBuilder builder = CreateBuilder(args); WebApplication app = builder.Build(); + // Push the per-request correlation properties (via Serilog LogContext) before the + // request-logging middleware emits its completion event, so those properties appear on it. app.UseGatewayRequestLoggingScope(); + app.UseSerilogRequestLogging(); app.UseStaticFiles(); app.UseAuthentication(); app.UseAuthorization(); @@ -55,6 +60,8 @@ public static class GatewayApplication }); StaticWebAssetsLoader.UseStaticWebAssets(builder.Environment, builder.Configuration); + ConfigureSerilog(builder); + builder.Services.AddGatewayConfiguration(); builder.Services.AddSqliteAuthStore(); builder.Services.AddGatewayGrpcAuthorization(); @@ -72,6 +79,26 @@ public static class GatewayApplication return builder; } + /// + /// Replaces the default Microsoft.Extensions.Logging provider with the shared + /// ZB.MOM.WW.Telemetry.Serilog bootstrap (). + /// Sinks and minimum level come from the Serilog configuration section; identity + /// (SiteId/NodeRole) is read from MxGateway:Telemetry when present. + /// + /// The web application builder being configured. + private static void ConfigureSerilog(WebApplicationBuilder builder) + { + string? siteId = builder.Configuration["MxGateway:Telemetry:SiteId"]; + string? nodeRole = builder.Configuration["MxGateway:Telemetry:NodeRole"]; + + builder.AddZbSerilog(options => + { + options.ServiceName = "mxgateway"; + options.SiteId = string.IsNullOrWhiteSpace(siteId) ? null : siteId; + options.NodeRole = string.IsNullOrWhiteSpace(nodeRole) ? null : nodeRole; + }); + } + private static string ResolveContentRootPath() { string? configuredContentRootPath = Environment.GetEnvironmentVariable("ASPNETCORE_CONTENTROOT"); diff --git a/src/ZB.MOM.WW.MxGateway.Server/ZB.MOM.WW.MxGateway.Server.csproj b/src/ZB.MOM.WW.MxGateway.Server/ZB.MOM.WW.MxGateway.Server.csproj index bbce9d8..09e7993 100644 --- a/src/ZB.MOM.WW.MxGateway.Server/ZB.MOM.WW.MxGateway.Server.csproj +++ b/src/ZB.MOM.WW.MxGateway.Server/ZB.MOM.WW.MxGateway.Server.csproj @@ -15,6 +15,13 @@ + + diff --git a/src/ZB.MOM.WW.MxGateway.Server/appsettings.Development.json b/src/ZB.MOM.WW.MxGateway.Server/appsettings.Development.json index 0c208ae..2e88f5b 100644 --- a/src/ZB.MOM.WW.MxGateway.Server/appsettings.Development.json +++ b/src/ZB.MOM.WW.MxGateway.Server/appsettings.Development.json @@ -1,8 +1,10 @@ { - "Logging": { - "LogLevel": { + "Serilog": { + "MinimumLevel": { "Default": "Information", - "Microsoft.AspNetCore": "Warning" + "Override": { + "Microsoft.AspNetCore": "Warning" + } } } } diff --git a/src/ZB.MOM.WW.MxGateway.Server/appsettings.json b/src/ZB.MOM.WW.MxGateway.Server/appsettings.json index 63928c7..df07779 100644 --- a/src/ZB.MOM.WW.MxGateway.Server/appsettings.json +++ b/src/ZB.MOM.WW.MxGateway.Server/appsettings.json @@ -1,9 +1,30 @@ { - "Logging": { - "LogLevel": { + "Serilog": { + "Using": [ + "Serilog.Sinks.Console", + "Serilog.Sinks.File" + ], + "MinimumLevel": { "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } + "Override": { + "Microsoft.AspNetCore": "Warning" + } + }, + "WriteTo": [ + { + "Name": "Console", + "Args": { + "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] [{NodeRole}/{NodeHostname}] {Message:lj} {Properties:j}{NewLine}{Exception}" + } + }, + { + "Name": "File", + "Args": { + "path": "logs/mxgateway-.log", + "rollingInterval": "Day" + } + } + ] }, "AllowedHosts": "*", "MxGateway": {