From beac6afc9cd4cb24f98e285390a8ceecc40d2f90 Mon Sep 17 00:00:00 2001 From: George Drak Date: Wed, 18 Oct 2023 18:44:35 +0500 Subject: [PATCH] feat(app): hook into host build event via custom di factory --- src/Sitko.Core.App/HostedLifecycleService.cs | 8 ++- .../SitkoCoreBaseApplicationBuilder.cs | 49 ++++++++++++------- .../SitkoCoreServerApplicationBuilder.cs | 30 ++++++++++-- .../WebAssemblyHostBuilderExtensions.cs | 4 +- 4 files changed, 65 insertions(+), 26 deletions(-) diff --git a/src/Sitko.Core.App/HostedLifecycleService.cs b/src/Sitko.Core.App/HostedLifecycleService.cs index 2483cd86f..2445f5aee 100644 --- a/src/Sitko.Core.App/HostedLifecycleService.cs +++ b/src/Sitko.Core.App/HostedLifecycleService.cs @@ -11,18 +11,16 @@ internal class HostedLifecycleService : IHostedLifecycleService private readonly ILogger logger; private readonly IApplicationContext applicationContext; private readonly IServiceProvider serviceProvider; - private readonly SerilogConfigurator serilogConfigurator; + private readonly IReadOnlyList enabledModules; public HostedLifecycleService(ILogger logger, IApplicationContext applicationContext, - IServiceProvider serviceProvider, IEnumerable applicationModuleRegistrations, - SerilogConfigurator serilogConfigurator) + IServiceProvider serviceProvider, IEnumerable applicationModuleRegistrations) { this.logger = logger; this.applicationContext = applicationContext; this.serviceProvider = serviceProvider; - this.serilogConfigurator = serilogConfigurator; enabledModules = ModulesHelper.GetEnabledModuleRegistrations(applicationContext, applicationModuleRegistrations); } @@ -34,7 +32,7 @@ public HostedLifecycleService(ILogger logger, IApplicati public async Task StartingAsync(CancellationToken cancellationToken) { await using var scope = serviceProvider.CreateAsyncScope(); - serilogConfigurator.ApplyLogging(applicationContext, enabledModules); + foreach (var enabledModule in enabledModules) { var shouldContinue = await enabledModule.GetInstance() diff --git a/src/Sitko.Core.App/SitkoCoreBaseApplicationBuilder.cs b/src/Sitko.Core.App/SitkoCoreBaseApplicationBuilder.cs index 448395779..2dfdb61b2 100644 --- a/src/Sitko.Core.App/SitkoCoreBaseApplicationBuilder.cs +++ b/src/Sitko.Core.App/SitkoCoreBaseApplicationBuilder.cs @@ -31,29 +31,15 @@ protected SitkoCoreBaseApplicationBuilder(string[] args, IServiceCollection serv // configure logging Configuration.Add(new SerilogDynamicConfigurationSource()); - var tmpLoggerConfiguration = new LoggerConfiguration(); - tmpLoggerConfiguration = ConfigureDefautLogger(tmpLoggerConfiguration); - if (BootApplicationContext.Options.EnableConsoleLogging != true) - { - tmpLoggerConfiguration = tmpLoggerConfiguration.WriteTo.Console( - outputTemplate: ApplicationOptions.BaseConsoleLogFormat, - formatProvider: CultureInfo.InvariantCulture, - restrictedToMinimumLevel: LogEventLevel.Debug); - } - - var tmpLogger = tmpLoggerConfiguration.CreateLogger(); - Log.Logger = tmpLogger; // set default logger until host is started - Console.OutputEncoding = Encoding.UTF8; - InternalLogger = new SerilogLoggerFactory(tmpLogger) - .CreateLogger(); - AddModule(); + InternalLogger = CreateInternalLogger(); Logging.ClearProviders(); Logging.AddSerilog(); serilogConfigurator.Configure(ConfigureDefautLogger); + AddModule(); + Services.AddSingleton(argsProvider); Services.AddSingleton(environment); - Services.AddSingleton(serilogConfigurator); Services.AddSingleton(); Services.AddTransient(); Services.AddFluentValidationExtensions(); @@ -117,6 +103,25 @@ public ISitkoCoreApplicationBuilder AddModule( public bool HasModule() where TModule : IApplicationModule => moduleRegistrations.Any(r => r.Type == typeof(TModule)); + private ILogger CreateInternalLogger() + { + var tmpLoggerConfiguration = new LoggerConfiguration(); + tmpLoggerConfiguration = ConfigureDefautLogger(tmpLoggerConfiguration); + if (BootApplicationContext.Options.EnableConsoleLogging != true) + { + tmpLoggerConfiguration = tmpLoggerConfiguration.WriteTo.Console( + outputTemplate: ApplicationOptions.BaseConsoleLogFormat, + formatProvider: CultureInfo.InvariantCulture, + restrictedToMinimumLevel: LogEventLevel.Debug); + } + + var tmpLogger = tmpLoggerConfiguration.CreateLogger(); + Log.Logger = tmpLogger; // set default logger until host is started + Console.OutputEncoding = Encoding.UTF8; + return new SerilogLoggerFactory(tmpLogger) + .CreateLogger(); + } + private void RegisterModule( Action? configureOptions = null, string? optionsKey = null) @@ -176,5 +181,15 @@ protected virtual LoggerConfiguration ConfigureDefautLogger(LoggerConfiguration return loggerConfiguration; } + protected virtual void BeforeContainerBuild() + { + var enabledModules = ModulesHelper.GetEnabledModuleRegistrations(BootApplicationContext, moduleRegistrations); + serilogConfigurator.ApplyLogging(BootApplicationContext, enabledModules); + foreach (var moduleRegistration in enabledModules) + { + moduleRegistration.PostConfigureServices(BootApplicationContext, Services); + } + } + protected void LogInternal(string message) => InternalLogger.LogInformation("Check log: {Message}", message); } diff --git a/src/Sitko.Core.App/SitkoCoreServerApplicationBuilder.cs b/src/Sitko.Core.App/SitkoCoreServerApplicationBuilder.cs index 35b5fd74e..7fd0663e6 100644 --- a/src/Sitko.Core.App/SitkoCoreServerApplicationBuilder.cs +++ b/src/Sitko.Core.App/SitkoCoreServerApplicationBuilder.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; namespace Sitko.Core.App; @@ -7,9 +8,15 @@ public class SitkoCoreServerApplicationBuilder : SitkoCoreBaseApplicationBuilder private readonly IHostApplicationBuilder applicationBuilder; public SitkoCoreServerApplicationBuilder(IHostApplicationBuilder applicationBuilder, string[] args) : base(args, - applicationBuilder.Services, applicationBuilder.Configuration, new ServerApplicationEnvironment(applicationBuilder.Environment), - applicationBuilder.Logging) => + applicationBuilder.Services, applicationBuilder.Configuration, + new ServerApplicationEnvironment(applicationBuilder.Environment), + applicationBuilder.Logging) + { this.applicationBuilder = applicationBuilder; + applicationBuilder.ConfigureContainer(new SitkoCoreServiceProviderBuilderFactory(), + _ => BeforeContainerBuild()); + } + protected override void BeforeModuleRegistration(IApplicationContext applicationContext, ApplicationModuleRegistration moduleRegistration) @@ -35,3 +42,20 @@ protected override void AfterModuleRegistration(IApplic } } } + +public class SitkoCoreServiceProviderBuilderFactory : IServiceProviderFactory +{ + public SitkoCoreServiceProviderBuilder CreateBuilder(IServiceCollection services) => new(services); + + public IServiceProvider CreateServiceProvider(SitkoCoreServiceProviderBuilder containerBuilder) => + containerBuilder.Build(); +} + +public class SitkoCoreServiceProviderBuilder +{ + private readonly IServiceCollection services; + + public SitkoCoreServiceProviderBuilder(IServiceCollection services) => this.services = services; + + public IServiceProvider Build() => services.BuildServiceProvider(); +} diff --git a/src/Sitko.Core.Blazor.Wasm/WebAssemblyHostBuilderExtensions.cs b/src/Sitko.Core.Blazor.Wasm/WebAssemblyHostBuilderExtensions.cs index c7618250f..770c779d8 100644 --- a/src/Sitko.Core.Blazor.Wasm/WebAssemblyHostBuilderExtensions.cs +++ b/src/Sitko.Core.Blazor.Wasm/WebAssemblyHostBuilderExtensions.cs @@ -40,6 +40,7 @@ public SitkoCoreBlazorWasmApplicationBuilder(WebAssemblyHostBuilder builder, str { options.AddDefaultResource(typeof(BaseForm)); }); + builder.ConfigureContainer(new SitkoCoreServiceProviderBuilderFactory(), _ => BeforeContainerBuild()); } protected override LoggerConfiguration ConfigureDefautLogger(LoggerConfiguration loggerConfiguration) @@ -56,7 +57,8 @@ public static class WebAssemblyHostBuilderExtensions public static ISitkoCoreBlazorApplicationBuilder AddSitkoCoreBlazorWasm(this WebAssemblyHostBuilder builder) => builder.AddSitkoCoreBlazorWasm(Array.Empty()); - public static ISitkoCoreBlazorApplicationBuilder AddSitkoCoreBlazorWasm(this WebAssemblyHostBuilder builder, string[] args) => + public static ISitkoCoreBlazorApplicationBuilder AddSitkoCoreBlazorWasm(this WebAssemblyHostBuilder builder, + string[] args) => ApplicationBuilderFactory.GetOrCreateApplicationBuilder(builder, applicationBuilder => new SitkoCoreBlazorWasmApplicationBuilder(applicationBuilder, args)); }