Skip to content

Commit

Permalink
Format output of exception as an array.
Browse files Browse the repository at this point in the history
Added filter for newlines and quotes to healthcheck results.
  • Loading branch information
irwinj committed May 8, 2023
1 parent 03cf80d commit f478f49
Showing 1 changed file with 23 additions and 18 deletions.
Expand Up @@ -293,10 +293,11 @@ void WriteEntry(string name, HealthReportEntry entry)
{
writer.WriteString("name", name);
writer.WriteString("status", entry.Status.ToString());
writer.WriteString("description", entry.Description);
writer.WriteString("description", Filter(entry.Description));
writer.WriteString("duration", entry.Duration.ToString("G"));

WriteTags(entry.Tags);

if (entry.Tags.Any()) WriteArray("tags", entry.Tags);
WriteException(entry.Exception);

var list = entry.Data
Expand All @@ -312,21 +313,10 @@ void WriteEntry(string name, HealthReportEntry entry)
writer.WritePropertyName(item.Key);

JsonSerializer.Serialize(writer, item.Value,
item.Value?.GetType() ?? typeof(object));
item.Value?.GetType() ?? typeof(object));
}
writer.WriteEndObject();

void WriteTags(IEnumerable<string> tags)
{
if (!tags.Any())
return;

writer.WriteStartArray("tags");
foreach (var tag in tags)
writer.WriteStringValue(tag);
writer.WriteEndArray();
}

void WriteException(Exception? e)
{
string message;
Expand Down Expand Up @@ -357,14 +347,29 @@ void WriteException(Exception? e)
}

writer.WriteStartObject("exception");
writer.WriteString("message", message);

if(stackTrace is not null)
writer.WriteString("trace", stackTrace);

if (message.Contains(Environment.NewLine))
writer.WriteString("message", Filter(message));
else
WriteArray("messages", message.Split(Environment.NewLine));

if (stackTrace is not null)
WriteArray("trace", stackTrace.Split(Environment.NewLine));

writer.WriteEndObject();
}

void WriteArray(string propertyName, IEnumerable<string> lines)
{
writer.WriteStartArray(propertyName);
foreach (var line in lines)
writer.WriteStringValue(Filter(line));
writer.WriteEndArray();
}
}

string? Filter(string? text)
=> text?.Replace("'", "`").Replace('\'', '`');
}
#endif
}

0 comments on commit f478f49

Please sign in to comment.