Skip to content

Commit

Permalink
ExceptionLayoutRenderer - Support Serialize Format
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Oct 14, 2017
1 parent 74e6e26 commit 97dde15
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 46 deletions.
6 changes: 5 additions & 1 deletion src/NLog/Config/ExceptionRenderingFormat.cs
Expand Up @@ -71,6 +71,10 @@ public enum ExceptionRenderingFormat
/// <summary>
/// Appends the contents of an Exception's Data property to the specified target.
/// </summary>
Data = 6
Data = 6,
/// <summary>
/// Destructure the exception (usually into JSON)
/// </summary>
Serialize = 7,
}
}
5 changes: 5 additions & 0 deletions src/NLog/Internal/NetStandardHelpers.cs
Expand Up @@ -102,6 +102,11 @@ public static MethodInfo[] GetMethods(this Type type)
return type.GetTypeInfo().GetMethods();
}

public static PropertyInfo[] GetProperties(this Type type)
{
return type.GetTypeInfo().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
}

public static PropertyInfo[] GetProperties(this Type type, BindingFlags bindingAttr)
{
return type.GetTypeInfo().GetProperties(bindingAttr);
Expand Down
14 changes: 13 additions & 1 deletion src/NLog/LayoutRenderers/ExceptionLayoutRenderer.cs
Expand Up @@ -64,6 +64,7 @@ public class ExceptionLayoutRenderer : LayoutRenderer
{"METHOD",ExceptionRenderingFormat.Method},
{"STACKTRACE", ExceptionRenderingFormat.StackTrace},
{"DATA",ExceptionRenderingFormat.Data},
{"@",ExceptionRenderingFormat.Serialize},
};

/// <summary>
Expand All @@ -85,7 +86,8 @@ public ExceptionLayoutRenderer()
{ExceptionRenderingFormat.ToString, AppendToString},
{ExceptionRenderingFormat.Method, AppendMethod},
{ExceptionRenderingFormat.StackTrace, AppendStackTrace},
{ExceptionRenderingFormat.Data, AppendData}
{ExceptionRenderingFormat.Data, AppendData},
{ExceptionRenderingFormat.Serialize, AppendSerializeObject},
};
}

Expand Down Expand Up @@ -378,6 +380,16 @@ protected virtual void AppendData(StringBuilder sb, Exception ex)
}
}

/// <summary>
/// Appends all the serialized properties of an Exception into the specified <see cref="StringBuilder" />.
/// </summary>
/// <param name="sb">The <see cref="StringBuilder"/> to append the rendered data to.</param>
/// <param name="ex">The Exception whose properties should be appended.</param>
protected virtual void AppendSerializeObject(StringBuilder sb, Exception ex)
{
ConfigurationItemFactory.Default.ValueSerializer.SerializeObject(ex, null, null, sb);
}

/// <summary>
/// Split the string and then compile into list of Rendering formats.
/// </summary>
Expand Down
8 changes: 1 addition & 7 deletions src/NLog/Targets/DefaultJsonSerializer.cs
Expand Up @@ -349,7 +349,7 @@ private bool SerializeTypeCodeValue(object value, StringBuilder destination, Jso
TypeCode objTypeCode = Convert.GetTypeCode(value);
if (objTypeCode == TypeCode.Object)
{
if (value is Guid || value is TimeSpan)
if (value is Guid || value is TimeSpan || value is Type || value is Assembly || value is MethodBase)
{
//object without property, to string
QuoteValue(destination, Convert.ToString(value, CultureInfo.InvariantCulture));
Expand Down Expand Up @@ -745,14 +745,8 @@ private static bool EscapeChar(char ch, bool escapeUnicode)

private static PropertyInfo[] GetPropertyInfosNoCache(Type type)
{
#if NETSTANDARD
var props = System.Linq.Enumerable.ToArray(type.GetRuntimeProperties());
#else
var props = type.GetProperties();
#endif
return props;
}


}
}

0 comments on commit 97dde15

Please sign in to comment.