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 ee81318
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 40 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,
}
}
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
2 changes: 1 addition & 1 deletion 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

0 comments on commit ee81318

Please sign in to comment.