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

small performance improvements for ServiceLocator #141

Merged
merged 1 commit into from
May 13, 2017
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
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
}
}