Skip to content

Commit

Permalink
fix(app): log boot messages
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Feb 7, 2024
1 parent d1c209b commit e2d4202
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Sitko.Core.App/ApplicationLifecycle.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
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<ApplicationModuleRegistration> applicationModuleRegistrations,
ILogger<ApplicationLifecycle> logger)
IBootLogger<ApplicationLifecycle> logger)
: IApplicationLifecycle
{
private readonly IReadOnlyList<ApplicationModuleRegistration> enabledModules =
ModulesHelper.GetEnabledModuleRegistrations(context, applicationModuleRegistrations);

public async Task StartingAsync(CancellationToken cancellationToken)
{
logger.LogInformation("Applicaiton starting");
await using var scope = provider.CreateAsyncScope();

foreach (var enabledModule in enabledModules)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -127,5 +132,7 @@ public async Task StoppedAsync(CancellationToken cancellationToken)
ex.ToString());
}
}

logger.LogInformation("Applicaiton stopped");
}
}
34 changes: 34 additions & 0 deletions src/Sitko.Core.App/Logging/BootLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Sitko.Core.App.Logging;

public class BootLogger<T> : IBootLogger<T>
{
private readonly ILogger<T> logger;
private readonly IApplicationContext applicationContext;
private readonly ILogger bootLogger;

// ReSharper disable once ContextualLoggerProblem
public BootLogger(ILogger<T> logger, IApplicationContext applicationContext,
[FromKeyedServices("BootLogger")] ILogger bootLogger)
{
this.logger = logger;
this.applicationContext = applicationContext;
this.bootLogger = bootLogger;
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
Func<TState, Exception?, string> 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>(TState state) where TState : notnull => logger.BeginScope(state);
}
7 changes: 7 additions & 0 deletions src/Sitko.Core.App/Logging/IBootLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Microsoft.Extensions.Logging;

namespace Sitko.Core.App.Logging;

internal interface IBootLogger<out T> : ILogger<T>
{
}
4 changes: 4 additions & 0 deletions src/Sitko.Core.App/SitkoCoreBaseApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Sitko.Core.App.Logging;
using Sitko.FluentValidation;
using Tempus;
using ILogger = Microsoft.Extensions.Logging.ILogger;

namespace Sitko.Core.App;

Expand Down Expand Up @@ -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<CommandsModule>();

Services.AddKeyedSingleton<ILogger>("BootLogger", InternalLogger);
Services.AddSingleton(typeof(IBootLogger<>), typeof(BootLogger<>));
Services.AddSingleton<IApplicationArgsProvider>(argsProvider);
Services.AddSingleton(Environment);
Services.AddSingleton<IApplicationContext, BuilderApplicationContext>();
Expand Down

0 comments on commit e2d4202

Please sign in to comment.