Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NLogLoggingConfiguration - Refactor to reduce code complexity #751

Merged
merged 1 commit into from
May 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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