-
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.
Merge pull request #143 from NLog/aspnet-user-isAuthenticated
added ${aspnet-user-isAuthenticated}
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
NLog.Web.AspNetCore/LayoutRenderers/AspNetUserIsAuthenticatedLayoutRenderer.cs
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,40 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using NLog.LayoutRenderers; | ||
using NLog.Web.LayoutRenderers; | ||
|
||
namespace NLog.Web.AspNetCore.LayoutRenderers | ||
{ | ||
/// <summary> | ||
/// Is the user authenticated? 0 = not authenticated, 1 = authenticated | ||
/// | ||
/// ${aspnet-user-isAuthenticated} | ||
/// </summary> | ||
[LayoutRenderer("aspnet-user-isAuthenticated")] | ||
public class AspNetUserIsAuthenticatedLayoutRenderer : AspNetLayoutRendererBase | ||
{ | ||
/// <summary> | ||
/// Render 0 or 1 | ||
/// </summary> | ||
/// <param name="builder"></param> | ||
/// <param name="logEvent"></param> | ||
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) | ||
{ | ||
var httpContext = HttpContextAccessor.HttpContext; | ||
if (httpContext == null) | ||
{ | ||
return; | ||
} | ||
|
||
if (httpContext.User?.Identity?.IsAuthenticated == true) | ||
{ | ||
builder.Append(1); | ||
} | ||
else | ||
{ | ||
builder.Append(0); | ||
} | ||
} | ||
} | ||
} |
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