Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added public method to configure the NLog ServiceLocator #273

Merged
merged 2 commits into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ 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 @@ -30,9 +32,7 @@ public static class AspNetExtensions
#endif
public static void AddNLogWeb(this IApplicationBuilder app)
{
ServiceLocator.ServiceProvider = app.ApplicationServices;
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
app.ApplicationServices.SetupNLogServiceLocator();
}

/// <summary>
Expand All @@ -43,8 +43,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(typeof(AspNetExtensions).GetTypeInfo().Assembly);
LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(_nlogWebAssembly);
LogManager.AddHiddenAssembly(_nlogWebAssembly);
var fileName = Path.Combine(env.ContentRootPath, configFileRelativePath);
LogManager.LoadConfiguration(fileName);
return LogManager.Configuration;
Expand All @@ -63,7 +63,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(typeof(AspNetExtensions).GetTypeInfo().Assembly);
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(_nlogWebAssembly);
builder.AddNLog();
return LogManager.LoadConfiguration(configFileName);
}
Expand All @@ -79,7 +79,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(typeof(AspNetExtensions).GetTypeInfo().Assembly);
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(_nlogWebAssembly);
builder.AddNLog();
LogManager.Configuration = configuration;
return LogManager.LogFactory;
Expand All @@ -103,8 +103,9 @@ public static IWebHostBuilder UseNLog(this IWebHostBuilder builder, NLogAspNetCo
{
if (builder == null) throw new ArgumentNullException(nameof(builder));
options = options ?? NLogAspNetCoreOptions.Default;

ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);

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

builder.ConfigureServices(services =>
{
Expand All @@ -125,5 +126,20 @@ public static IWebHostBuilder UseNLog(this IWebHostBuilder builder, NLogAspNetCo
}
#endif

/// <summary>
/// Override the default <see cref="IServiceProvider"/> used by the NLog ServiceLocator.
/// NLog ServiceLocator uses the <see cref="IServiceProvider"/> to access context specific services (Ex. <see cref="IHttpContextAccessor"/>)
/// </summary>
/// <remarks>
/// Should only be used if the standard approach for configuring NLog is not enough
/// </remarks>
/// <param name="serviceProvider"></param>
public static IServiceProvider SetupNLogServiceLocator(this IServiceProvider serviceProvider)
{
ServiceLocator.ServiceProvider = serviceProvider;
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(_nlogWebAssembly);
LogManager.AddHiddenAssembly(_nlogWebAssembly);
return serviceProvider;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ internal static class ServiceLocator
/// The current service provider for reading ASP.NET Core session, request etc.
/// </summary>
public static IServiceProvider ServiceProvider { get; set; }


}
}