Skip to content

Commit

Permalink
Adjusted NLog#412 by renaming LoadNLogConfigFromSection to LoadLoadCo…
Browse files Browse the repository at this point in the history
…nfigurationFromSection
  • Loading branch information
snakefoot committed May 10, 2020
1 parent 4bd3bcb commit f856768
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/NetCore2/ConsoleExampleJsonConfig/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private static void Main()

LogManager.Setup()
.SetupExtensions(s => s.AutoLoadAssemblies(false))
.LoadNLogConfigFromSection(config);
.LoadConfigurationFromSection(config);

var servicesProvider = BuildDi(config);
using (servicesProvider as IDisposable)
Expand Down
11 changes: 7 additions & 4 deletions src/NLog.Extensions.Logging/Config/SetupBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ public static class SetupBuilderExtensions
/// <summary>
/// Loads NLog LoggingConfiguration from appsettings.json from NLog-section
/// </summary>
public static ISetupBuilder LoadNLogConfigFromSection(this ISetupBuilder setupBuilder, Microsoft.Extensions.Configuration.IConfiguration configuration)
public static ISetupBuilder LoadConfigurationFromSection(this ISetupBuilder setupBuilder, Microsoft.Extensions.Configuration.IConfiguration configuration, string configSection = "NLog")
{
setupBuilder.SetupExtensions(s => s.RegisterConfigSettings(configuration));
var nlogConfig = configuration.GetSection("NLog");
if (nlogConfig != null && nlogConfig.GetChildren().Any())
if (!string.IsNullOrEmpty(configSection))
{
setupBuilder.LogFactory.Configuration = new NLogLoggingConfiguration(nlogConfig, setupBuilder.LogFactory);
var nlogConfig = configuration.GetSection(configSection);
if (nlogConfig != null && nlogConfig.GetChildren().Any())
{
setupBuilder.LogFactory.Configuration = new NLogLoggingConfiguration(nlogConfig, setupBuilder.LogFactory);
}
}
return setupBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public void ReloadLogFactoryConfigurationKeepVariables()
}

[Fact]
public void SetupBuilderNLogLoggingConfiguration()
public void SetupBuilderLoadConfigurationFromSection()
{
var memoryConfig = CreateMemoryConfigConsoleTargetAndRule();
memoryConfig["NLog:Targets:file:type"] = "File";
Expand All @@ -181,7 +181,7 @@ public void SetupBuilderNLogLoggingConfiguration()
var logFactory = new LogFactory();
logFactory.Setup()
.SetupExtensions(s => s.AutoLoadAssemblies(false))
.LoadNLogConfigFromSection(configuration);
.LoadConfigurationFromSection(configuration);

Assert.Single(logFactory.Configuration.LoggingRules);
Assert.Equal(2, logFactory.Configuration.LoggingRules[0].Targets.Count);
Expand Down

0 comments on commit f856768

Please sign in to comment.