From 4bf71a0b2c193404320f1f78cc5725c2c899c012 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Mon, 1 Jun 2026 08:03:49 -0400 Subject: [PATCH] refactor(logging): adopt ZB.MOM.WW.Telemetry.Serilog bootstrap Swap the gateway process logging from the default Microsoft.Extensions.Logging provider onto the shared ZB.MOM.WW.Telemetry.Serilog two-stage bootstrap. - Add a cross-repo ProjectReference to ZB.MOM.WW.Telemetry.Serilog (transitively brings the Telemetry core package); the referenced project resolves its own Directory.Build.props / Directory.Packages.props so it does not perturb this build. - Replace MEL wiring in GatewayApplication with builder.AddZbSerilog(ServiceName= "mxgateway"; SiteId/NodeRole read from MxGateway:Telemetry when present) and add app.UseSerilogRequestLogging(). - Add a Serilog section (Console + daily rolling File sinks, MinimumLevel) to appsettings.json and a MinimumLevel override to appsettings.Development.json, replacing the old MEL Logging sections. The net48/x86 worker is untouched. Correlation scope + redaction move to the shared ILogRedactor seam in the follow-up commit. --- .../GatewayApplication.cs | 27 +++++++++++++++++ .../ZB.MOM.WW.MxGateway.Server.csproj | 7 +++++ .../appsettings.Development.json | 8 +++-- .../appsettings.json | 29 ++++++++++++++++--- 4 files changed, 64 insertions(+), 7 deletions(-) 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": {