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

Auto ordering of variables-section when dependent complex variables #739

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ private IEnumerable<IConfigurationSection> GetVariablesChildren(IConfigurationSe
{
var configKey = GetConfigKey(variable);
var configValue = variable.Value;
if (string.IsNullOrEmpty(configKey) || string.IsNullOrEmpty(configValue) || !configValue.Contains('$'))
if (string.IsNullOrEmpty(configKey) || configValue?.Contains('$') == false)
yield return variable;

sortVariables ??= new List<KeyValuePair<string, IConfigurationSection>>();
Expand All @@ -345,6 +345,9 @@ private IEnumerable<IConfigurationSection> GetVariablesChildren(IConfigurationSe
for (int i = sortVariables.Count - 1; i >= 0; i--)
{
var configValue = sortVariables[i].Value.Value;
if (configValue is null)
continue;

var independentVariable = true;
for (int j = i - 1; j >= 0; j--)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using NLog.Targets;
using NLog.Targets.Wrappers;
using Xunit;
using Xunit.Sdk;

namespace NLog.Extensions.Logging.Tests
{
Expand All @@ -14,6 +15,11 @@ public class NLogLoggingConfigurationTests
const string DefaultSectionName = "NLog";
const string CustomSectionName = "MyCustomSection";

public NLogLoggingConfigurationTests()
{
LogManager.ThrowConfigExceptions = true;
}

[Fact]
public void LoadSimpleConfig()
{
Expand Down Expand Up @@ -188,7 +194,7 @@ public void LoadVariablesConfig()
}

[Fact]
public void LoadVariablesSortedConfig()
public void LoadVariablesDependentConfig()
{
var memoryConfig = CreateMemoryConfigConsoleTargetAndRule();
memoryConfig["NLog:Targets:file:type"] = "File";
Expand All @@ -212,13 +218,13 @@ public void LoadVariableJsonLayoutConfig()
var memoryConfig = CreateMemoryConfigConsoleTargetAndRule();
memoryConfig["NLog:Targets:file:type"] = "File";
memoryConfig["NLog:Targets:file:fileName"] = "hello.txt";
memoryConfig["NLog:Targets:file:layout"] = "${my_json}";
memoryConfig["NLog:Targets:console:layout"] = "${my_json}";
memoryConfig["NLog:Variables:my_json:type"] = "JsonLayout";
memoryConfig["NLog:Variables:my_json:attributes:0:name"] = "message";
memoryConfig["NLog:Variables:my_json:attributes:0:layout"] = "${message}";
memoryConfig["NLog:Variables:my_json:attributes:1:name"] = "logger";
memoryConfig["NLog:Variables:my_json:attributes:1:layout"] = "${logger}";
memoryConfig["NLog:Targets:file:layout"] = "${var_json}";
memoryConfig["NLog:Targets:console:layout"] = "${var_json}";
memoryConfig["NLog:Variables:var_json:type"] = "JsonLayout";
memoryConfig["NLog:Variables:var_json:attributes:0:name"] = "message";
memoryConfig["NLog:Variables:var_json:attributes:0:layout"] = "${message}";
memoryConfig["NLog:Variables:var_json:attributes:1:name"] = "logger";
memoryConfig["NLog:Variables:var_json:attributes:1:layout"] = "${logger}";

var logConfig = CreateNLogLoggingConfigurationWithNLogSection(memoryConfig);

Expand All @@ -231,6 +237,31 @@ public void LoadVariableJsonLayoutConfig()
Assert.Equal(2, logConfig.AllTargets.Count(t => (t as TargetWithLayout)?.Layout is NLog.Layouts.JsonLayout));
}

[Fact]
public void LoadVariableDependentJsonLayoutConfig()
{
var memoryConfig = CreateMemoryConfigConsoleTargetAndRule();
memoryConfig["NLog:Targets:file:type"] = "File";
memoryConfig["NLog:Targets:file:fileName"] = "hello.txt";
memoryConfig["NLog:Targets:file:layout"] = "${var_json}";
memoryConfig["NLog:Targets:console:layout"] = "${var_json}";
memoryConfig["NLog:Variables:var_folder"] = "hello";
memoryConfig["NLog:Variables:var_file"] = "${var_folder}/world.txt";
memoryConfig["NLog:Variables:var_json:type"] = "JsonLayout";
memoryConfig["NLog:Variables:var_json:attributes:0:name"] = "file";
memoryConfig["NLog:Variables:var_json:attributes:0:layout"] = "${var_file}";

var logConfig = CreateNLogLoggingConfigurationWithNLogSection(memoryConfig);

Assert.Single(logConfig.LoggingRules);
Assert.Equal(3, logConfig.Variables.Count);
Assert.Equal(2, logConfig.LoggingRules[0].Targets.Count);
Assert.Equal(2, logConfig.AllTargets.Count);
Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget));
Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget));
Assert.Equal(2, logConfig.AllTargets.Count(t => (t as TargetWithLayout)?.Layout is NLog.Layouts.JsonLayout));
}

[Fact]
public void LoadDefaultWrapperConfig()
{
Expand Down