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 ASP.NET 5 compatible package #30

Merged
merged 22 commits into from
Feb 14, 2016
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
Expand Up @@ -2,6 +2,7 @@
using System.Reflection;
using System.Web;
using System.Web.SessionState;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Layouts;
using NLog.Web.LayoutRenderers;
Expand All @@ -11,13 +12,23 @@ namespace NLog.Web.Tests.LayoutRenderers
{
public class AspNetSessionValueLayoutRendererTests : TestInvolvingAspNetHttpContext
{



public AspNetSessionValueLayoutRendererTests()
{
SetUp();
}

public void SetUp()
{
//auto load won't work yet (in DNX), so use <extensions>
LogManager.Configuration = CreateConfigurationFromString(@"
<nlog throwExceptions='true'>
<extensions>
<add assembly='NLog.Web' />
</extensions>
</nlog>");
SetupFakeSession();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.IO;
using System.Reflection;
using System.Web;
using System.Xml;
using NLog.Config;

namespace NLog.Web.Tests.LayoutRenderers
{
Expand All @@ -28,6 +30,16 @@ protected virtual void CleanUp()
{
}

protected XmlLoggingConfiguration CreateConfigurationFromString(string configXml)
{

XmlDocument doc = new XmlDocument();
doc.LoadXml(configXml);
using (var stringReader = new StringReader(doc.DocumentElement.OuterXml))
using (XmlReader reader = XmlReader.Create(stringReader))
return new XmlLoggingConfiguration(reader, null);
}

protected HttpContext SetupFakeHttpContext()
{
var httpRequest = SetUpHttpRequest();
Expand Down Expand Up @@ -57,7 +69,7 @@ protected void AddHeader(HttpRequest request, string headerName, string headerVa
item.Add(headerValue);
t.InvokeMember("BaseAdd", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null,
headers,
new object[] {headerName, item});
new object[] { headerName, item });
t.InvokeMember("MakeReadOnly", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,
null,
headers, null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#if !DNX
using System.Web;
#else
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;

#endif



namespace NLog.Web
{
#if !DNX
/// <summary>
/// Provides the HttpContext associated with the current request.
/// </summary>
Expand All @@ -16,8 +25,10 @@ public HttpContextBase HttpContext
{
if (System.Web.HttpContext.Current == null)
return null;
return new HttpContextWrapper(System.Web.HttpContext.Current);
return new HttpContextWrapper(System.Web.HttpContext.Current);
}
}

}
#endif
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
#if !DNX

using System;
using System.Web;

namespace NLog.Web
{

/// <summary>
/// Provides access to the HttpContext
/// </summary>
Expand All @@ -14,3 +17,4 @@ public interface IHttpContextAccessor
HttpContextBase HttpContext { get; }
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace NLog.Web.Internal
{
Expand All @@ -24,7 +25,7 @@ public static object GetValue(string key, Func<string, object> getVal, bool eval

foreach (var property in path.Skip(1))
{
var propertyInfo = value.GetType().GetProperty(property);
var propertyInfo = GetPropertyInfo(value, property);
value = propertyInfo.GetValue(value, null);
}
}
Expand All @@ -34,5 +35,14 @@ public static object GetValue(string key, Func<string, object> getVal, bool eval
}
return value;
}

private static PropertyInfo GetPropertyInfo(object value, string propertyName)
{
#if !DNX
return value.GetType().GetProperty(propertyName);
#else
return value.GetType().GetTypeInfo().GetDeclaredProperty(propertyName);
#endif
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
using System;
using System.Globalization;
using System.Text;
#if !DNX
using System.Web;
#else
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
#endif
using NLog.Config;
using NLog.LayoutRenderers;


#if !DNX
namespace NLog.Web.LayoutRenderers
{
/// <summary>
Expand Down Expand Up @@ -36,6 +43,7 @@ namespace NLog.Web.LayoutRenderers
[LayoutRenderer("aspnet-application")]
public class AspNetApplicationValueLayoutRenderer : AspNetLayoutRendererBase
{

/// <summary>
/// Gets or sets the variable name.
/// </summary>
Expand All @@ -56,7 +64,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
return;
}

HttpContextBase context = HttpContextAccessor.HttpContext;
var context = HttpContextAccessor.HttpContext;

if (context.Application == null)
{
Expand All @@ -65,5 +73,8 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)

builder.Append(Convert.ToString(context.Application[this.Variable], CultureInfo.CurrentUICulture));
}


}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
using System.Globalization;
using System.Linq;
using System.Text;
#if !DNX
using System.Web;
#else
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
#endif
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web.Internal;
Expand Down Expand Up @@ -38,6 +43,16 @@ namespace NLog.Web.LayoutRenderers
[LayoutRenderer("aspnet-item")]
public class AspNetItemValueLayoutRenderer : AspNetLayoutRendererBase
{

#if DNX
/// <summary>
/// Initializes the <see cref="AspNetItemValueLayoutRenderer"/> with the <see cref="IHttpContextAccessor"/>.
/// </summary>
public AspNetItemValueLayoutRenderer(IHttpContextAccessor accessor) : base(accessor)
{
}
#endif

/// <summary>
/// Gets or sets the item variable name.
/// </summary>
Expand All @@ -63,7 +78,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
return;
}

HttpContextBase context = HttpContextAccessor.HttpContext;
var context = HttpContextAccessor.HttpContext;

var value = PropertyReader.GetValue(Variable, k => context.Items[k], EvaluateAsNestedProperties);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
using System.Text;
using NLog.LayoutRenderers;
#if !DNX
using System.Web;
#else
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
#endif

namespace NLog.Web.LayoutRenderers
{
Expand All @@ -8,14 +14,28 @@ namespace NLog.Web.LayoutRenderers
/// </summary>
public abstract class AspNetLayoutRendererBase : LayoutRenderer
{

#if DNX
/// <summary>
/// Initializes the <see cref="AspNetLayoutRendererBase"/> with the <see cref="IHttpContextAccessor"/>.
/// </summary>
protected AspNetLayoutRendererBase(IHttpContextAccessor accessor)
{
HttpContextAccessor = accessor;

}
#else

/// <summary>
/// Initializes the <see cref="AspNetLayoutRendererBase"/> with the <see cref="DefaultHttpContextAccessor"/>.
/// Initializes the <see cref="AspNetLayoutRendererBase"/>.
/// </summary>
protected AspNetLayoutRendererBase()
{

HttpContextAccessor = new DefaultHttpContextAccessor();
}

}
#endif
/// <summary>
/// Provides access to the current request HttpContext.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
using System;
using System.Text;
#if !DNX
using System.Web;
#else
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
#endif
using NLog.Config;
using NLog.LayoutRenderers;

Expand All @@ -26,6 +32,14 @@ namespace NLog.Web.LayoutRenderers
[LayoutRenderer("aspnet-request")]
public class AspNetRequestValueLayoutRenderer : AspNetLayoutRendererBase
{
#if DNX
/// <summary>
/// Initializes the <see cref="AspNetRequestValueLayoutRenderer"/> with the <see cref="IHttpContextAccessor"/>.
/// </summary>
public AspNetRequestValueLayoutRenderer(IHttpContextAccessor accessor) : base(accessor)
{
}
#endif
/// <summary>
/// Gets or sets the item name. The QueryString, Form, Cookies, or ServerVariables collection variables having the specified name are rendered.
/// </summary>
Expand Down Expand Up @@ -70,7 +84,7 @@ public class AspNetRequestValueLayoutRenderer : AspNetLayoutRendererBase
/// <param name="logEvent">Logging event.</param>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
HttpContextBase context = HttpContextAccessor.HttpContext;
var context = HttpContextAccessor.HttpContext;
var httpRequest = context.Request;
if (httpRequest == null)
{
Expand All @@ -79,24 +93,39 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)

if (this.QueryString != null)
{
#if !DNX
builder.Append(httpRequest.QueryString[this.QueryString]);
#else
builder.Append(httpRequest.Query[this.QueryString]);
#endif
}
else if (this.Form != null)
{
builder.Append(httpRequest.Form[this.Form]);
}
else if (this.Cookie != null)
{
HttpCookie cookie = httpRequest.Cookies[this.Cookie];
#if !DNX
var cookie = httpRequest.Cookies[this.Cookie];

if (cookie != null)
{
builder.Append(cookie.Value);
}
#else
var cookie = httpRequest.Cookies[this.Cookie];
builder.Append(cookie);
#endif

}
else if (this.ServerVariable != null)
{
#if !DNX
builder.Append(httpRequest.ServerVariables[this.ServerVariable]);
#else

throw new NotSupportedException();
#endif
}
else if (this.Header != null)
{
Expand All @@ -109,7 +138,11 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
}
else if (this.Item != null)
{
#if !DNX
builder.Append(httpRequest[this.Item]);
#else
builder.Append(httpRequest.HttpContext.Items[this.Item]);
#endif
}
}
}
Expand Down
Loading