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

Added ThreadSafe-attribute for LayoutRenderer to optimize async Precalculate #326

Merged
merged 1 commit into from
Oct 31, 2018
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
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
#if !ASP_NET_CORE
//TODO test .NET Core
using System.Collections.Specialized;
using System.IO;
using System.IO;
#if !ASP_NET_CORE
using System.Collections.Specialized;
using System.Web;
using System.Web.Routing;
using System.Web.SessionState;
#else
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using HttpContextBase = Microsoft.AspNetCore.Http.HttpContext;
#endif
using NLog.Common;
using NLog.Config;
using NLog.Web.LayoutRenderers;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using Xunit;
using System.Collections.Generic;

namespace NLog.Web.Tests.LayoutRenderers
{
public class AspNetRequestValueLayoutRendererTests : TestBase
public class AspNetRequestValueLayoutRendererTests : TestInvolvingAspNetHttpContext
{
[Fact]
public void NullHttpContextRendersEmptyString()
Expand All @@ -31,6 +30,7 @@ public void NullHttpContextRendersEmptyString()
Assert.Empty(result);
}

#if !ASP_NET_CORE
[Fact]
public void NullRequestRendersEmptyStringWithoutLoggingError()
{
Expand All @@ -50,6 +50,7 @@ public void NullRequestRendersEmptyStringWithoutLoggingError()
Assert.Empty(result);
Assert.True(string.IsNullOrEmpty(internalLog.ToString()));
}
#endif

public class ItemTests
{
Expand Down Expand Up @@ -86,7 +87,11 @@ public void KeyFoundRendersValue()
{
var expectedResult = "value";
var httpContext = Substitute.For<HttpContextBase>();
#if !ASP_NET_CORE
httpContext.Request["key"].Returns(expectedResult);
#else
httpContext.Request.HttpContext.Items.Returns(new Dictionary<object, object>() { { "key", expectedResult } });
#endif

var renderer = new AspNetRequestValueLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
Expand Down Expand Up @@ -133,7 +138,12 @@ public void KeyFoundRendersValue()
{
var expectedResult = "value";
var httpContext = Substitute.For<HttpContextBase>();
#if !ASP_NET_CORE
httpContext.Request.QueryString.Returns(new NameValueCollection { {"key", expectedResult} });
#else
var queryCollection = new Microsoft.AspNetCore.Http.Internal.QueryCollection(new Dictionary<string, StringValues>() { { "key", expectedResult } });
httpContext.Request.Query.Returns(queryCollection);
#endif

var renderer = new AspNetRequestValueLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
Expand Down Expand Up @@ -180,7 +190,12 @@ public void KeyFoundRendersValue()
{
var expectedResult = "value";
var httpContext = Substitute.For<HttpContextBase>();
#if !ASP_NET_CORE
httpContext.Request.Headers.Returns(new NameValueCollection { { "key", expectedResult } });
#else
var headerDictionary = new HeaderDictionary(new Dictionary<string, StringValues>() { { "key", expectedResult } });
httpContext.Request.Headers.Returns(headerDictionary);
#endif

var renderer = new AspNetRequestValueLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
Expand Down Expand Up @@ -227,7 +242,13 @@ public void KeyFoundRendersValue()
{
var expectedResult = "value";
var httpContext = Substitute.For<HttpContextBase>();
#if !ASP_NET_CORE
httpContext.Request.Form.Returns(new NameValueCollection { { "key", expectedResult } });
#else
httpContext.Request.HasFormContentType.Returns(true);
var formCollection = new FormCollection(new Dictionary<string, StringValues>{ { "key", expectedResult } });
httpContext.Request.Form.Returns(formCollection);
#endif

var renderer = new AspNetRequestValueLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
Expand All @@ -239,6 +260,7 @@ public void KeyFoundRendersValue()
}
}

#if !ASP_NET_CORE
public class ServerVariablesTests
{
[Fact]
Expand Down Expand Up @@ -285,7 +307,7 @@ public void KeyFoundRendersValue()
Assert.Equal(expectedResult, result);
}
}

#endif
public class CookieTests
{
[Fact]
Expand Down Expand Up @@ -321,7 +343,12 @@ public void KeyFoundRendersValue()
{
var expectedResult = "value";
var httpContext = Substitute.For<HttpContextBase>();
#if !ASP_NET_CORE
httpContext.Request.Cookies.Returns(new HttpCookieCollection {new HttpCookie("key", expectedResult) });
#else
var cookieCollection = new Microsoft.AspNetCore.Http.Internal.RequestCookieCollection(new Dictionary<string, string>{ { "key", expectedResult } });
httpContext.Request.Cookies.Returns(cookieCollection);
#endif

var renderer = new AspNetRequestValueLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
Expand All @@ -333,5 +360,4 @@ public void KeyFoundRendersValue()
}
}
}
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#else
using System.Web.Hosting;
#endif
using NLog.Config;
using NLog.LayoutRenderers;

namespace NLog.Web.LayoutRenderers
Expand All @@ -21,6 +22,8 @@ namespace NLog.Web.LayoutRenderers
/// </summary>
#endif
[LayoutRenderer("aspnet-appbasepath")]
[ThreadAgnostic]
[ThreadSafe]
public class AspNetAppBasePathLayoutRenderer : LayoutRenderer
{
#if ASP_NET_CORE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace NLog.Web.LayoutRenderers
/// </code>
/// </example>
[LayoutRenderer("aspnet-application")]
[ThreadSafe]
public class AspNetApplicationValueLayoutRenderer : AspNetLayoutRendererBase
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web.DependencyInjection;

Expand All @@ -13,6 +14,8 @@ namespace NLog.Web.LayoutRenderers
/// Rendering development environment. <see cref="IHostingEnvironment"/>
/// </summary>
[LayoutRenderer("aspnet-environment")]
[ThreadAgnostic]
[ThreadSafe]
public class AspNetEnvironmentLayoutRenderer : LayoutRenderer
{
private static IHostingEnvironment _hostingEnvironment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace NLog.Web.LayoutRenderers
/// </code>
/// </example>
[LayoutRenderer("aspnet-item")]
[ThreadSafe]
public class AspNetItemValueLayoutRenderer : AspNetLayoutRendererBase
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using NLog.LayoutRenderers;
using System.Text;
#if !ASP_NET_CORE
using System.Web.Routing;
Expand All @@ -8,6 +7,8 @@
using Microsoft.AspNetCore.Http;
using HttpContextBase = Microsoft.AspNetCore.Http.HttpContext;
#endif
using NLog.Config;
using NLog.LayoutRenderers;

namespace NLog.Web.LayoutRenderers
{
Expand All @@ -23,6 +24,7 @@ namespace NLog.Web.LayoutRenderers
/// </code>
/// </example>
[LayoutRenderer("aspnet-mvc-action")]
[ThreadSafe]
public class AspNetMvcActionRenderer : AspNetMvcLayoutRendererBase
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using NLog.LayoutRenderers;
using System.Text;
#if !ASP_NET_CORE
using System.Web.Routing;
Expand All @@ -8,7 +7,8 @@
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Http;
#endif

using NLog.Config;
using NLog.LayoutRenderers;

namespace NLog.Web.LayoutRenderers
{
Expand All @@ -24,6 +24,7 @@ namespace NLog.Web.LayoutRenderers
/// </code>
/// </example>
[LayoutRenderer("aspnet-mvc-controller")]
[ThreadSafe]
public class AspNetMvcControllerRenderer : AspNetMvcLayoutRendererBase
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if ASP_NET_CORE
using System.Text;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web.Internal;

Expand All @@ -14,6 +15,7 @@ namespace NLog.Web.LayoutRenderers
/// </code>
/// </example>
[LayoutRenderer("aspnet-request-contenttype")]
[ThreadSafe]
public class AspNetRequestContentTypeLayoutRenderer : AspNetLayoutRendererBase
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#else
using Microsoft.AspNetCore.Http;
#endif
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web.Enums;

using NLog.Web.Internal;

namespace NLog.Web.LayoutRenderers
Expand All @@ -25,6 +25,7 @@ namespace NLog.Web.LayoutRenderers
/// </code>
/// </example>
[LayoutRenderer("aspnet-request-cookie")]
[ThreadSafe]
public class AspNetRequestCookieLayoutRenderer : AspNetLayoutMultiValueRendererBase
{
/// <summary>
Expand Down Expand Up @@ -52,62 +53,50 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
}

#if !ASP_NET_CORE

private IEnumerable<KeyValuePair<string, string>> GetCookies(HttpCookieCollection cookies)
{
var cookieNames = CookieNames;
if (cookieNames != null)
foreach (var cookieName in CookieNames)
{
foreach (var cookieName in cookieNames)
var httpCookie = cookies[cookieName];
if (httpCookie == null)
{
var httpCookie = cookies[cookieName];
if (httpCookie == null)
{
continue;
}
continue;
}

if (OutputFormat == AspNetRequestLayoutOutputFormat.Json)
if (OutputFormat == AspNetRequestLayoutOutputFormat.Json)
{
// Split multi-valued cookie, as allowed for in the HttpCookie API for backwards compatibility with classic ASP
var isFirst = true;
foreach (var multiValueKey in httpCookie.Values.AllKeys)
{
// Split multi-valued cookie, as allowed for in the HttpCookie API for backwards compatibility with classic ASP
var isFirst = true;
foreach (var multiValueKey in httpCookie.Values.AllKeys)
var cookieKey = multiValueKey;
if (isFirst)
{
var cookieKey = multiValueKey;
if (isFirst)
{
cookieKey = cookieName;
isFirst = false;
}
yield return new KeyValuePair<string, string>(cookieKey, httpCookie.Values[multiValueKey]);
cookieKey = cookieName;
isFirst = false;
}
}
else
{
yield return new KeyValuePair<string, string>(cookieName, httpCookie.Value);
yield return new KeyValuePair<string, string>(cookieKey, httpCookie.Values[multiValueKey]);
}
}
else
{
yield return new KeyValuePair<string, string>(cookieName, httpCookie.Value);
}
}
}

#else

private IEnumerable<KeyValuePair<string, string>> GetCookies(IRequestCookieCollection cookies)
{
var cookieNames = CookieNames;
if (cookieNames != null)
foreach (var cookieName in CookieNames)
{
foreach (var cookieName in cookieNames)
if (!cookies.TryGetValue(cookieName, out var cookieValue))
{
if (!cookies.TryGetValue(cookieName, out var cookieValue))
{
continue;
}

yield return new KeyValuePair<string, string>(cookieName, cookieValue);
continue;
}

yield return new KeyValuePair<string, string>(cookieName, cookieValue);
}
}

#endif
}
}
Loading