Skip to content

Commit

Permalink
Configure NLogProviderOptions from appsettings.json
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Jan 13, 2019
1 parent 8f2a7c4 commit 9e7f343
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 3,159 deletions.
8 changes: 6 additions & 2 deletions examples/NetCore2/ConsoleExample/ConsoleExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.0" />
<PackageReference Include="NLog.Schema" Version="4.5.11" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\NLog.Extensions.Logging\NLog.Extensions.Logging.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="nlog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
3,106 changes: 0 additions & 3,106 deletions examples/NetCore2/ConsoleExample/NLog.xsd

This file was deleted.

31 changes: 17 additions & 14 deletions examples/NetCore2/ConsoleExample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Extensions.Logging;
using System;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

namespace ConsoleExample
{
Expand Down Expand Up @@ -44,21 +44,20 @@ private static IServiceProvider BuildDi()
// Runner is the custom class
services.AddTransient<Runner>();

services.AddSingleton<ILoggerFactory, LoggerFactory>();
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Trace));
var config = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();

var serviceProvider = services.BuildServiceProvider();

var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();

// configure NLog
loggerFactory.AddNLog(new NLogProviderOptions
// configure Logging with NLog
services.AddLogging(loggingBuilder =>
{
CaptureMessageTemplates = true,
CaptureMessageProperties = true
loggingBuilder.ClearProviders();
loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
loggingBuilder.AddNLog(config);
});

var serviceProvider = services.BuildServiceProvider();
return serviceProvider;
}
}
Expand All @@ -75,6 +74,10 @@ public Runner(ILogger<Runner> logger)
public void DoAction(string name)
{
_logger.LogDebug(20, "Doing hard work! {Action}", name);
_logger.LogInformation(21, "Doing hard work! {Action}", name);
_logger.LogWarning(22, "Doing hard work! {Action}", name);
_logger.LogError(23, "Doing hard work! {Action}", name);
_logger.LogCritical(24, "Doing hard work! {Action}", name);
}
}
}
9 changes: 9 additions & 0 deletions examples/NetCore2/ConsoleExample/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"NLog": {
"IncludeScopes": false,
"ParseMessageTemplates": true,
"CaptureMessageProperties": true
}
}
}
12 changes: 11 additions & 1 deletion src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,17 @@ public static IHostBuilder UseNLog(this IHostBuilder builder, NLogProviderOption
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(ConfigureExtensions).GetTypeInfo()
.Assembly);

services.AddSingleton(new LoggerFactory().AddNLog(options));
services.AddSingleton<ILoggerProvider>(serviceProvider =>
{
var provider = new NLogLoggerProvider(options ?? new NLogProviderOptions());
if (hostbuilder.Configuration != null)
{
// TODO ConfigSettingLayoutRenderer.DefaultConfiguration = hostbuilder.Configuration;
if (options == null)
provider.ConfigureNLogProvider(hostbuilder.Configuration?.GetSection("Logging:NLog"));
}
return provider;
});
});

return builder;
Expand Down
5 changes: 2 additions & 3 deletions src/NLog.Extensions.Hosting/NLog.Extensions.Hosting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
<DebugType Condition=" '$(Configuration)' == 'Debug' ">Full</DebugType>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

Expand All @@ -28,7 +27,7 @@
<AssemblyOriginatorKeyFile>..\NLog.snk</AssemblyOriginatorKeyFile>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.1.0" />
</ItemGroup>
Expand Down
85 changes: 78 additions & 7 deletions src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
using System;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using NLog.Common;
using NLog.Config;

namespace NLog.Extensions.Logging
{
/// <summary>
/// Helpers for .NET Core
/// Helpers for configuring NLog for Microsoft Extension Logging (MEL)
/// </summary>
public static class ConfigureExtensions
{
/// <summary>
/// Enable NLog as logging provider in .NET Core.
/// Enable NLog as logging provider for Microsoft Extension Logging
/// </summary>
/// <param name="factory"></param>
/// <returns>ILoggerFactory for chaining</returns>
public static ILoggerFactory AddNLog(this ILoggerFactory factory)
{
return AddNLog(factory, null);
return factory.AddNLog(NLogProviderOptions.Default);
}

/// <summary>
/// Enable NLog as logging provider in .NET Core.
/// Enable NLog as logging provider for Microsoft Extension Logging
/// </summary>
/// <param name="factory"></param>
/// <param name="options">NLog options</param>
Expand All @@ -33,19 +35,45 @@ public static ILoggerFactory AddNLog(this ILoggerFactory factory, NLogProviderOp
return factory;
}

/// <summary>
/// Enable NLog as logging provider for Microsoft Extension Logging
/// </summary>
/// <param name="factory"></param>
/// <param name="configuration"></param>
/// <returns>ILoggerFactory for chaining</returns>
public static ILoggerFactory AddNLog(this ILoggerFactory factory, IConfiguration configuration)
{
var provider = CreateNLogProvider(configuration);
factory.AddProvider(provider);
return factory;
}

#if !NETCORE1_0
/// <summary>
/// Enable NLog as logging provider in .NET Core.
/// Enable NLog as logging provider for Microsoft Extension Logging
/// </summary>
/// <param name="factory"></param>
/// <returns>ILoggerFactory for chaining</returns>
public static ILoggingBuilder AddNLog(this ILoggingBuilder factory)
{
return AddNLog(factory, null);
return factory.AddNLog(NLogProviderOptions.Default);
}

/// <summary>
/// Enable NLog as logging provider in .NET Core.
/// Enable NLog as logging provider for Microsoft Extension Logging
/// </summary>
/// <param name="factory"></param>
/// <param name="configuration">Configuration</param>
/// <returns>ILoggerFactory for chaining</returns>
public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, IConfiguration configuration)
{
var provider = CreateNLogProvider(configuration);
factory.AddProvider(provider);
return factory;
}

/// <summary>
/// Enable NLog as logging provider for Microsoft Extension Logging
/// </summary>
/// <param name="factory"></param>
/// <param name="options">NLog options</param>
Expand Down Expand Up @@ -83,5 +111,48 @@ public static LoggingConfiguration ConfigureNLog(this ILoggerFactory loggerFacto
LogManager.Configuration = config;
return config;
}

/// <summary>
/// Factory method for <see cref="NLogLoggerProvider"/>
/// </summary>
/// <param name="nlogProvider"></param>
/// <param name="configurationSection">Microsoft Extension Configuration</param>
/// <returns></returns>
public static NLogLoggerProvider ConfigureNLogProvider(this NLogLoggerProvider nlogProvider, IConfigurationSection configurationSection)
{
if (configurationSection == null)
return nlogProvider;

var configProps = nlogProvider.Options.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.SetMethod?.IsPublic == true).ToDictionary(p => p.Name, StringComparer.OrdinalIgnoreCase);
foreach (var configValue in configurationSection.GetChildren())
{
if (configProps.TryGetValue(configValue.Key, out var propertyInfo))
{
try
{
var result = Convert.ChangeType(configValue.Value, propertyInfo.PropertyType);
propertyInfo.SetMethod.Invoke(nlogProvider.Options, new[] { result });
}
catch (Exception ex)
{
InternalLogger.Warn(ex, "NLogProviderOptions: Property {0} could not be assigned value: {1}", configValue.Key, configValue.Value);
}
}
}

return nlogProvider;
}

private static NLogLoggerProvider CreateNLogProvider(IConfiguration configuration)
{
var provider = new NLogLoggerProvider(new NLogProviderOptions());
if (configuration != null)
{
// TODO ConfigSettingLayoutRenderer.DefaultConfiguration = configuration;
provider.ConfigureNLogProvider(configuration.GetSection("Logging:NLog"));
}

return provider;
}
}
}
23 changes: 13 additions & 10 deletions src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>

<TargetFrameworks>net451;net461;netstandard1.3;netstandard1.5;netstandard2.0</TargetFrameworks>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
<DebugType Condition=" '$(Configuration)' == 'Debug' ">Full</DebugType>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

Expand All @@ -17,7 +16,7 @@

For ASP.NET Core, use NLog.Web.AspNetCore: https://www.nuget.org/packages/NLog.Web.AspNetCore
</Description>
<PackageTags>NLog;Microsoft.Extensions.Logging;log;logfiles;netcore;nlog</PackageTags>
<PackageTags>NLog;Microsoft.Extensions.Logging;log;logfiles;netcore</PackageTags>
<PackageReleaseNotes>

- Fixed null-reference-exception when parsing empty scope-property collection (@snakefoot)
Expand Down Expand Up @@ -63,29 +62,33 @@ Full changelog: https://github.com/NLog/NLog.Extensions.Logging/blob/master/CHAN
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="[4.5.10,5.0.0-beta01)" />
<PackageReference Include="NLog" Version="[4.5.11,5.0.0-beta01)" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="1.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.0.0" />
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.0" />
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="1.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.0.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.5' ">
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="1.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.0.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.0" />
</ItemGroup>

<PropertyGroup>
<AssemblyTitle>$(Title)</AssemblyTitle>
</PropertyGroup>
</Project>

49 changes: 38 additions & 11 deletions test/NLog.Extensions.Hosting.Tests/ExtensionMethodTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
Expand All @@ -10,25 +11,51 @@ namespace NLog.Extensions.Hosting.Tests
public class ExtensionMethodTests
{
[Fact]
public void UseNLog_noParams_buildsAndExposesValidIServiceCollectiont()
public void UseNLog_noParams_WorksWithNLog()
{
var actual = new HostBuilder().UseNLog().Build();
var serviceCount = actual.Services.GetServices<ILoggerFactory>().Count();

Assert.NotNull(actual);
Assert.Equal(2, serviceCount);
TestHostingResult(actual);
}

[Fact]
public void UseNLog_withOptionsParam_buildsAndExposesValidIServiceCollection()
public void UseNLog_withOptionsParam_WorksWithNLog()
{
var someParam = new NLogProviderOptions {CaptureMessageProperties = false, CaptureMessageTemplates = false};

var actual = new HostBuilder().UseNLog(someParam).Build();
var serviceCount = actual.Services.GetServices<ILoggerFactory>().Count();
TestHostingResult(actual);
}

[Fact]
public void UseNLog_withConfiguration_WorksWithNLog()
{
var memoryConfig = new Dictionary<string, string>();
memoryConfig["NLog:CaptureMessageProperties"] = "true";
memoryConfig["NLog:CaptureMessageTemplates"] = "false";
memoryConfig["NLog:IgnoreScopes"] = "false";

var someParam = new NLogProviderOptions { CaptureMessageProperties = false, CaptureMessageTemplates = false };
var actual = new HostBuilder().ConfigureHostConfiguration(config => config.AddInMemoryCollection(memoryConfig)).UseNLog(someParam).Build();
TestHostingResult(actual);
}

private static void TestHostingResult(IHost host)
{
try
{
var nlogTarget = new Targets.MemoryTarget() { Name = "Output" };
Config.SimpleConfigurator.ConfigureForTargetLogging(nlogTarget);

var loggerFactory = host.Services.GetService<ILoggerFactory>();
Assert.NotNull(loggerFactory);

Assert.NotNull(actual);
Assert.Equal(2, serviceCount);
var logger = loggerFactory.CreateLogger("Hello");
logger.LogError("World");
Assert.NotEmpty(nlogTarget.Logs);
}
finally
{
LogManager.Configuration = null;
}
}
}
}
Loading

0 comments on commit 9e7f343

Please sign in to comment.