Skip to content

Commit

Permalink
Merge pull request #268 from snakefoot/master
Browse files Browse the repository at this point in the history
NLogBuilder is needed, because DLL autoload doesn't work
  • Loading branch information
304NotModified authored Apr 2, 2018
2 parents 0f94df3 + 72b805c commit 1bfddc9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
20 changes: 20 additions & 0 deletions NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,29 @@ public static IWebHostBuilder UseNLog(this IWebHostBuilder builder, NLogAspNetCo
{
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}

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

}
Expand Down
10 changes: 3 additions & 7 deletions NLog.Web.AspNetCore/NLogBuilder.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -19,9 +15,9 @@ public static class NLogBuilder
/// </summary>
/// <param name="configFileName">Path to NLog configuration file, e.g. nlog.config. </param>>
/// <returns>LogFactory to get loggers, add events etc</returns>
[Obsolete("Instead use NLog.LogManager.LoadConfiguration()")]
public static LogFactory ConfigureNLog(string configFileName)
{
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
return LogManager.LoadConfiguration(configFileName);
}

Expand All @@ -30,9 +26,9 @@ public static LogFactory ConfigureNLog(string configFileName)
/// </summary>
/// <param name="configuration">Config for NLog</param>
/// <returns>LogFactory to get loggers, add events etc</returns>
[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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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();
}
}

0 comments on commit 1bfddc9

Please sign in to comment.