From cb4e11691f49b6aa15c16d2845ac3c5a63d0c842 Mon Sep 17 00:00:00 2001 From: Julian Verdurmen <304NotModified@users.noreply.github.com> Date: Sat, 13 May 2017 16:55:24 +0200 Subject: [PATCH] small performance improvements for ServiceLocator --- .../AspNetEnvironmentLayoutRenderer.cs | 14 ++++++++++++-- .../AspNetLayoutRendererBase.cs | 10 ++++++++++ .../IISInstanceNameLayoutRenderer.cs | 18 ++++++++++++++++-- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AspNetEnvironmentLayoutRenderer.cs b/NLog.Web.AspNetCore/LayoutRenderers/AspNetEnvironmentLayoutRenderer.cs index 2165c959..2356f32d 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AspNetEnvironmentLayoutRenderer.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AspNetEnvironmentLayoutRenderer.cs @@ -16,6 +16,10 @@ namespace NLog.Web.LayoutRenderers // ReSharper disable once InconsistentNaming public class AspNetEnvironmentLayoutRenderer : LayoutRenderer { + private static IHostingEnvironment _hostingEnvironment; + + private static IHostingEnvironment HostingEnvironment => _hostingEnvironment ?? (_hostingEnvironment = ServiceLocator.ServiceProvider?.GetService()); + /// /// Append to target /// @@ -23,8 +27,14 @@ public class AspNetEnvironmentLayoutRenderer : LayoutRenderer /// Logging event. protected override void Append(StringBuilder builder, LogEventInfo logEvent) { - var env = ServiceLocator.ServiceProvider?.GetService(); - builder.Append(env?.EnvironmentName); + builder.Append(HostingEnvironment?.EnvironmentName); + } + + /// + protected override void CloseLayoutRenderer() + { + _hostingEnvironment = null; + base.CloseLayoutRenderer(); } } } diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AspNetLayoutRendererBase.cs b/NLog.Web.AspNetCore/LayoutRenderers/AspNetLayoutRendererBase.cs index b2b35e17..4411e096 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AspNetLayoutRendererBase.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AspNetLayoutRendererBase.cs @@ -85,6 +85,16 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent) /// Logging event. protected abstract void DoAppend(StringBuilder builder, LogEventInfo logEvent); +#if NETSTANDARD_1plus + +/// + protected override void CloseLayoutRenderer() + { + _httpContextAccessor = null; + base.CloseLayoutRenderer(); + } +#endif + } } diff --git a/NLog.Web.AspNetCore/LayoutRenderers/IISInstanceNameLayoutRenderer.cs b/NLog.Web.AspNetCore/LayoutRenderers/IISInstanceNameLayoutRenderer.cs index 94e9b656..56d21bb4 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/IISInstanceNameLayoutRenderer.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/IISInstanceNameLayoutRenderer.cs @@ -25,6 +25,12 @@ namespace NLog.Web.LayoutRenderers // ReSharper disable once InconsistentNaming public class IISInstanceNameLayoutRenderer : LayoutRenderer { +#if NETSTANDARD_1plus + private static IHostingEnvironment _hostingEnvironment; + + private static IHostingEnvironment HostingEnvironment => _hostingEnvironment ?? (_hostingEnvironment = ServiceLocator.ServiceProvider?.GetService()); +#endif + /// /// Append to target /// @@ -35,13 +41,21 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent) #if NETSTANDARD_1plus - var env = ServiceLocator.ServiceProvider?.GetService(); - builder.Append(env?.ApplicationName); + builder.Append(HostingEnvironment?.ApplicationName); #else builder.Append(HostingEnvironment.SiteName); #endif } +#if NETSTANDARD_1plus + + /// + protected override void CloseLayoutRenderer() + { + _hostingEnvironment = null; + base.CloseLayoutRenderer(); + } +#endif } }