diff --git a/src/Sitko.Core.App/Application.cs b/src/Sitko.Core.App/Application.cs index 05667bfeb..5265d5c65 100644 --- a/src/Sitko.Core.App/Application.cs +++ b/src/Sitko.Core.App/Application.cs @@ -47,15 +47,9 @@ protected Application(string[] args) Configuration = tmpHost.Services.GetService(); Environment = tmpHost.Services.GetService(); Logger = tmpHost.Services.GetService>(); - LoggingEnableConsole = Environment.IsDevelopment(); Name = Environment.ApplicationName; - _loggerConfiguration.MinimumLevel.ControlledBy(_logLevelSwitcher.Switch); - _loggerConfiguration.Enrich.FromLogContext() - .Enrich.WithProperty("App", Name) - .Enrich.WithProperty("AppVersion", Version); - HostBuilder = Host.CreateDefaultBuilder(args) .UseDefaultServiceProvider(options => { @@ -69,9 +63,12 @@ protected Application(string[] args) }); } - protected LogEventLevel LoggingLevel { get; set; } = LogEventLevel.Information; - protected bool LoggingEnableConsole { get; set; } - protected Action? LoggingConfigure { get; set; } + protected virtual bool LoggingEnableConsole => Environment.IsDevelopment(); + + protected virtual void ConfigureLogging(LoggerConfiguration loggerConfiguration, + LogLevelSwitcher logLevelSwitcher) + { + } public string Name { get; private set; } public string Version { get; private set; } = "dev"; @@ -164,28 +161,34 @@ public IHostBuilder GetHostBuilder() return HostBuilder; } - protected virtual void ConfigureLogging() + protected virtual string ConsoleLogFormat => + "[{Timestamp:HH:mm:ss} {Level:u3} {SourceContext}] {Message:lj}{NewLine}{Exception}"; + + private void InitLogging() { - _logLevelSwitcher.Switch.MinimumLevel = LoggingLevel; - _logLevelSwitcher.MsMessagesSwitch.MinimumLevel = LogEventLevel.Warning; - _loggerConfiguration.MinimumLevel.Override("Microsoft", _logLevelSwitcher.MsMessagesSwitch); + _loggerConfiguration.MinimumLevel.ControlledBy(_logLevelSwitcher.Switch); + _loggerConfiguration.Enrich.FromLogContext() + .Enrich.WithProperty("App", Name) + .Enrich.WithProperty("AppVersion", Version); + _logLevelSwitcher.Switch.MinimumLevel = + Environment.IsDevelopment() ? LogEventLevel.Debug : LogEventLevel.Information; if (LoggingEnableConsole) { _loggerConfiguration .WriteTo.Console( - outputTemplate: - "[{Timestamp:HH:mm:ss} {Level:u3} {SourceContext}] {Message:lj}{NewLine}{Exception}", + outputTemplate: ConsoleLogFormat, levelSwitch: _logLevelSwitcher.Switch); } - LoggingConfigure?.Invoke(_loggerConfiguration, _logLevelSwitcher); - foreach (var entry in LogEventLevels) + ConfigureLogging(_loggerConfiguration, _logLevelSwitcher); + foreach ((var key, LogEventLevel value) in LogEventLevels) { - _loggerConfiguration.MinimumLevel.Override(entry.Key, entry.Value); + _loggerConfiguration.MinimumLevel.Override(key, value); } } + protected void RegisterModule( Action? configure = null) where TModule : IApplicationModule where TModuleConfig : class, new() @@ -204,16 +207,23 @@ protected void RegisterModule( configure?.Invoke(context.Configuration, context.HostingEnvironment, config); } - var module = (TModule)Activator.CreateInstance(typeof(TModule), config, this); - if (!_check) + var instance = Activator.CreateInstance(typeof(TModule), config, this); + if (instance is TModule module) { - module.CheckConfig(); - } + if (!_check) + { + module.CheckConfig(); + } - module.ConfigureLogging(_loggerConfiguration, _logLevelSwitcher, - context.Configuration, context.HostingEnvironment); - module.ConfigureServices(services, context.Configuration, context.HostingEnvironment); - AddModule(module); + module.ConfigureLogging(_loggerConfiguration, _logLevelSwitcher, + context.Configuration, context.HostingEnvironment); + module.ConfigureServices(services, context.Configuration, context.HostingEnvironment); + AddModule(module); + } + else + { + throw new Exception($"Can't instantiate module {typeof(TModule)}"); + } }); } @@ -236,7 +246,7 @@ private void Init() } InitApplication(); - ConfigureLogging(); + InitLogging(); _initComplete = true; } } @@ -358,16 +368,11 @@ public T Get(string key) return default; #pragma warning restore 8603 } - - protected void LogModuleRegistrationFailed() where T : IApplicationModule - { - Logger.LogError("Can't register module {Module}: empty configuration", typeof(T)); - } } - public class Application : Application where T : Application + public abstract class Application : Application where T : Application { - public Application(string[] args) : base(args) + protected Application(string[] args) : base(args) { ConfigureServices((context, services) => { @@ -419,7 +424,7 @@ public T ConfigureAppConfiguration(Action { public TestApplication(string[] args) : base(args) { - LoggingLevel = LogEventLevel.Debug; + } + + protected override void ConfigureLogging(LoggerConfiguration loggerConfiguration, + LogLevelSwitcher logLevelSwitcher) + { + base.ConfigureLogging(loggerConfiguration, logLevelSwitcher); + logLevelSwitcher.Switch.MinimumLevel = LogEventLevel.Debug; } }