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

${aspnet-session} - added ValueType to handle integer session values #570

Merged
merged 4 commits into from
Jun 22, 2020
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like the name

{
/// <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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea since ASP_NET_CORE3 includes entire framework

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