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

ASP.NET Core: Improved ConfigureNLog for better out-of-the-box experience #268

Merged
merged 1 commit into from
Apr 2, 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
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm maybe this needs a minor version change, because of changed api usage.

(4.6 instead of 4.5.1)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the two bug-issues has shown, then 4.5.0 is not working that well out of the box (actually very broken). Think 4.5.1 would be fine. Unless you have a good idea for how to pre-register when AspNetCore?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks, NLog.LogManager.LoadConfiguration still works of course (in the same bugged way)

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();
}
}