Closed object closed 9 years ago
As a workaround I created the following class that added exception properties the generated JSON, but IMHO this is something that should be made available in Serilog:
public class StructuredExceptionEnricher : ILogEventEnricher
{
public const string ExceptionPropertyName = "Exception";
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
if (logEvent == null)
throw new ArgumentNullException("logEvent");
if (logEvent.Exception != null)
{
var exceptionProperty = new LogEventProperty(
ExceptionPropertyName,
new DictionaryValue(logEvent.Exception.GetType().GetProperties().Select(
x => new KeyValuePair<ScalarValue, LogEventPropertyValue>(
new ScalarValue(x.Name),
new ScalarValue(x.GetValue(logEvent.Exception, null))))));
logEvent.AddOrUpdateProperty(exceptionProperty);
}
}
}
Thanks for the feedback, will keep this on the list :+1:
FYI, you can read this discussion about how I handled it in my project. It's on seq google group, but is used as an Enricher.
Thanks @tbolon, I also ended up with custom enricher, but IMHO this is of so common use that it might be added as a part of core implementation.
I've reopened #78 to try to condense exception-related stuff there.
I wrote earlier a post on SO (http://stackoverflow.com/questions/25323607/exception-desctructuring-in-serilog), and I'll try to explain better what I mean. Let's look at this code:
If I use JsonFormatter, I will get the following JSON:
{"Timestamp":"2014-08-21T11:47:15.7030000+02:00","Level":"Debug","MessageTemplate":"This is an exception","RenderedMessage":"This is an exception","Exception":"System.ArgumentException: Exception message\r\nParameternavn: TestArgument ---> System.Exception: Inner exception\r\n --- "}
Note that the JSON only include text representation of the exception, and since this is a JSON I would like it to be destructured or at least have a possibility to enable such option.