From d3957718497b643d046051f492d598a83001f18a Mon Sep 17 00:00:00 2001 From: Julian Verdurmen <304NotModified@users.noreply.github.com> Date: Thu, 28 May 2020 01:28:46 +0200 Subject: [PATCH 1/4] test --- .../AspNetSessionValueLayoutRendererTests2.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/Shared/LayoutRenderers/AspNetSessionValueLayoutRendererTests2.cs b/tests/Shared/LayoutRenderers/AspNetSessionValueLayoutRendererTests2.cs index 64684435..7bc4a455 100644 --- a/tests/Shared/LayoutRenderers/AspNetSessionValueLayoutRendererTests2.cs +++ b/tests/Shared/LayoutRenderers/AspNetSessionValueLayoutRendererTests2.cs @@ -23,7 +23,7 @@ namespace NLog.Web.Tests.LayoutRenderers public class AspNetSessionValueLayoutRendererTests2 : LayoutRenderersTestBase { [Fact] - public void SingleItemRendersCorrectValue() + public void SingleStringItemRendersCorrectValue() { // Arrange var (renderer, _) = CreateRenderer(); @@ -36,6 +36,20 @@ public void SingleItemRendersCorrectValue() Assert.Equal("https://duckduckgo.com", result); } + [Fact] + public void SingleIntItemRendersCorrectValue() + { + // Arrange + var (renderer, _) = CreateRenderer(); + renderer.Variable = "b"; + + // Act + string result = renderer.Render(LogEventInfo.CreateNullEvent()); + + // Assert + Assert.Equal("123", result); + } + [Fact] public void MissingItemRendersEmpty() { @@ -50,12 +64,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(); var sessionFeature = new SessionFeatureMock(mockSession); From fb9827b23916d6199cc7c641d2d80bd876b47a0b Mon Sep 17 00:00:00 2001 From: Julian Verdurmen <304NotModified@users.noreply.github.com> Date: Sun, 21 Jun 2020 23:40:41 +0200 Subject: [PATCH 2/4] Support int32 session value --- .../LayoutRenderers/SessionValueType.cs | 20 ++++++++++++++++ src/Shared/Internal/HttpContextExtensions.cs | 14 +++++++++++ .../AspNetSessionValueLayoutRenderer.cs | 23 ++++++++++++++++++- .../AspNetSessionValueLayoutRendererTests2.cs | 1 + 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/NLog.Web.AspNetCore/LayoutRenderers/SessionValueType.cs diff --git a/src/NLog.Web.AspNetCore/LayoutRenderers/SessionValueType.cs b/src/NLog.Web.AspNetCore/LayoutRenderers/SessionValueType.cs new file mode 100644 index 00000000..bbaae1fb --- /dev/null +++ b/src/NLog.Web.AspNetCore/LayoutRenderers/SessionValueType.cs @@ -0,0 +1,20 @@ +#if ASP_NET_CORE + +namespace NLog.Web.LayoutRenderers +{ + /// + /// The type of a value + /// + public enum SessionValueType + { + /// + /// String + /// + String, + /// + /// Int32 + /// + Int32 + } +} +#endif \ No newline at end of file diff --git a/src/Shared/Internal/HttpContextExtensions.cs b/src/Shared/Internal/HttpContextExtensions.cs index 5fa13270..fa53fd36 100644 --- a/src/Shared/Internal/HttpContextExtensions.cs +++ b/src/Shared/Internal/HttpContextExtensions.cs @@ -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 diff --git a/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs index e89a9b0c..a7d6522d 100644 --- a/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs @@ -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 @@ -70,6 +72,13 @@ public AspNetSessionValueLayoutRenderer() /// public CultureInfo Culture { get; set; } +#if ASP_NET_CORE + /// + /// The hype of the value. + /// + public SessionValueType ValueType = SessionValueType.String; +#endif + /// /// Renders the specified ASP.NET Session value and appends it to the specified . /// @@ -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 { @@ -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 HttpContextExtensions.GetInt32(session, key); + default: return HttpContextExtensions.GetString(session, key); + } + } +#endif } } \ No newline at end of file diff --git a/tests/Shared/LayoutRenderers/AspNetSessionValueLayoutRendererTests2.cs b/tests/Shared/LayoutRenderers/AspNetSessionValueLayoutRendererTests2.cs index 7bc4a455..d84636c9 100644 --- a/tests/Shared/LayoutRenderers/AspNetSessionValueLayoutRendererTests2.cs +++ b/tests/Shared/LayoutRenderers/AspNetSessionValueLayoutRendererTests2.cs @@ -42,6 +42,7 @@ public void SingleIntItemRendersCorrectValue() // Arrange var (renderer, _) = CreateRenderer(); renderer.Variable = "b"; + renderer.ValueType = SessionValueType.Int32; // Act string result = renderer.Render(LogEventInfo.CreateNullEvent()); From 639bddebad6703884a5ca9711221bec19f4e7e8d Mon Sep 17 00:00:00 2001 From: Julian Verdurmen <304NotModified@users.noreply.github.com> Date: Sun, 21 Jun 2020 23:51:16 +0200 Subject: [PATCH 3/4] prop --- src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs index a7d6522d..1b354df0 100644 --- a/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs @@ -76,7 +76,7 @@ public AspNetSessionValueLayoutRenderer() /// /// The hype of the value. /// - public SessionValueType ValueType = SessionValueType.String; + public SessionValueType ValueType { get; set; } = SessionValueType.String; #endif /// From 58c36c32b1a8f102d207dd0994774ac98e7e91df Mon Sep 17 00:00:00 2001 From: Julian Verdurmen <304NotModified@users.noreply.github.com> Date: Sun, 21 Jun 2020 23:55:00 +0200 Subject: [PATCH 4/4] improve usage --- src/Shared/Internal/HttpContextExtensions.cs | 2 +- .../LayoutRenderers/AspNetSessionValueLayoutRenderer.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Shared/Internal/HttpContextExtensions.cs b/src/Shared/Internal/HttpContextExtensions.cs index fa53fd36..b55780da 100644 --- a/src/Shared/Internal/HttpContextExtensions.cs +++ b/src/Shared/Internal/HttpContextExtensions.cs @@ -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)) diff --git a/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs index 1b354df0..b6185c9b 100644 --- a/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs @@ -131,8 +131,8 @@ private object GetSessionValue(ISession session, string key) switch (ValueType) { case SessionValueType.Int32: - return HttpContextExtensions.GetInt32(session, key); - default: return HttpContextExtensions.GetString(session, key); + return session.GetInt32(key); + default: return session.GetString(key); } } #endif