Skip to content

Commit

Permalink
#49 refactor application
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Sep 25, 2020
1 parent 50304e8 commit 2fd6927
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 37 deletions.
75 changes: 40 additions & 35 deletions src/Sitko.Core.App/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,9 @@ protected Application(string[] args)
Configuration = tmpHost.Services.GetService<IConfiguration>();
Environment = tmpHost.Services.GetService<IHostEnvironment>();
Logger = tmpHost.Services.GetService<ILogger<Application>>();
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 =>
{
Expand All @@ -69,9 +63,12 @@ protected Application(string[] args)
});
}

protected LogEventLevel LoggingLevel { get; set; } = LogEventLevel.Information;
protected bool LoggingEnableConsole { get; set; }
protected Action<LoggerConfiguration, LogLevelSwitcher>? 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";
Expand Down Expand Up @@ -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<TModule, TModuleConfig>(
Action<IConfiguration, IHostEnvironment, TModuleConfig>? configure = null)
where TModule : IApplicationModule<TModuleConfig> where TModuleConfig : class, new()
Expand All @@ -204,16 +207,23 @@ protected void RegisterModule<TModule, TModuleConfig>(
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)}");
}
});
}

Expand All @@ -236,7 +246,7 @@ private void Init()
}

InitApplication();
ConfigureLogging();
InitLogging();
_initComplete = true;
}
}
Expand Down Expand Up @@ -358,16 +368,11 @@ public T Get<T>(string key)
return default;
#pragma warning restore 8603
}

protected void LogModuleRegistrationFailed<T>() where T : IApplicationModule
{
Logger.LogError("Can't register module {Module}: empty configuration", typeof(T));
}
}

public class Application<T> : Application where T : Application<T>
public abstract class Application<T> : Application where T : Application<T>
{
public Application(string[] args) : base(args)
protected Application(string[] args) : base(args)
{
ConfigureServices((context, services) =>
{
Expand Down Expand Up @@ -419,7 +424,7 @@ public T ConfigureAppConfiguration(Action<HostBuilderContext, IConfigurationBuil

protected override string? GetVersion()
{
return typeof(T).Assembly.GetName().Version.ToString();
return typeof(T).Assembly.GetName().Version?.ToString();
}
}
}
1 change: 0 additions & 1 deletion src/Sitko.Core.App/Logging/LogLevelSwitcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ namespace Sitko.Core.App.Logging
public class LogLevelSwitcher
{
public readonly LoggingLevelSwitch Switch = new LoggingLevelSwitch();
public readonly LoggingLevelSwitch MsMessagesSwitch = new LoggingLevelSwitch();
}
}
8 changes: 7 additions & 1 deletion src/Sitko.Core.Xunit/BaseTestScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ public class TestApplication : Application<TestApplication>
{
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;
}
}

Expand Down

0 comments on commit 2fd6927

Please sign in to comment.