diff --git a/src/Sitko.Core.App/ApplicationLifecycle.cs b/src/Sitko.Core.App/ApplicationLifecycle.cs index e5a75e1d..6971ada8 100644 --- a/src/Sitko.Core.App/ApplicationLifecycle.cs +++ b/src/Sitko.Core.App/ApplicationLifecycle.cs @@ -1,14 +1,15 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Serilog; +using Sitko.Core.App.Logging; namespace Sitko.Core.App; -public class ApplicationLifecycle( +internal class ApplicationLifecycle( IApplicationContext context, IServiceProvider provider, IEnumerable applicationModuleRegistrations, - ILogger logger) + IBootLogger logger) : IApplicationLifecycle { private readonly IReadOnlyList enabledModules = @@ -16,6 +17,7 @@ public class ApplicationLifecycle( public async Task StartingAsync(CancellationToken cancellationToken) { + logger.LogInformation("Applicaiton starting"); await using var scope = provider.CreateAsyncScope(); foreach (var enabledModule in enabledModules) @@ -93,10 +95,13 @@ public async Task StartedAsync(CancellationToken cancellationToken) ex.ToString()); } } + + logger.LogInformation("Applicaiton started"); } public async Task StoppingAsync(CancellationToken cancellationToken) { + logger.LogInformation("Applicaiton stopping"); foreach (var moduleRegistration in enabledModules) { try @@ -127,5 +132,7 @@ public async Task StoppedAsync(CancellationToken cancellationToken) ex.ToString()); } } + + logger.LogInformation("Applicaiton stopped"); } } diff --git a/src/Sitko.Core.App/Logging/BootLogger.cs b/src/Sitko.Core.App/Logging/BootLogger.cs new file mode 100644 index 00000000..b2595af7 --- /dev/null +++ b/src/Sitko.Core.App/Logging/BootLogger.cs @@ -0,0 +1,34 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace Sitko.Core.App.Logging; + +public class BootLogger : IBootLogger +{ + private readonly ILogger logger; + private readonly IApplicationContext applicationContext; + private readonly ILogger bootLogger; + + // ReSharper disable once ContextualLoggerProblem + public BootLogger(ILogger logger, IApplicationContext applicationContext, + [FromKeyedServices("BootLogger")] ILogger bootLogger) + { + this.logger = logger; + this.applicationContext = applicationContext; + this.bootLogger = bootLogger; + } + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, + Func formatter) + { + logger.Log(logLevel, eventId, state, exception, formatter); + if (!applicationContext.IsDevelopment()) + { + bootLogger.Log(logLevel, eventId, state, exception, formatter); + } + } + + public bool IsEnabled(LogLevel logLevel) => true; + + public IDisposable? BeginScope(TState state) where TState : notnull => logger.BeginScope(state); +} diff --git a/src/Sitko.Core.App/Logging/IBootLogger.cs b/src/Sitko.Core.App/Logging/IBootLogger.cs new file mode 100644 index 00000000..5a798a99 --- /dev/null +++ b/src/Sitko.Core.App/Logging/IBootLogger.cs @@ -0,0 +1,7 @@ +using Microsoft.Extensions.Logging; + +namespace Sitko.Core.App.Logging; + +internal interface IBootLogger : ILogger +{ +} diff --git a/src/Sitko.Core.App/SitkoCoreBaseApplicationBuilder.cs b/src/Sitko.Core.App/SitkoCoreBaseApplicationBuilder.cs index 665ec205..3d09933b 100644 --- a/src/Sitko.Core.App/SitkoCoreBaseApplicationBuilder.cs +++ b/src/Sitko.Core.App/SitkoCoreBaseApplicationBuilder.cs @@ -10,6 +10,7 @@ using Sitko.Core.App.Logging; using Sitko.FluentValidation; using Tempus; +using ILogger = Microsoft.Extensions.Logging.ILogger; namespace Sitko.Core.App; @@ -111,12 +112,15 @@ private void Init() // configure logging Configuration.Add(new SerilogDynamicConfigurationSource()); internalLogger = CreateInternalLogger(); + internalLogger.LogInformation("Start application in {Environment}", Environment.EnvironmentName); Logging.ClearProviders(); Logging.AddSerilog(); serilogConfigurator.Configure(ConfigureDefautLogger); AddModule(); + Services.AddKeyedSingleton("BootLogger", InternalLogger); + Services.AddSingleton(typeof(IBootLogger<>), typeof(BootLogger<>)); Services.AddSingleton(argsProvider); Services.AddSingleton(Environment); Services.AddSingleton();