Skip to content

Commit

Permalink
Fix Sonar code smell (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored May 26, 2024
1 parent 4547441 commit e471ed5
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,18 +348,8 @@ private IEnumerable<IConfigurationSection> GetVariablesChildren(IConfigurationSe
if (configValue is null)
continue;

var independentVariable = true;
for (int j = sortVariables.Count - 1; j >= 0; j--)
{
var otherConfigKey = sortVariables[j].Key;
var referenceVariable = $"${{{otherConfigKey}}}";
if (configValue.IndexOf(referenceVariable, StringComparison.OrdinalIgnoreCase) >= 0)
{
independentVariable = false;
break;
}
}
if (independentVariable)
bool usingOtherVariables = IsNLogConfigVariableValueUsingOthers(configValue, sortVariables);
if (!usingOtherVariables)
{
foundIndependentVariable = true;
yield return sortVariables[i].Value;
Expand All @@ -378,6 +368,21 @@ private IEnumerable<IConfigurationSection> GetVariablesChildren(IConfigurationSe
}
}

private static bool IsNLogConfigVariableValueUsingOthers(string variableValue, List<KeyValuePair<string, IConfigurationSection>> allVariables)
{
foreach (var otherConfigVariable in allVariables)
{
var otherConfigKey = otherConfigVariable.Key;
var referenceVariable = $"${{{otherConfigKey}}}";
if (variableValue.IndexOf(referenceVariable, StringComparison.OrdinalIgnoreCase) >= 0)
{
return true;
}
}

return false;
}

private static string GetConfigKey(IConfigurationSection child)
{
return child.Key?.Trim() ?? string.Empty;
Expand Down

0 comments on commit e471ed5

Please sign in to comment.