Skip to content

Commit

Permalink
Merge pull request #141 from NLog/Improve-performance2
Browse files Browse the repository at this point in the history
small performance improvements for ServiceLocator
  • Loading branch information
304NotModified committed May 13, 2017
2 parents e8fc87a + cb4e116 commit 68dde7a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,25 @@ 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<IHostingEnvironment>());

/// <summary>
/// Append to target
/// </summary>
/// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param>
/// <param name="logEvent">Logging event.</param>
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
var env = ServiceLocator.ServiceProvider?.GetService<IHostingEnvironment>();
builder.Append(env?.EnvironmentName);
builder.Append(HostingEnvironment?.EnvironmentName);
}

/// <inheritdoc />
protected override void CloseLayoutRenderer()
{
_hostingEnvironment = null;
base.CloseLayoutRenderer();
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions NLog.Web.AspNetCore/LayoutRenderers/AspNetLayoutRendererBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent)
/// <param name="logEvent">Logging event.</param>
protected abstract void DoAppend(StringBuilder builder, LogEventInfo logEvent);

#if NETSTANDARD_1plus

/// <inheritdoc />
protected override void CloseLayoutRenderer()
{
_httpContextAccessor = null;
base.CloseLayoutRenderer();
}
#endif


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<IHostingEnvironment>());
#endif

/// <summary>
/// Append to target
/// </summary>
Expand All @@ -35,13 +41,21 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent)


#if NETSTANDARD_1plus
var env = ServiceLocator.ServiceProvider?.GetService<IHostingEnvironment>();
builder.Append(env?.ApplicationName);
builder.Append(HostingEnvironment?.ApplicationName);

#else
builder.Append(HostingEnvironment.SiteName);
#endif

}
#if NETSTANDARD_1plus

/// <inheritdoc />
protected override void CloseLayoutRenderer()
{
_hostingEnvironment = null;
base.CloseLayoutRenderer();
}
#endif
}
}

0 comments on commit 68dde7a

Please sign in to comment.