From 72b805cbce7a3d1b6a59b1d86c20b6a9e4be555b Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Sun, 1 Apr 2018 21:39:16 +0200 Subject: [PATCH] NLogBuilder is needed, because DLL autoload doesn't work unless the DLL has been loaded. --- NLog.Web.AspNetCore/AspNetExtensions.cs | 20 +++++++++++++++++++ NLog.Web.AspNetCore/NLogBuilder.cs | 10 +++------- .../ASP.NET Core 2 - VS2017/Program.cs | 4 ++-- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/NLog.Web.AspNetCore/AspNetExtensions.cs b/NLog.Web.AspNetCore/AspNetExtensions.cs index e30120e5..052999c8 100644 --- a/NLog.Web.AspNetCore/AspNetExtensions.cs +++ b/NLog.Web.AspNetCore/AspNetExtensions.cs @@ -116,9 +116,29 @@ public static IWebHostBuilder UseNLog(this IWebHostBuilder builder, NLogAspNetCo { services.TryAddSingleton(); } + + RegisterHiddenAssembliesForCallSite(); }); return builder; } + + private static void RegisterHiddenAssembliesForCallSite() + { + var allAssemblies = AppDomain.CurrentDomain.GetAssemblies(); + foreach (var assembly in allAssemblies) + { + if ( assembly.FullName.StartsWith("NLog.Extensions.Logging,", StringComparison.OrdinalIgnoreCase) + || assembly.FullName.StartsWith("NLog.Web,", StringComparison.OrdinalIgnoreCase) + || assembly.FullName.StartsWith("NLog.Web.AspNetCore,", StringComparison.OrdinalIgnoreCase) + || assembly.FullName.StartsWith("Microsoft.Extensions.Logging,", StringComparison.OrdinalIgnoreCase) + || assembly.FullName.StartsWith("Microsoft.Extensions.Logging.Abstractions,", StringComparison.OrdinalIgnoreCase) + || assembly.FullName.StartsWith("Microsoft.Extensions.Logging.Filter,", StringComparison.OrdinalIgnoreCase) + || assembly.FullName.StartsWith("Microsoft.Logging,", StringComparison.OrdinalIgnoreCase)) + { + LogManager.AddHiddenAssembly(assembly); + } + } + } #endif } diff --git a/NLog.Web.AspNetCore/NLogBuilder.cs b/NLog.Web.AspNetCore/NLogBuilder.cs index f86326be..b6cbee53 100644 --- a/NLog.Web.AspNetCore/NLogBuilder.cs +++ b/NLog.Web.AspNetCore/NLogBuilder.cs @@ -1,11 +1,7 @@ -#if ASP_NET_CORE2 - +#if ASP_NET_CORE using System; -using System.Collections.Generic; using System.Reflection; -using NLog.Common; using NLog.Config; -using NLog.Extensions.Logging; namespace NLog.Web { @@ -19,9 +15,9 @@ public static class NLogBuilder /// /// Path to NLog configuration file, e.g. nlog.config. > /// LogFactory to get loggers, add events etc - [Obsolete("Instead use NLog.LogManager.LoadConfiguration()")] public static LogFactory ConfigureNLog(string configFileName) { + ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); return LogManager.LoadConfiguration(configFileName); } @@ -30,9 +26,9 @@ public static LogFactory ConfigureNLog(string configFileName) /// /// Config for NLog /// LogFactory to get loggers, add events etc - [Obsolete("Instead assign property NLog.LogManager.Configuration")] public static LogFactory ConfigureNLog(LoggingConfiguration configuration) { + ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); LogManager.Configuration = configuration; return LogManager.LogFactory; } diff --git a/examples/ASP.NET Core 2/Visual Studio 2017/ASP.NET Core 2 - VS2017/Program.cs b/examples/ASP.NET Core 2/Visual Studio 2017/ASP.NET Core 2 - VS2017/Program.cs index 8164c744..a7cc1287 100644 --- a/examples/ASP.NET Core 2/Visual Studio 2017/ASP.NET Core 2 - VS2017/Program.cs +++ b/examples/ASP.NET Core 2/Visual Studio 2017/ASP.NET Core 2 - VS2017/Program.cs @@ -11,7 +11,7 @@ public static class Program { public static void Main(string[] args) { - var logger = NLog.LogManager.LoadConfiguration("nlog.config").GetCurrentClassLogger(); + var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); try { logger.Debug("init main"); @@ -38,7 +38,7 @@ public static IWebHost BuildWebHost(string[] args) => logging.ClearProviders(); logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); }) - .UseNLog() // NLog: setup NLog for Dependency injection + .UseNLog() // NLog: Setup NLog for Dependency injection .Build(); } }