Skip to content

Commit

Permalink
Merge pull request #283 from snakefoot/master
Browse files Browse the repository at this point in the history
Avoid assembly lookup on static class initialization
  • Loading branch information
304NotModified authored Apr 29, 2018
2 parents 15958cc + a0b185c commit 44f29fe
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ namespace NLog.Web
/// </summary>
public static class AspNetExtensions
{
private static readonly Assembly _nlogWebAssembly = typeof(AspNetExtensions).GetTypeInfo().Assembly;

/// <summary>
/// Enable NLog Web for ASP.NET Core.
/// </summary>
Expand All @@ -43,8 +41,8 @@ public static void AddNLogWeb(this IApplicationBuilder app)
/// <returns>LoggingConfiguration for chaining</returns>
public static LoggingConfiguration ConfigureNLog(this IHostingEnvironment env, string configFileRelativePath)
{
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(_nlogWebAssembly);
LogManager.AddHiddenAssembly(_nlogWebAssembly);
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
var fileName = Path.Combine(env.ContentRootPath, configFileRelativePath);
LogManager.LoadConfiguration(fileName);
return LogManager.Configuration;
Expand All @@ -63,7 +61,7 @@ public static LoggingConfiguration ConfigureNLog(this IHostingEnvironment env, s
[Obsolete("Use UseNLog() on IWebHostBuilder, and NLog.Web.NLogBuilder.ConfigureNLog()")]
public static LogFactory ConfigureNLog(this ILoggingBuilder builder, string configFileName)
{
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(_nlogWebAssembly);
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
builder.AddNLog();
return LogManager.LoadConfiguration(configFileName);
}
Expand All @@ -79,7 +77,7 @@ public static LogFactory ConfigureNLog(this ILoggingBuilder builder, string conf
[Obsolete("Use UseNLog() on IWebHostBuilder, and NLog.Web.NLogBuilder.ConfigureNLog()")]
public static LogFactory ConfigureNLog(this ILoggingBuilder builder, LoggingConfiguration configuration)
{
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(_nlogWebAssembly);
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
builder.AddNLog();
LogManager.Configuration = configuration;
return LogManager.LogFactory;
Expand All @@ -104,11 +102,11 @@ public static IWebHostBuilder UseNLog(this IWebHostBuilder builder, NLogAspNetCo
if (builder == null) throw new ArgumentNullException(nameof(builder));
options = options ?? NLogAspNetCoreOptions.Default;

ConfigurationItemFactory.Default.RegisterItemsFromAssembly(_nlogWebAssembly);
LogManager.AddHiddenAssembly(_nlogWebAssembly);

builder.ConfigureServices(services =>
{
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);

//note: when registering ILoggerFactory, all non NLog stuff and stuff before this will be removed
services.AddSingleton<ILoggerProvider>(serviceProvider =>
{
Expand Down Expand Up @@ -137,8 +135,8 @@ public static IWebHostBuilder UseNLog(this IWebHostBuilder builder, NLogAspNetCo
public static IServiceProvider SetupNLogServiceLocator(this IServiceProvider serviceProvider)
{
ServiceLocator.ServiceProvider = serviceProvider;
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(_nlogWebAssembly);
LogManager.AddHiddenAssembly(_nlogWebAssembly);
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
return serviceProvider;
}
}
Expand Down

0 comments on commit 44f29fe

Please sign in to comment.