Skip to content

Commit

Permalink
NLogBeginScopeParser - Reduce complexity of the individual methods
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Aug 1, 2018
1 parent 2bbddba commit 5fc532d
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static ScopeProperties CaptureScopeProperties(System.Collections.IEnumera

if (scope == null)
{
if (!TryCreateExtractor(stateExractor, property.GetType(), out keyValueExtractor))
if (!TryLookupExtractor(stateExractor, property.GetType(), out keyValueExtractor))
return null;

scope = new ScopeProperties();
Expand All @@ -122,7 +122,7 @@ public static ScopeProperties CaptureScopeProperties(System.Collections.IEnumera

public static ScopeProperties CaptureScopeProperty<TState>(TState scopeProperty, ConcurrentDictionary<Type, KeyValuePair<Func<object, object>, Func<object, object>>> stateExractor)
{
if (!TryCreateExtractor(stateExractor, scopeProperty.GetType(), out var keyValueExtractor))
if (!TryLookupExtractor(stateExractor, scopeProperty.GetType(), out var keyValueExtractor))
return null;

var scope = new ScopeProperties();
Expand All @@ -145,32 +145,13 @@ private static void AddKeyValueProperty(ScopeProperties scope, KeyValuePair<Func
}
}

public static bool TryCreateExtractor(ConcurrentDictionary<Type, KeyValuePair<Func<object, object>, Func<object, object>>> stateExractor, Type propertyType, out KeyValuePair<Func<object, object>, Func<object, object>> keyValueExtractor)
private static bool TryLookupExtractor(ConcurrentDictionary<Type, KeyValuePair<Func<object, object>, Func<object, object>>> stateExractor, Type propertyType, out KeyValuePair<Func<object, object>, Func<object, object>> keyValueExtractor)
{
if (!stateExractor.TryGetValue(propertyType, out keyValueExtractor))
{
try
{
var itemType = propertyType.GetTypeInfo();
if (!itemType.IsGenericType || itemType.GetGenericTypeDefinition() != typeof(KeyValuePair<,>))
return false;

var keyPropertyInfo = itemType.GetDeclaredProperty("Key");
var valuePropertyInfo = itemType.GetDeclaredProperty("Value");
if (valuePropertyInfo == null || keyPropertyInfo == null)
return false;

var keyValuePairObjParam = System.Linq.Expressions.Expression.Parameter(typeof(object), "pair");
var keyValuePairTypeParam = System.Linq.Expressions.Expression.Convert(keyValuePairObjParam, propertyType);

var propertyKeyAccess = System.Linq.Expressions.Expression.Property(keyValuePairTypeParam, keyPropertyInfo);
var propertyKeyAccessObj = System.Linq.Expressions.Expression.Convert(propertyKeyAccess, typeof(object));
var propertyKeyLambda = System.Linq.Expressions.Expression.Lambda<Func<object, object>>(propertyKeyAccessObj, keyValuePairObjParam).Compile();

var propertyValueAccess = System.Linq.Expressions.Expression.Property(keyValuePairTypeParam, valuePropertyInfo);
var propertyValueLambda = System.Linq.Expressions.Expression.Lambda<Func<object, object>>(propertyValueAccess, keyValuePairObjParam).Compile();

keyValueExtractor = new KeyValuePair<Func<object, object>, Func<object, object>>(propertyKeyLambda, propertyValueLambda);
return TryBuildExtractor(propertyType, out keyValueExtractor);
}
catch (Exception ex)
{
Expand All @@ -185,6 +166,31 @@ public static bool TryCreateExtractor(ConcurrentDictionary<Type, KeyValuePair<Fu
return keyValueExtractor.Key != null;
}

private static bool TryBuildExtractor(Type propertyType, out KeyValuePair<Func<object, object>, Func<object, object>> keyValueExtractor)
{
keyValueExtractor = default(KeyValuePair<Func<object, object>, Func<object, object>>);

var itemType = propertyType.GetTypeInfo();
if (!itemType.IsGenericType || itemType.GetGenericTypeDefinition() != typeof(KeyValuePair<,>))
return false;

var keyPropertyInfo = itemType.GetDeclaredProperty("Key");
var valuePropertyInfo = itemType.GetDeclaredProperty("Value");
if (valuePropertyInfo == null || keyPropertyInfo == null)
return false;

var keyValuePairObjParam = System.Linq.Expressions.Expression.Parameter(typeof(object), "pair");
var keyValuePairTypeParam = System.Linq.Expressions.Expression.Convert(keyValuePairObjParam, propertyType);

var propertyKeyAccess = System.Linq.Expressions.Expression.Property(keyValuePairTypeParam, keyPropertyInfo);
var propertyKeyAccessObj = System.Linq.Expressions.Expression.Convert(propertyKeyAccess, typeof(object));
var propertyKeyLambda = System.Linq.Expressions.Expression.Lambda<Func<object, object>>(propertyKeyAccessObj, keyValuePairObjParam).Compile();

var propertyValueAccess = System.Linq.Expressions.Expression.Property(keyValuePairTypeParam, valuePropertyInfo);
var propertyValueLambda = System.Linq.Expressions.Expression.Lambda<Func<object, object>>(propertyValueAccess, keyValuePairObjParam).Compile();
return true;
}

public void AddDispose(IDisposable disposable)
{
if (disposable != null)
Expand Down

0 comments on commit 5fc532d

Please sign in to comment.