Skip to content

Commit

Permalink
warn about reconfiguring easy logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-knozderko committed Sep 15, 2023
1 parent 52fc80f commit b8bcba2
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions Snowflake.Data/Core/Session/EasyLoggingStarter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,17 @@ public virtual void Init(string configFilePathFromConnectionString)
return;
}
var config = _easyLoggingConfigProvider.ProvideConfig(configFilePathFromConnectionString);
var previousInitParameters = _initParameters;
if (config == null)
{
_initParameters = new EasyLoggingInitParameters(configFilePathFromConnectionString, null, null);
_initParameters = EasyLoggingInitParameters.WhenConfigNotFound(configFilePathFromConnectionString);
return;
}
var logLevel = GetLogLevel(config.CommonProps.LogLevel);
var logFilePath = GetLogPath(config.CommonProps.LogPath);
_easyLoggerManager.ReconfigureEasyLogging(logLevel, logFilePath);
_initParameters =
new EasyLoggingInitParameters(configFilePathFromConnectionString, logLevel, logFilePath);
var logPath = GetLogPath(config.CommonProps.LogPath);
WarnWhenConfigureForTheSecondTime(previousInitParameters);
_easyLoggerManager.ReconfigureEasyLogging(logLevel, logPath);
_initParameters = EasyLoggingInitParameters.WhenConfigFound(configFilePathFromConnectionString, logLevel, logPath);
}
}

Expand All @@ -78,6 +79,18 @@ private bool TriedToInitializedWithoutConfigFileFromConnectionString()
{
return _initParameters != null && _initParameters.HasNoConfigFilePathFromConnectionString();
}

private void WarnWhenConfigureForTheSecondTime(EasyLoggingInitParameters previousInitParameters)
{
if (previousInitParameters == null)
{
return;
}
if (previousInitParameters.ShouldConfigureLogger && _initParameters.ShouldConfigureLogger)
{
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");
}
}

private EasyLoggingLogLevel GetLogLevel(string logLevel)
{
Expand Down Expand Up @@ -114,14 +127,27 @@ internal class EasyLoggingInitParameters
private readonly EasyLoggingLogLevel? _logLevel;

private readonly string _logPath;

public bool ShouldConfigureLogger { get; }

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

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

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

public bool HasNoConfigFilePathFromConnectionString()
{
return string.IsNullOrEmpty(_configFilePathFromConnectionString);
Expand Down

0 comments on commit b8bcba2

Please sign in to comment.