Skip to content

Commit

Permalink
Merge pull request #233 from NLog/safe-hide-assembly
Browse files Browse the repository at this point in the history
Better hide assemblies for ${callsite}
  • Loading branch information
304NotModified authored Dec 5, 2017
2 parents 3ace8c5 + f657728 commit 8ad6d8e
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions NLog.Web.AspNetCore/NLogBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,30 @@ public static LogFactory ConfigureNLog(LoggingConfiguration configuration)
private static void ConfigureHiddenAssemblies()
{
//ignore this
LogManager.AddHiddenAssembly(Assembly.Load(new AssemblyName("Microsoft.Extensions.Logging")));
LogManager.AddHiddenAssembly(Assembly.Load(new AssemblyName("Microsoft.Extensions.Logging.Abstractions")));
SafeAddHiddenAssembly("Microsoft.Extensions.Logging");
SafeAddHiddenAssembly("Microsoft.Extensions.Logging.Abstractions");

//try the Filter ext, this one is not mandatory so could fail
SafeAddHiddenAssembly("Microsoft.Extensions.Logging.Filter", false);

LogManager.AddHiddenAssembly(typeof(ConfigureExtensions).GetTypeInfo().Assembly);
}

private static void SafeAddHiddenAssembly(string assemblyName, bool logOnException = true)
{
try
{
//try the Filter ext
var filterAssembly = Assembly.Load(new AssemblyName("Microsoft.Extensions.Logging.Filter"));
LogManager.AddHiddenAssembly(filterAssembly);
InternalLogger.Trace("Hide {0}", assemblyName);
var assembly = Assembly.Load(new AssemblyName(assemblyName));
LogManager.AddHiddenAssembly(assembly);
}
catch (Exception)
catch (Exception ex)
{
//ignore
if (logOnException)
{
InternalLogger.Debug(ex, "Hiding assembly {0} failed. This could influence the ${callsite}", assemblyName);
}
}

LogManager.AddHiddenAssembly(typeof(ConfigureExtensions).GetTypeInfo().Assembly);
}
}
}
Expand Down

0 comments on commit 8ad6d8e

Please sign in to comment.