From 4422e34348a7667e0fb7c60f747eae067c6385fa Mon Sep 17 00:00:00 2001 From: Krzysztof Nozderko Date: Mon, 18 Sep 2023 13:33:08 +0200 Subject: [PATCH] refactor --- .../Core/Session/EasyLoggingStarter.cs | 73 ++++++------------- 1 file changed, 23 insertions(+), 50 deletions(-) diff --git a/Snowflake.Data/Core/Session/EasyLoggingStarter.cs b/Snowflake.Data/Core/Session/EasyLoggingStarter.cs index 1d3f27e1d..a4663bcc5 100644 --- a/Snowflake.Data/Core/Session/EasyLoggingStarter.cs +++ b/Snowflake.Data/Core/Session/EasyLoggingStarter.cs @@ -21,7 +21,7 @@ internal class EasyLoggingStarter private readonly object _lockForExclusiveInit = new object(); - private EasyLoggingInitParameters _initParameters = null; + private EasyLoggingInitTrialParameters _initTrialParameters = null; public static readonly EasyLoggingStarter Instance = new EasyLoggingStarter(EasyLoggingConfigProvider.Instance, EasyLoggerManager.Instance, DirectoryOperations.Instance); @@ -51,50 +51,30 @@ public virtual void Init(string configFilePathFromConnectionString) var config = _easyLoggingConfigProvider.ProvideConfig(configFilePathFromConnectionString); if (config == null) { - _initParameters = EasyLoggingInitParameters.CreateWhenConfigNotFound(configFilePathFromConnectionString); + _initTrialParameters = new EasyLoggingInitTrialParameters(configFilePathFromConnectionString); return; } var logLevel = GetLogLevel(config.CommonProps.LogLevel); var logPath = GetLogPath(config.CommonProps.LogPath); _easyLoggerManager.ReconfigureEasyLogging(logLevel, logPath); - _initParameters = EasyLoggingInitParameters.CreateWhenConfigFound(configFilePathFromConnectionString, logLevel, logPath); + _initTrialParameters = new EasyLoggingInitTrialParameters(configFilePathFromConnectionString); } } private bool AllowedToInitialize(string configFilePathFromConnectionString) { - var isAllowed = NeverTriedToInitialize() || - (TriedToInitializeWithoutConfigFileFromConnectionString() && - !string.IsNullOrEmpty(configFilePathFromConnectionString)); - if (!isAllowed) + var everTriedToInitialize = _initTrialParameters != null; + var triedToInitializeWithoutConfigFile = everTriedToInitialize && !_initTrialParameters.IsConfigFilePathGiven(); + var isGivenConfigFilePath = !string.IsNullOrEmpty(configFilePathFromConnectionString); + var isAllowedToInitialize = !everTriedToInitialize || (triedToInitializeWithoutConfigFile && isGivenConfigFilePath); + if (!isAllowedToInitialize && _initTrialParameters.HasDifferentConfigPath(configFilePathFromConnectionString)) { - WarnWhyNotAllowed(configFilePathFromConnectionString); + s_logger.Warn($"Easy logging will not be configured for CLIENT_CONFIG_FILE={configFilePathFromConnectionString} because it was previously configured for a different client config"); } - return isAllowed; - } - - private bool NeverTriedToInitialize() - { - return _initParameters == null; + return isAllowedToInitialize; } - private bool TriedToInitializeWithoutConfigFileFromConnectionString() - { - return _initParameters != null && _initParameters.HasNoConfigFilePathFromConnectionString(); - } - - private void WarnWhyNotAllowed(string configFilePath) - { - var isDifferentConfigPath = _initParameters.ConfigFilePathFromConnectionString != null - && configFilePath != null - && _initParameters.ConfigFilePathFromConnectionString != configFilePath; - if (isDifferentConfigPath) - { - s_logger.Warn($"Easy logging will not be configured for CLIENT_CONFIG_FILE={configFilePath} because it was previously configured for a different client config"); - } - } - private EasyLoggingLogLevel GetLogLevel(string logLevel) { if (string.IsNullOrEmpty(logLevel)) @@ -123,33 +103,26 @@ private string GetLogPath(string logPath) } } - internal class EasyLoggingInitParameters + internal class EasyLoggingInitTrialParameters { - public string ConfigFilePathFromConnectionString { get; } - - private readonly EasyLoggingLogLevel? _logLevel; - - private readonly string _logPath; + private string _configFilePathFromConnectionString { get; } - private EasyLoggingInitParameters( - string configFilePathFromConnectionString, - EasyLoggingLogLevel? logLevel, - string logPath) + public EasyLoggingInitTrialParameters( + string configFilePathFromConnectionString) { - ConfigFilePathFromConnectionString = configFilePathFromConnectionString; - _logLevel = logLevel; - _logPath = logPath; + _configFilePathFromConnectionString = configFilePathFromConnectionString; } - public static EasyLoggingInitParameters CreateWhenConfigNotFound(string configFilePathFromConnectionString) => - new EasyLoggingInitParameters(configFilePathFromConnectionString, null, null); + public bool IsConfigFilePathGiven() + { + return _configFilePathFromConnectionString != null; + } - public static EasyLoggingInitParameters CreateWhenConfigFound(string configFilePathFromConnectionString, EasyLoggingLogLevel logLevel, string logPath) => - new EasyLoggingInitParameters(configFilePathFromConnectionString, logLevel, logPath); - - public bool HasNoConfigFilePathFromConnectionString() + public bool HasDifferentConfigPath(string configFilePath) { - return string.IsNullOrEmpty(ConfigFilePathFromConnectionString); + return IsConfigFilePathGiven() + && configFilePath != null + && _configFilePathFromConnectionString != configFilePath; } } }