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

Refactor code #198

Merged
merged 1 commit into from
Oct 12, 2017
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
34 changes: 16 additions & 18 deletions NLog.Web.AspNetCore/Internal/PropertyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,29 @@ public static object GetValue(string key, Func<string, object> getVal, bool eval
return null;
}

object value;
if (evaluateAsNestedProperties)
{
var path = key.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
var value = evaluateAsNestedProperties ? GetValueAsNestedProperties(key, getVal) : getVal(key);
return value;
}

private static object GetValueAsNestedProperties(string key, Func<string, object> getVal)
{
var path = key.Split(new[] {'.'}, StringSplitOptions.RemoveEmptyEntries);

value = getVal(path.First());
var value = getVal(path.First());

if (value != null)
if (value != null)
{
foreach (var property in path.Skip(1))
{
foreach (var property in path.Skip(1))
var propertyInfo = GetPropertyInfo(value, property);
value = propertyInfo?.GetValue(value, null);
if (value == null)
{
var propertyInfo = GetPropertyInfo(value, property);
value = propertyInfo?.GetValue(value, null);
if (value == null)
{
//done
break;
}
//done
break;
}
}
}
else
{
value = getVal(key);
}
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,67 +42,75 @@ public abstract class AspNetLayoutMultiValueRendererBase : AspNetLayoutRendererB
protected void SerializeValues(IEnumerable<KeyValuePair<string, string>> values,
StringBuilder builder)
{
var firstItem = true;

switch (OutputFormat)
{
case AspNetRequestLayoutOutputFormat.Flat:

foreach (var kpv in values)
{
var key = kpv.Key;
var value = kpv.Value;

if (!firstItem)
{
builder.Append(ItemSeparator);
}
firstItem = false;
builder.Append(key);
builder.Append(ValueSeparator);
builder.Append(value);
}


SerializValuesFlat(values, builder);
break;
case AspNetRequestLayoutOutputFormat.Json:
SerializeValuesJson(values, builder);
break;
}
}

private void SerializeValuesJson(IEnumerable<KeyValuePair<string, string>> values, StringBuilder builder)
{
var firstItem = true;
var valueList = values.ToList();

var valueList = values.ToList();

if (valueList.Count > 0)
if (valueList.Count > 0)
{
var addArray = valueList.Count > (SingleAsArray ? 0 : 1);

if (addArray)
{
builder.Append('[');
}

foreach (var kpv in valueList)
{
var key = kpv.Key;
var value = kpv.Value;
if (!firstItem)
{
var addArray = valueList.Count > (SingleAsArray ? 0 : 1);

if (addArray)
{
builder.Append('[');
}

foreach (var kpv in valueList)
{
var key = kpv.Key;
var value = kpv.Value;
if (!firstItem)
{
builder.Append(',');
}
firstItem = false;

//quoted key
builder.Append('{');
AppendQuoted(builder, key);

builder.Append(':');
//quoted value;
AppendQuoted(builder, value);
builder.Append('}');
}
if (addArray)
{
builder.Append(']');
}
builder.Append(',');
}
break;
firstItem = false;

//quoted key
builder.Append('{');
AppendQuoted(builder, key);

builder.Append(':');
//quoted value;
AppendQuoted(builder, value);
builder.Append('}');
}
if (addArray)
{
builder.Append(']');
}
}

}

private void SerializValuesFlat(IEnumerable<KeyValuePair<string, string>> values, StringBuilder builder)
{
var firstItem = true;
foreach (var kpv in values)
{
var key = kpv.Key;
var value = kpv.Value;

if (!firstItem)
{
builder.Append(ItemSeparator);
}
firstItem = false;
builder.Append(key);
builder.Append(ValueSeparator);
builder.Append(value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ public class AspNetMvcActionRenderer : AspNetMvcLayoutRendererBase
protected override void MvcDoAppend(StringBuilder builder, LogEventInfo logEvent, HttpContextBase context)
{
string key = "action";
string controller;

#if !ASP_NET_CORE
controller = RouteTable.Routes?.GetRouteData(context)?.Values[key]?.ToString();
var controller = RouteTable.Routes?.GetRouteData(context)?.Values[key]?.ToString();
#else
controller = context?.GetRouteData()?.Values?[key]?.ToString();
var controller = context?.GetRouteData()?.Values?[key]?.ToString();
#endif
if (!string.IsNullOrEmpty(controller))
builder.Append(controller);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ public class AspNetMvcControllerRenderer : AspNetMvcLayoutRendererBase
protected override void MvcDoAppend(StringBuilder builder, LogEventInfo logEvent, HttpContextBase context)
{
string key = "controller";
string controller;

#if !ASP_NET_CORE
controller = RouteTable.Routes?.GetRouteData(context)?.Values[key]?.ToString();
var controller = RouteTable.Routes?.GetRouteData(context)?.Values[key]?.ToString();
#else
controller = context?.GetRouteData()?.Values?[key]?.ToString();
var controller = context?.GetRouteData()?.Values?[key]?.ToString();
#endif
if (!string.IsNullOrEmpty(controller))
builder.Append(controller);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,12 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)

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




if (value != null)
{
if (this.OutputFormat == AspNetRequestLayoutOutputFormat.Json)
Expand Down
41 changes: 24 additions & 17 deletions NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestUrlRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,28 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
if (httpRequest == null)
return;

string url, pathAndQuery, port, host, scheme;
url = pathAndQuery = port = host = scheme = null;
var url = CreateUrl(httpRequest);

builder.Append(url);
}

#if !ASP_NET_CORE


private string CreateUrl(HttpRequestBase httpRequest)
{
string port = null, host = null, scheme = null;

if (httpRequest.Url == null)
{
return;
return null;
}

if (IncludePort && httpRequest.Url.Port > 0)
{
port = ":" + httpRequest.Url.Port;
}

if (IncludeQueryString)
{
pathAndQuery = httpRequest.Url.PathAndQuery;
}
else
{
pathAndQuery = httpRequest.Url.AbsolutePath;
}
var pathAndQuery = IncludeQueryString ? httpRequest.Url.PathAndQuery : httpRequest.Url.AbsolutePath;

if (IncludeHost)
{
Expand All @@ -96,8 +95,15 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
scheme = httpRequest.Url.Scheme + "://";
}

url = $"{scheme}{host}{port}{pathAndQuery}";
var url = $"{scheme}{host}{port}{pathAndQuery}";
return url;
}

#else
private string CreateUrl(Microsoft.AspNetCore.Http.HttpRequest httpRequest)
{
string pathAndQuery = null, port = null, host = null, scheme = null;

if (IncludeQueryString)
{
pathAndQuery = httpRequest.QueryString.Value;
Expand All @@ -114,13 +120,14 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
}

if (IncludeScheme && !String.IsNullOrWhiteSpace(httpRequest.Scheme))
{
{
scheme = httpRequest.Scheme + "://";
}

url = $"{scheme}{host}{port}{httpRequest.PathBase.ToUriComponent()}{httpRequest.Path.ToUriComponent()}{pathAndQuery}";
#endif
builder.Append(url);
var url = $"{scheme}{host}{port}{httpRequest.PathBase.ToUriComponent()}{httpRequest.Path.ToUriComponent()}{pathAndQuery}";
return url;
}

#endif
}
}
Loading