Skip to content

Commit

Permalink
Handle int for session value (#570)
Browse files Browse the repository at this point in the history
* test

* Support int32 session value

* prop

* improve usage
  • Loading branch information
304NotModified authored Jun 22, 2020
1 parent a451ecc commit 99d93f1
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
20 changes: 20 additions & 0 deletions src/NLog.Web.AspNetCore/LayoutRenderers/SessionValueType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#if ASP_NET_CORE

namespace NLog.Web.LayoutRenderers
{
/// <summary>
/// The type of a value
/// </summary>
public enum SessionValueType
{
/// <summary>
/// String
/// </summary>
String,
/// <summary>
/// Int32
/// </summary>
Int32
}
}
#endif
16 changes: 15 additions & 1 deletion src/Shared/Internal/HttpContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal static HttpResponse TryGetResponse(this HttpContext context)
}
#endif

#if ASP_NET_CORE
#if ASP_NET_CORE1 || ASP_NET_CORE2
internal static string GetString(this ISession session, string key)
{
if (!session.TryGetValue(key, out var data))
Expand All @@ -82,6 +82,20 @@ internal static string GetString(this ISession session, string key)

return Encoding.UTF8.GetString(data);
}

public static int? GetInt32(this ISession session, string key)
{
if (!session.TryGetValue(key, out var data))
{
return null;
}

if (data == null || data.Length < 4)
{
return null;
}
return data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
}
#endif

#if !ASP_NET_CORE
Expand Down
23 changes: 22 additions & 1 deletion src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using NLog.Web.Internal;
#if !ASP_NET_CORE
using System.Web;
#else
using Microsoft.AspNetCore.Http;
#endif

namespace NLog.Web.LayoutRenderers
Expand Down Expand Up @@ -70,6 +72,13 @@ public AspNetSessionValueLayoutRenderer()
/// <docgen category='Rendering Options' order='10' />
public CultureInfo Culture { get; set; }

#if ASP_NET_CORE
/// <summary>
/// The hype of the value.
/// </summary>
public SessionValueType ValueType { get; set; } = SessionValueType.String;
#endif

/// <summary>
/// Renders the specified ASP.NET Session value and appends it to the specified <see cref="StringBuilder" />.
/// </summary>
Expand Down Expand Up @@ -102,7 +111,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
object value;
try
{
value = PropertyReader.GetValue(Variable, contextSession, (session, key) => session.GetString(key), EvaluateAsNestedProperties);
value = PropertyReader.GetValue(Variable, contextSession, (session, key) => GetSessionValue(session, key), EvaluateAsNestedProperties);
}
finally
{
Expand All @@ -115,5 +124,17 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
builder.Append(Convert.ToString(value, formatProvider));
}
}

#if ASP_NET_CORE
private object GetSessionValue(ISession session, string key)
{
switch (ValueType)
{
case SessionValueType.Int32:
return session.GetInt32(key);
default: return session.GetString(key);
}
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace NLog.Web.Tests.LayoutRenderers
public class AspNetSessionValueLayoutRendererTests2 : LayoutRenderersTestBase<AspNetSessionValueLayoutRenderer>
{
[Fact]
public void SingleItemRendersCorrectValue()
public void SingleStringItemRendersCorrectValue()
{
// Arrange
var (renderer, _) = CreateRenderer();
Expand All @@ -36,6 +36,21 @@ public void SingleItemRendersCorrectValue()
Assert.Equal("https://duckduckgo.com", result);
}

[Fact]
public void SingleIntItemRendersCorrectValue()
{
// Arrange
var (renderer, _) = CreateRenderer();
renderer.Variable = "b";
renderer.ValueType = SessionValueType.Int32;

// Act
string result = renderer.Render(LogEventInfo.CreateNullEvent());

// Assert
Assert.Equal("123", result);
}

[Fact]
public void MissingItemRendersEmpty()
{
Expand All @@ -50,12 +65,15 @@ public void MissingItemRendersEmpty()
Assert.Empty(result);
}



private static (AspNetSessionValueLayoutRenderer, HttpContext) CreateRenderer(bool throwsError = false)
{
var (renderer, httpContext) = CreateWithHttpContext();

var mockSession = new SessionMock(throwsError);
mockSession.SetString("a", "https://duckduckgo.com");
mockSession.SetInt32("b", 123);
httpContext.Session = mockSession;
httpContext.Items = new Dictionary<object, object>();
var sessionFeature = new SessionFeatureMock(mockSession);
Expand Down

0 comments on commit 99d93f1

Please sign in to comment.