Skip to content

Commit

Permalink
Change warning
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-knozderko committed Sep 15, 2023
1 parent 16ec4bd commit b6f046f
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions Snowflake.Data/Core/Session/EasyLoggingStarter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,46 +49,49 @@ public virtual void Init(string configFilePathFromConnectionString)
return;
}
var config = _easyLoggingConfigProvider.ProvideConfig(configFilePathFromConnectionString);
var previousInitParameters = _initParameters;
if (config == null)
{
_initParameters = EasyLoggingInitParameters.WhenConfigNotFound(configFilePathFromConnectionString);
_initParameters = EasyLoggingInitParameters.CreateWhenConfigNotFound(configFilePathFromConnectionString);
return;
}
var logLevel = GetLogLevel(config.CommonProps.LogLevel);
var logPath = GetLogPath(config.CommonProps.LogPath);
WarnWhenConfigureForTheSecondTime(previousInitParameters);
_easyLoggerManager.ReconfigureEasyLogging(logLevel, logPath);
_initParameters = EasyLoggingInitParameters.WhenConfigFound(configFilePathFromConnectionString, logLevel, logPath);
_initParameters = EasyLoggingInitParameters.CreateWhenConfigFound(configFilePathFromConnectionString, logLevel, logPath);
}
}

private bool AllowedToInitialize(string configFilePathFromConnectionString)
{
return NeverTriedToInitialize() ||
(TriedToInitializedWithoutConfigFileFromConnectionString() &&
var isAllowed = NeverTriedToInitialize() ||
(TriedToInitializeWithoutConfigFileFromConnectionString() &&
!string.IsNullOrEmpty(configFilePathFromConnectionString));
}
if (!isAllowed)
{
WarnWhyNotAllowed(configFilePathFromConnectionString);
}

return isAllowed;
}

private bool NeverTriedToInitialize()
{
return _initParameters == null;
}

private bool TriedToInitializedWithoutConfigFileFromConnectionString()
private bool TriedToInitializeWithoutConfigFileFromConnectionString()
{
return _initParameters != null && _initParameters.HasNoConfigFilePathFromConnectionString();
}

private void WarnWhenConfigureForTheSecondTime(EasyLoggingInitParameters previousInitParameters)
private void WarnWhyNotAllowed(string configFilePath)
{
if (previousInitParameters == null)
{
return;
}
if (previousInitParameters.ShouldConfigureLogger && _initParameters.ShouldConfigureLogger)
var isDifferentConfigPath = _initParameters.ConfigFilePathFromConnectionString != null
&& configFilePath != null
&& _initParameters.ConfigFilePathFromConnectionString != configFilePath;
if (isDifferentConfigPath)
{
s_logger.Warn("Easy logging will be configured once again because it's the first time where client config is provided in a connection string");
s_logger.Warn($"Easy logging will not be configured for CLIENT_CONFIG_FILE={configFilePath} because it was previously configured for a different client config");
}
}

Expand Down Expand Up @@ -122,35 +125,31 @@ private string GetLogPath(string logPath)

internal class EasyLoggingInitParameters
{
private readonly string _configFilePathFromConnectionString;
public string ConfigFilePathFromConnectionString { get; }

private readonly EasyLoggingLogLevel? _logLevel;

private readonly string _logPath;

public bool ShouldConfigureLogger { get; }

private EasyLoggingInitParameters(
string configFilePathFromConnectionString,
EasyLoggingLogLevel? logLevel,
string logPath,
bool shouldConfigureLogger)
string logPath)
{
_configFilePathFromConnectionString = configFilePathFromConnectionString;
ConfigFilePathFromConnectionString = configFilePathFromConnectionString;
_logLevel = logLevel;
_logPath = logPath;
ShouldConfigureLogger = shouldConfigureLogger;
}

public static EasyLoggingInitParameters WhenConfigNotFound(string configFilePathFromConnectionString) =>
new EasyLoggingInitParameters(configFilePathFromConnectionString, null, null, false);
public static EasyLoggingInitParameters CreateWhenConfigNotFound(string configFilePathFromConnectionString) =>
new EasyLoggingInitParameters(configFilePathFromConnectionString, null, null);

public static EasyLoggingInitParameters WhenConfigFound(string configFilePathFromConnectionString, EasyLoggingLogLevel logLevel, string logPath) =>
new EasyLoggingInitParameters(configFilePathFromConnectionString, logLevel, logPath, true);
public static EasyLoggingInitParameters CreateWhenConfigFound(string configFilePathFromConnectionString, EasyLoggingLogLevel logLevel, string logPath) =>
new EasyLoggingInitParameters(configFilePathFromConnectionString, logLevel, logPath);

public bool HasNoConfigFilePathFromConnectionString()
{
return string.IsNullOrEmpty(_configFilePathFromConnectionString);
return string.IsNullOrEmpty(ConfigFilePathFromConnectionString);
}
}
}

0 comments on commit b6f046f

Please sign in to comment.