Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-knozderko committed Sep 18, 2023
1 parent b6f046f commit 4422e34
Showing 1 changed file with 23 additions and 50 deletions.
73 changes: 23 additions & 50 deletions Snowflake.Data/Core/Session/EasyLoggingStarter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit 4422e34

Please sign in to comment.