Skip to content

Commit

Permalink
NLog.LogManager.Setup() - Replaced LogFactory-builder method with ISe…
Browse files Browse the repository at this point in the history
…tupNLogConfigBuilder
  • Loading branch information
snakefoot committed Apr 22, 2020
1 parent 5ec96ae commit 426c07b
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 22 deletions.
51 changes: 51 additions & 0 deletions src/NLog/Config/ISetupNLogConfigBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// Copyright (c) 2004-2020 Jaroslaw Kowalski <jaak@jkowalski.net>, Kim Christensen, Julian Verdurmen
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Jaroslaw Kowalski nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//

namespace NLog.Config
{
/// <summary>
/// Interface for fluent setup of LogFactory options
/// </summary>
public interface ISetupNLogConfigBuilder
{
/// <summary>
/// LogFactory under configuration
/// </summary>
LogFactory LogFactory { get; }

/// <summary>
/// LoggingConfiguration being built
/// </summary>
LoggingConfiguration Configuration { get; set; }
}
}
56 changes: 56 additions & 0 deletions src/NLog/Internal/SetupNLogConfigBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Copyright (c) 2004-2020 Jaroslaw Kowalski <jaak@jkowalski.net>, Kim Christensen, Julian Verdurmen
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Jaroslaw Kowalski nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//

namespace NLog.Internal
{
using NLog.Config;

internal class SetupNLogConfigBuilder : ISetupNLogConfigBuilder
{
internal SetupNLogConfigBuilder(LogFactory logFactory, LoggingConfiguration configuration)
{
LogFactory = logFactory;
Configuration = configuration;
}

public LogFactory LogFactory { get; }

public LoggingConfiguration Configuration
{
get => _configuration ?? (_configuration = new LoggingConfiguration(LogFactory));
set => _configuration = value;
}

internal LoggingConfiguration _configuration;
}
}
46 changes: 26 additions & 20 deletions src/NLog/SetupBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,46 +70,52 @@ public static ISetupBuilder SetupSerialization(this ISetupBuilder setupBuilder,
}

/// <summary>
/// Loads NLog config from filename <paramref name="configFile"/> if provided, else fallback to scanning for NLog.config
/// Loads NLog config created by the method <paramref name="configBuilder"/>
/// </summary>
public static ISetupBuilder LoadNLogConfigFromFile(this ISetupBuilder setupBuilder, string configFile = null, bool optional = true)
public static ISetupBuilder LoadNLogConfig(this ISetupBuilder setupBuilder, Action<ISetupNLogConfigBuilder> configBuilder)
{
setupBuilder.LogFactory.LoadConfiguration(configFile, optional);
var config = setupBuilder.LogFactory._config;
var setupConfig = new SetupNLogConfigBuilder(setupBuilder.LogFactory, config);
configBuilder(setupConfig);
var newConfig = setupConfig._configuration;
bool configHasChanged = !ReferenceEquals(config, setupBuilder.LogFactory._config);

if (ReferenceEquals(newConfig, setupBuilder.LogFactory._config))
{
setupBuilder.LogFactory.ReconfigExistingLoggers();
}
else if (!configHasChanged || !ReferenceEquals(config, newConfig))
{
setupBuilder.LogFactory.Configuration = newConfig;
}

return setupBuilder;
}

/// <summary>
/// Loads NLog config from XML in <paramref name="configXml"/>
/// Loads NLog config provided in <paramref name="loggingConfiguration"/>
/// </summary>
public static ISetupBuilder LoadNLogConfigFromXml(this ISetupBuilder setupBuilder, string configXml)
public static ISetupBuilder LoadNLogConfig(this ISetupBuilder setupBuilder, LoggingConfiguration loggingConfiguration)
{
setupBuilder.LogFactory.Configuration = XmlLoggingConfiguration.CreateFromXmlString(configXml, setupBuilder.LogFactory);
setupBuilder.LogFactory.Configuration = loggingConfiguration;
return setupBuilder;
}

/// <summary>
/// Loads NLog config provided in <paramref name="loggingConfiguration"/>
/// Loads NLog config from filename <paramref name="configFile"/> if provided, else fallback to scanning for NLog.config
/// </summary>
public static ISetupBuilder LoadNLogConfig(this ISetupBuilder setupBuilder, LoggingConfiguration loggingConfiguration)
public static ISetupBuilder LoadNLogConfigFromFile(this ISetupBuilder setupBuilder, string configFile = null, bool optional = true)
{
setupBuilder.LogFactory.Configuration = loggingConfiguration;
setupBuilder.LogFactory.LoadConfiguration(configFile, optional);
return setupBuilder;
}

/// <summary>
/// Loads NLog config returned by generator method <paramref name="configBuilder"/>
/// Loads NLog config from XML in <paramref name="configXml"/>
/// </summary>
public static ISetupBuilder LoadNLogConfig(this ISetupBuilder setupBuilder, Func<LogFactory, LoggingConfiguration> configBuilder)
public static ISetupBuilder LoadNLogConfigFromXml(this ISetupBuilder setupBuilder, string configXml)
{
var config = configBuilder(setupBuilder.LogFactory);
if (ReferenceEquals(config, setupBuilder.LogFactory._config))
{
setupBuilder.LogFactory.ReconfigExistingLoggers();
}
else
{
setupBuilder.LogFactory.Configuration = config;
}
setupBuilder.LogFactory.Configuration = XmlLoggingConfiguration.CreateFromXmlString(configXml, setupBuilder.LogFactory);
return setupBuilder;
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/NLog.UnitTests/Config/LogFactorySetupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public void SetupBuilderLoadNLogConfigFactoryTest()
LoggingConfiguration logConfig = null;

// Act
logFactory.Setup().LoadNLogConfig(f => logConfig = new LoggingConfiguration(f));
logFactory.Setup().LoadNLogConfig(b => logConfig = b.Configuration);

// Assert
Assert.Same(logConfig, logFactory.Configuration);
Expand Down Expand Up @@ -532,7 +532,7 @@ public void SetupBuilderLoadNLogConfigFromXmlPatchTest()
logFactory.Setup().
LoadNLogConfigFromXml("<nlog autoshutdown='false'></nlog>").
LoadNLogConfigFromFile(). // No effect, since config already loaded
LoadNLogConfig(f => { f.Configuration.Variables["Hello"] = "World"; return f.Configuration; });
LoadNLogConfig(b => { b.Configuration.Variables["Hello"] = "World"; });

// Assert
Assert.False(logFactory.AutoShutdown);
Expand Down

0 comments on commit 426c07b

Please sign in to comment.