Skip to content

Commit

Permalink
LogManager.Setup() - SetupBuilderExtension with LoadConfigurationFrom…
Browse files Browse the repository at this point in the history
…AppSettings
  • Loading branch information
snakefoot committed May 24, 2020
1 parent 04aeacd commit 6c1cb1b
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using NLog.Web;

namespace NLog.Web.AspNetCore2.Example
Expand All @@ -11,7 +12,13 @@ public static class Program
{
public static void Main(string[] args)
{
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
var config = new ConfigurationBuilder().Build();

var logger = LogManager.Setup()
.SetupExtensions(ext => ext.RegisterNLogWeb().RegisterConfigSettings(config))
.LoadConfigurationFromFile("NLog.config")
.GetCurrentClassLogger();

try
{
logger.Debug("init main");
Expand Down
6 changes: 5 additions & 1 deletion examples/ASP.NET Core 3/ASP.NET Core 3 - VS2019/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Web;

namespace ASP.NET_Core_3___VS2019
Expand All @@ -14,7 +15,10 @@ public class Program
{
public static void Main(string[] args)
{
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
var logger = LogManager.Setup()
.LoadConfigurationFromAppSettings()
.GetCurrentClassLogger();

try
{
logger.Debug("init main");
Expand Down
95 changes: 95 additions & 0 deletions src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
#if ASP_NET_CORE3
using System.IO;
#endif
using System.Linq;
using Microsoft.Extensions.Configuration;
using NLog.Config;
using NLog.Extensions.Logging;
using NLog.Web.DependencyInjection;

namespace NLog.Web
{
/// <summary>
/// Extension methods to setup LogFactory options
/// </summary>
public static class SetupBuilderExtensions
{
#if ASP_NET_CORE3
/// <summary>
/// Loads NLog LoggingConfiguration from appsettings.json from the NLog-section
/// </summary>
public static ISetupBuilder LoadConfigurationFromAppSettings(this ISetupBuilder setupBuilder, string basePath = null, string environment = null, string nlogConfigSection = "NLog", bool optional = true, bool reloadOnChange = false)
{
environment = environment ?? GetAspNetCoreEnvironment("DOTNET_ENVIRONMENT") ?? GetAspNetCoreEnvironment("ASPNETCORE_ENVIRONMENT") ?? "Production";

var builder = new ConfigurationBuilder()
// Host Configuration
.SetBasePath(basePath ?? Directory.GetCurrentDirectory())
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
.AddEnvironmentVariables(prefix: "DOTNET_")
// App Configuration
.AddJsonFile("appsettings.json", optional, reloadOnChange)
.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: reloadOnChange)
.AddEnvironmentVariables();

var config = builder.Build();
if (!string.IsNullOrEmpty(nlogConfigSection) && config.GetSection(nlogConfigSection)?.GetChildren().Any() == true)
{
return setupBuilder.SetupExtensions(e => e.AutoLoadAssemblies(false).RegisterNLogWeb()).LoadConfigurationFromSection(config, nlogConfigSection);
}
else
{
setupBuilder.SetupExtensions(e => e.AutoLoadAssemblies(false).RegisterConfigSettings(config).RegisterNLogWeb());

if (!string.IsNullOrEmpty(basePath))
{
if (!string.IsNullOrEmpty(environment))
{
setupBuilder.LoadConfigurationFromFile(Path.Combine(basePath, $"NLog.{environment}.config"), optional: true);
}

setupBuilder.LoadConfigurationFromFile(Path.Combine(basePath, "NLog.config"), optional: true);
}
else if (!string.IsNullOrEmpty(environment))
{
setupBuilder.LoadConfigurationFromFile($"NLog.{environment}.config", optional: true);
}

return setupBuilder.LoadConfigurationFromFile(); // No effect, if config already loaded
}
}

private static string GetAspNetCoreEnvironment(string variableName)
{
try
{
var environment = Environment.GetEnvironmentVariable(variableName);
if (string.IsNullOrWhiteSpace(environment))
return null;

return environment.Trim();
}
catch (Exception ex)
{
NLog.Common.InternalLogger.Error(ex, "Failed to lookup environment variable {0}", variableName);
return null;
}
}
#endif

/// <summary>
/// Convience method to register aspnet-layoutrenders in NLog.Web as one-liner before loading NLog.config
/// </summary>
/// <remarks>
/// If not providing <paramref name="serviceProvider"/>, then output from aspnet-layoutrenderers will remain empty
/// </remarks>
public static ISetupBuilder RegisterNLogWeb(this ISetupBuilder setupBuilder, IServiceProvider serviceProvider = null)
{
setupBuilder.SetupExtensions(s => s.RegisterNLogWeb());
if (serviceProvider != null)
ServiceLocator.ServiceProvider = serviceProvider;
return setupBuilder;
}
}
}
30 changes: 30 additions & 0 deletions src/NLog.Web.AspNetCore/Config/SetupExtensionsBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Reflection;
using Microsoft.Extensions.Configuration;
using NLog.Config;
using NLog.Extensions.Logging;

namespace NLog.Web
{
/// <summary>
/// Extension methods to setup NLog extensions, so they are known when loading NLog LoggingConfiguration
/// </summary>
public static class SetupExtensionsBuilderExtensions
{
/// <summary>
/// Replace with version from NLog.Extension.Logging when it has been released with NLog 4.7
/// </summary>
internal static ISetupExtensionsBuilder RegisterConfigSettings(this ISetupExtensionsBuilder setupBuilder, IConfiguration configuration)
{
ConfigSettingLayoutRenderer.DefaultConfiguration = configuration;
return setupBuilder.RegisterLayoutRenderer<ConfigSettingLayoutRenderer>("configsetting");
}

/// <summary>
/// Register the NLog.Web.AspNetCore LayoutRenderers
/// </summary>
public static ISetupExtensionsBuilder RegisterNLogWeb(this ISetupExtensionsBuilder setupBuilder)
{
return setupBuilder.RegisterAssembly(typeof(NLogAspNetCoreOptions).GetTypeInfo().Assembly);
}
}
}
93 changes: 91 additions & 2 deletions tests/NLog.Web.AspNetCore.Tests/AspNetCoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Castle.Core.Logging;
using Xunit;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -16,7 +15,6 @@
using NLog.Config;
using NLog.Layouts;
using NLog.Targets;
using NLog.Web.Tests.LayoutRenderers;
using ILoggerFactory = Microsoft.Extensions.Logging.ILoggerFactory;

namespace NLog.Web.Tests
Expand Down Expand Up @@ -54,8 +52,99 @@ public void UseNLogShouldLogTest()

Assert.Single(logged);
Assert.Equal("logger1|error1", logged.First());
}

#if ASP_NET_CORE3
[Fact]
public void LoadConfigurationFromAppSettingsShouldLogTest()
{
var tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), nameof(AspNetCoreTests), Guid.NewGuid().ToString()).Replace("\\", "/");
var appSettings = System.IO.Path.Combine(tempPath, "appsettings.json");

try
{
// Arrange
System.IO.Directory.CreateDirectory(tempPath);
System.IO.File.AppendAllText(appSettings, @"{
""basepath"": """ + tempPath + @""",
""NLog"": {
""throwConfigExceptions"": true,
""targets"": {
""logfile"": {
""type"": ""File"",
""fileName"": ""${configsetting:basepath}/hello.txt"",
""layout"": ""${message}""
}
},
""rules"": [
{
""logger"": ""*"",
""minLevel"": ""Debug"",
""writeTo"": ""logfile""
}
]
}
}");

// Act
var logFactory = new LogFactory();
var logger = logFactory.Setup().LoadConfigurationFromAppSettings(basePath: tempPath).GetCurrentClassLogger();
logger.Info("Hello World");

// Assert
var fileOutput = System.IO.File.ReadAllText(System.IO.Path.Combine(tempPath, "hello.txt"));
Assert.Contains("Hello World", fileOutput);
}
finally
{
if (System.IO.Directory.Exists(tempPath))
{
System.IO.Directory.Delete(tempPath, true);
}
}
}

[Fact]
public void LoadConfigurationFromAppSettingsShouldLogTest2()
{
var tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), nameof(AspNetCoreTests), Guid.NewGuid().ToString()).Replace("\\", "/");
var appSettings = System.IO.Path.Combine(tempPath, "appsettings.json");

try
{
// Arrange
System.IO.Directory.CreateDirectory(tempPath);
System.IO.File.AppendAllText(appSettings, @"{
""basepath"": """ + tempPath + @"""
}");

System.IO.File.AppendAllText(System.IO.Path.Combine(tempPath, "NLog.config"), @"<nlog>
<targets>
<target type=""file"" name=""logfile"" layout=""${message}"" fileName=""${configsetting:basepath}/hello.txt"" />
</targets>
<rules>
<logger name=""*"" minLevel=""Debug"" writeTo=""logfile"" />
</rules>
</nlog>");

// Act
var logFactory = new LogFactory();
var logger = logFactory.Setup().LoadConfigurationFromAppSettings(basePath: tempPath).GetCurrentClassLogger();
logger.Info("Hello World");

// Assert
var fileOutput = System.IO.File.ReadAllText(System.IO.Path.Combine(tempPath, "hello.txt"));
Assert.Contains("Hello World", fileOutput);
}
finally
{
if (System.IO.Directory.Exists(tempPath))
{
System.IO.Directory.Delete(tempPath, true);
}
}
}
#endif

private static LoggingConfiguration CreateConfigWithMemoryTarget(out MemoryTarget target, Layout layout)
{
Expand Down

0 comments on commit 6c1cb1b

Please sign in to comment.