Skip to content

Commit

Permalink
Merge pull request #143 from NLog/aspnet-user-isAuthenticated
Browse files Browse the repository at this point in the history
added ${aspnet-user-isAuthenticated}
  • Loading branch information
304NotModified authored May 13, 2017
2 parents 14a5cf9 + 45c2136 commit 3b001e2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
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);
}
}
}
}
2 changes: 2 additions & 0 deletions NLog.Web/NLog.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetSessionValueLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetUserAuthTypeLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetUserIdentityLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetUserIsAuthenticatedLayoutRenderer.cs" />

<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\IISInstanceNameLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetRequestReferrerRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetRequestUrlRenderer.cs" />
Expand Down

0 comments on commit 3b001e2

Please sign in to comment.