-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced NLogWebLayoutRenderer for caching of IHttpContextAccessor (#…
…659)
- Loading branch information
1 parent
4430692
commit 62e3335
Showing
4 changed files
with
60 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
#if ASP_NET_CORE | ||
using Microsoft.AspNetCore.Http; | ||
using HttpContextBase = Microsoft.AspNetCore.Http.HttpContext; | ||
#else | ||
using System.Web; | ||
#endif | ||
using NLog.Config; | ||
using NLog.LayoutRenderers; | ||
|
||
namespace NLog.Web.LayoutRenderers | ||
{ | ||
/// <summary> | ||
/// Specialized layout render which has a cached <see cref="IHttpContextAccessor"/> | ||
/// </summary> | ||
internal class NLogWebLayoutRenderer : FuncLayoutRenderer | ||
{ | ||
readonly Func<LogEventInfo, HttpContextBase, LoggingConfiguration, object> _func; | ||
|
||
#if !ASP_NET_CORE | ||
|
||
private static IHttpContextAccessor HttpContextAccessor { get; } = AspNetLayoutRendererBase.DefaultHttpContextAccessor; | ||
#else | ||
|
||
private IHttpContextAccessor _httpContextAccessor; | ||
public IHttpContextAccessor HttpContextAccessor | ||
{ | ||
get => _httpContextAccessor ?? (_httpContextAccessor = AspNetLayoutRendererBase.RetrieveHttpContextAccessor(GetType())); | ||
set => _httpContextAccessor = value; | ||
} | ||
#endif | ||
|
||
public NLogWebLayoutRenderer(string name, Func<LogEventInfo, HttpContextBase, LoggingConfiguration, object> func) : base(name) | ||
{ | ||
_func = func; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override object RenderValue(LogEventInfo logEvent) | ||
{ | ||
var httpContext = HttpContextAccessor?.HttpContext; | ||
return _func(logEvent, httpContext, LoggingConfiguration); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters