From 6745a7261911029066081db9190fe09825b51f21 Mon Sep 17 00:00:00 2001 From: AhmadAbuhussein Date: Sun, 11 Oct 2026 10:59:10 +0300 Subject: [PATCH] feat(elastic): optional configureLogger hook for extra Serilog sinks UseSerilog replaces the .NET logging pipeline, so a logging provider registered elsewhere (e.g. Sentry's) never receives an app's logs. The new optional configureLogger callback runs after the built-in sinks, so an app can add its own, such as Sentry. Existing callers are unchanged. --- README.md | 11 +++++++++++ SW.Logger.ElasticSearch/IHostBuilderExtensions.cs | 15 +++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 62062e3..b86296d 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,17 @@ public static IHostBuilder CreateHostBuilder(string[] args) => }); ``` +To add more Serilog sinks or enrichers, pass `configureLogger`; it runs after the built-in ones. Serilog replaces the .NET logging pipeline, so a logging provider such as Sentry's never receives these logs unless it is added here as a Serilog sink: + +```csharp +builder.Host.UseSwElasticSearchLogger(configureLogger: (context, loggerConfiguration) => + loggerConfiguration.WriteTo.Sentry(o => + { + o.InitializeSdk = false; // already initialized by UseSentry + o.MinimumEventLevel = LogEventLevel.Error; + })); +``` + #### 2. Configure Application Pipeline ```csharp diff --git a/SW.Logger.ElasticSearch/IHostBuilderExtensions.cs b/SW.Logger.ElasticSearch/IHostBuilderExtensions.cs index c7c5f17..7c040e4 100644 --- a/SW.Logger.ElasticSearch/IHostBuilderExtensions.cs +++ b/SW.Logger.ElasticSearch/IHostBuilderExtensions.cs @@ -22,7 +22,15 @@ namespace SW.Logger.ElasticSerach { public static class IHostBuilderExtensions { - public static IHostBuilder UseSwElasticSearchLogger(this IHostBuilder builder, Action configure = null) + /// Overrides the options read from the "SwLogger" configuration section. + /// + /// Runs after the built-in sinks are configured, to add more sinks or enrichers (for example + /// Sentry). UseSerilog replaces the .NET logging pipeline, so a logging provider registered + /// elsewhere never receives these logs; a Serilog sink added here does. + /// + public static IHostBuilder UseSwElasticSearchLogger(this IHostBuilder builder, + Action configure = null, + Action configureLogger = null) { var loggerOptions = new LoggerOptions { @@ -32,7 +40,10 @@ public static IHostBuilder UseSwElasticSearchLogger(this IHostBuilder builder, A if (configure != null) configure.Invoke(loggerOptions); return builder.UseSerilog((hostBuilderContext, loggerConfiguration) => - ConfigureSerilog(hostBuilderContext, loggerConfiguration, loggerOptions)); + { + ConfigureSerilog(hostBuilderContext, loggerConfiguration, loggerOptions); + configureLogger?.Invoke(hostBuilderContext, loggerConfiguration); + }); } private static LoggerConfiguration ConfigureSerilog(HostBuilderContext hostBuilderContext,