diff --git a/src/Hosting/Conventions/SerilogHostingConvention.cs b/src/Hosting/Conventions/SerilogHostingConvention.cs index 41dd0c752..045119d36 100644 --- a/src/Hosting/Conventions/SerilogHostingConvention.cs +++ b/src/Hosting/Conventions/SerilogHostingConvention.cs @@ -3,9 +3,13 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Rocket.Surgery.Conventions; +using Rocket.Surgery.Conventions.Hosting; using Rocket.Surgery.Hosting; -using Rocket.Surgery.LaunchPad.Serilog; using Serilog; +using Serilog.Core; +using Serilog.Events; +using Serilog.Extensions.Hosting; +using Serilog.Extensions.Logging; using ILogger = Serilog.ILogger; namespace Rocket.Surgery.LaunchPad.Hosting.Conventions; @@ -17,17 +21,33 @@ namespace Rocket.Surgery.LaunchPad.Hosting.Conventions; /// [PublicAPI] [ExportConvention] -public class SerilogHostingConvention : IHostApplicationConvention +public class SerilogHostingConvention : IHostApplicationConvention, IHostCreatedConvention { - private readonly LaunchPadLoggingOptions _options; - - /// - /// Initializes a new instance of the class. - /// - /// The options. - public SerilogHostingConvention(LaunchPadLoggingOptions? options = null) + private void CustomAddSerilog( + IServiceCollection collection, + Action configureLogger + ) { - _options = options ?? new LaunchPadLoggingOptions(); + collection.AddSingleton(new LoggerProviderCollection()); + collection.AddSingleton( + services => + { + var loggerConfiguration = new LoggerConfiguration(); + loggerConfiguration.WriteTo.Providers(services.GetRequiredService()); + configureLogger(services, loggerConfiguration); + return loggerConfiguration.CreateLogger(); + } + ); + collection.AddSingleton(services => services.GetRequiredService().ForContext(new NullEnricher())); + collection.AddSingleton( + services => new SerilogLoggerFactory( + services.GetRequiredService(), + true, + services.GetRequiredService() + ) + ); + collection.AddSingleton(services => new DiagnosticContext(services.GetRequiredService())); + collection.AddSingleton(services => services.GetRequiredService()); } /// @@ -58,30 +78,35 @@ public void Register(IConventionContext context, IHostApplicationBuilder builder } else { - builder.Services.AddSerilog( - (services, loggerConfiguration) => loggerConfiguration.ApplyConventions(context, builder.Configuration, services), - _options.PreserveStaticLogger, - _options.WriteToProviders + CustomAddSerilog( + builder.Services, + (services, loggerConfiguration) => loggerConfiguration.ApplyConventions(context, builder.Configuration, services) ); } if (context.Get() != null) // ReSharper disable once NullableWarningSuppressionIsUsed builder.Services.AddSingleton(context.Get()!); + } - if (_options.WriteToProviders) return; + /// + public void Register(IConventionContext context, IHost host) + { + host + .Services + .GetServices() + .Aggregate( + host.Services.GetRequiredService(), + (factory, loggerProvider) => + { + factory.AddProvider(loggerProvider); + return factory; + } + ); + } - builder.OnHostStarting( - provider => provider - .GetServices() - .Aggregate( - provider.GetRequiredService(), - (factory, loggerProvider) => - { - factory.AddProvider(loggerProvider); - return factory; - } - ) - ); + private class NullEnricher : ILogEventEnricher + { + public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { } } } \ No newline at end of file diff --git a/src/Serilog/LaunchPadLoggingOptions.cs b/src/Serilog/LaunchPadLoggingOptions.cs index c8f19bece..572383a94 100644 --- a/src/Serilog/LaunchPadLoggingOptions.cs +++ b/src/Serilog/LaunchPadLoggingOptions.cs @@ -27,14 +27,4 @@ public class LaunchPadLoggingOptions /// Enable or disable debug logging, defaults to enabled /// public bool EnableDebugLogging { get; set; } = true; - - /// - /// Base option from the serilog package - /// - public bool WriteToProviders { get; set; } - - /// - /// Base option from the serilog package - /// - public bool PreserveStaticLogger { get; set; } } \ No newline at end of file