diff --git a/src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs b/src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs
index af736f62..62d23eda 100644
--- a/src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs
+++ b/src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs
@@ -12,7 +12,7 @@
namespace NLog.Extensions.Hosting
{
///
- /// Helpers for IHostbuilder, netcore 2.1
+ /// Helpers for IHostbuilder
///
public static class ConfigureExtensions
{
@@ -24,7 +24,7 @@ public static class ConfigureExtensions
/// IHostBuilder for chaining
public static IHostBuilder UseNLog(this IHostBuilder builder)
{
- if (builder == null) throw new ArgumentNullException(nameof(builder));
+ Guard.ThrowIfNull(builder);
return builder.UseNLog(null);
}
@@ -37,7 +37,7 @@ public static IHostBuilder UseNLog(this IHostBuilder builder)
/// IHostBuilder for chaining
public static IHostBuilder UseNLog(this IHostBuilder builder, NLogProviderOptions options)
{
- if (builder == null) throw new ArgumentNullException(nameof(builder));
+ Guard.ThrowIfNull(builder);
#if NETSTANDARD2_0
builder.ConfigureServices((builderContext, services) => AddNLogLoggerProvider(services, builderContext.Configuration, null, options, CreateNLogLoggerProvider));
#else
diff --git a/src/NLog.Extensions.Hosting/NLog.Extensions.Hosting.csproj b/src/NLog.Extensions.Hosting/NLog.Extensions.Hosting.csproj
index 9557ba5b..05d2f80d 100644
--- a/src/NLog.Extensions.Hosting/NLog.Extensions.Hosting.csproj
+++ b/src/NLog.Extensions.Hosting/NLog.Extensions.Hosting.csproj
@@ -49,6 +49,7 @@ Full changelog: https://github.com/NLog/NLog.Extensions.Logging/blob/master/CHAN
+
diff --git a/src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs b/src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs
index b8e6cb14..7dc6e58a 100644
--- a/src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs
+++ b/src/NLog.Extensions.Logging/Config/NLogLoggingConfiguration.cs
@@ -55,7 +55,7 @@ public override IEnumerable FileNamesToWatch
{
get
{
- if (_autoReload && _reloadConfiguration == null)
+ if (_autoReload && _reloadConfiguration is null)
{
// Prepare for setting up reload notification handling
_reloadConfiguration = state => ReloadConfigurationSection((IConfigurationSection)state);
@@ -229,7 +229,7 @@ private IEnumerable GetChildren()
}
}
- if (ReferenceEquals(_nameOverride, VariableKey) && _configurationSection.Value == null)
+ if (ReferenceEquals(_nameOverride, VariableKey) && _configurationSection.Value is null)
{
yield return new LoggingConfigurationElement(_configurationSection);
}
@@ -255,7 +255,7 @@ private IEnumerable GetChildren(IEnumerable
diff --git a/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs b/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs
index 69c54869..e2213234 100644
--- a/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs
+++ b/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs
@@ -41,6 +41,7 @@ public static ILoggerFactory AddNLog(this ILoggerFactory factory)
#endif
public static ILoggerFactory AddNLog(this ILoggerFactory factory, NLogProviderOptions options)
{
+ Guard.ThrowIfNull(factory);
factory.AddProvider(new NLogLoggerProvider(options));
return factory;
}
@@ -56,6 +57,7 @@ public static ILoggerFactory AddNLog(this ILoggerFactory factory, NLogProviderOp
#endif
public static ILoggerFactory AddNLog(this ILoggerFactory factory, IConfiguration configuration)
{
+ Guard.ThrowIfNull(factory);
var provider = CreateNLogLoggerProvider(null, configuration, null);
factory.AddProvider(provider);
return factory;
@@ -65,11 +67,11 @@ public static ILoggerFactory AddNLog(this ILoggerFactory factory, IConfiguration
///
/// Enable NLog as logging provider for Microsoft Extension Logging
///
- ///
+ ///
/// ILoggingBuilder for chaining
- public static ILoggingBuilder AddNLog(this ILoggingBuilder factory)
+ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder)
{
- return factory.AddNLog((NLogProviderOptions)null);
+ return builder.AddNLog((NLogProviderOptions)null);
}
///
@@ -80,6 +82,7 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder factory)
/// ILoggingBuilder for chaining
public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, IConfiguration configuration)
{
+ Guard.ThrowIfNull(factory);
AddNLogLoggerProvider(factory, configuration, null, CreateNLogLoggerProvider);
return factory;
}
@@ -87,26 +90,28 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, IConfigurati
///
/// Enable NLog as logging provider for Microsoft Extension Logging
///
- ///
+ ///
/// Override configuration and not use default Host Builder Configuration
/// NLog Logging Provider options
/// ILoggingBuilder for chaining
- public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, IConfiguration configuration, NLogProviderOptions options)
+ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, IConfiguration configuration, NLogProviderOptions options)
{
- AddNLogLoggerProvider(factory, configuration, options, CreateNLogLoggerProvider);
- return factory;
+ Guard.ThrowIfNull(builder);
+ AddNLogLoggerProvider(builder, configuration, options, CreateNLogLoggerProvider);
+ return builder;
}
///
/// Enable NLog as logging provider for Microsoft Extension Logging
///
- ///
+ ///
/// NLog Logging Provider options
/// ILoggingBuilder for chaining
- public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, NLogProviderOptions options)
+ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, NLogProviderOptions options)
{
- AddNLogLoggerProvider(factory, null, options, CreateNLogLoggerProvider);
- return factory;
+ Guard.ThrowIfNull(builder);
+ AddNLogLoggerProvider(builder, null, options, CreateNLogLoggerProvider);
+ return builder;
}
///
@@ -129,6 +134,7 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfi
/// ILoggingBuilder for chaining
public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfiguration configuration, NLogProviderOptions options)
{
+ Guard.ThrowIfNull(builder);
AddNLogLoggerProvider(builder, null, options, (serviceProvider, config, options) =>
{
var logFactory = configuration?.LogFactory ?? LogManager.LogFactory;
@@ -148,6 +154,7 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfi
/// ILoggingBuilder for chaining
public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, string configFileRelativePath)
{
+ Guard.ThrowIfNull(builder);
AddNLogLoggerProvider(builder, null, null, (serviceProvider, config, options) =>
{
var provider = CreateNLogLoggerProvider(serviceProvider, config, options);
@@ -167,6 +174,8 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, string confi
/// ILoggingBuilder for chaining
public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, NLogProviderOptions options, Func factoryBuilder)
{
+ Guard.ThrowIfNull(builder);
+ Guard.ThrowIfNull(factoryBuilder);
AddNLogLoggerProvider(builder, null, options, (serviceProvider, config, options) =>
{
serviceProvider.SetupNLogConfigSettings(config, LogManager.LogFactory);
@@ -187,6 +196,8 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, NLogProvider
/// ILoggingBuilder for chaining
public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, Func factoryBuilder)
{
+ Guard.ThrowIfNull(builder);
+ Guard.ThrowIfNull(factoryBuilder);
AddNLogLoggerProvider(builder, null, null, (serviceProvider, config, options) =>
{
serviceProvider.SetupNLogConfigSettings(config, LogManager.LogFactory);
@@ -240,7 +251,7 @@ public static LoggingConfiguration ConfigureNLog(this ILoggerFactory loggerFacto
///
public static NLogLoggerProvider Configure(this NLogLoggerProvider nlogProvider, IConfigurationSection configurationSection)
{
- if (nlogProvider == null || configurationSection == null)
+ if (nlogProvider is null || configurationSection is null)
return nlogProvider;
Configure(nlogProvider.Options, configurationSection);
@@ -257,7 +268,7 @@ public static NLogProviderOptions Configure(this NLogProviderOptions options, IC
{
options = options ?? NLogProviderOptions.Default;
- if (configurationSection == null || !(configurationSection.GetChildren()?.Any() ?? false))
+ if (configurationSection is null || !(configurationSection.GetChildren()?.Any() ?? false))
return options;
var configProps = options.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.SetMethod?.IsPublic == true).ToDictionary(p => p.Name, StringComparer.OrdinalIgnoreCase);
diff --git a/src/NLog.Extensions.Logging/Internal/Guard.cs b/src/NLog.Extensions.Logging/Internal/Guard.cs
new file mode 100644
index 00000000..d9ba2ded
--- /dev/null
+++ b/src/NLog.Extensions.Logging/Internal/Guard.cs
@@ -0,0 +1,69 @@
+//
+// Copyright (c) 2004-2021 Jaroslaw Kowalski , 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.
+//
+
+#if !NETCOREAPP3_1_OR_GREATER
+namespace System.Runtime.CompilerServices
+{
+ [AttributeUsage(AttributeTargets.Parameter)]
+ sealed class CallerArgumentExpressionAttribute : Attribute
+ {
+ public CallerArgumentExpressionAttribute(string param)
+ {
+ Param = param;
+ }
+
+ public string Param { get; }
+ }
+}
+#endif
+
+namespace NLog.Extensions.Logging
+{
+ using System;
+ using System.Runtime.CompilerServices;
+ internal static class Guard
+ {
+ internal static T ThrowIfNull(
+ T arg,
+ [CallerArgumentExpression("arg")] string param = "")
+ where T : class
+ {
+ if (arg is null)
+ {
+ throw new ArgumentNullException(string.IsNullOrEmpty(param) ? typeof(T).Name : param);
+ }
+
+ return arg;
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/NLog.Extensions.Logging/Internal/RegisterNLogLoggingProvider.cs b/src/NLog.Extensions.Logging/Internal/RegisterNLogLoggingProvider.cs
index d9009f30..317992d1 100644
--- a/src/NLog.Extensions.Logging/Internal/RegisterNLogLoggingProvider.cs
+++ b/src/NLog.Extensions.Logging/Internal/RegisterNLogLoggingProvider.cs
@@ -65,7 +65,7 @@ internal static NLogLoggerProvider CreateNLogLoggerProvider(this IServiceProvide
var configuration = serviceProvider.SetupNLogConfigSettings(hostConfiguration, provider.LogFactory);
- if (configuration != null && (!ReferenceEquals(configuration, hostConfiguration) || options == null))
+ if (configuration != null && (!ReferenceEquals(configuration, hostConfiguration) || options is null))
{
provider.Configure(configuration.GetSection("Logging:NLog"));
}
diff --git a/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs b/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs
index 67b4dc11..c9f02b69 100644
--- a/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs
+++ b/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs
@@ -165,12 +165,12 @@ public static IDisposable CaptureScopeProperties(IEnumerable scopePropertyCollec
var keyValueExtractor = default(KeyValuePair, Func