Skip to content

Commit

Permalink
Pre-encode property names of the JSON (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexaka1 authored Aug 29, 2024
1 parent b91747b commit a7ee0b8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-paws-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"alexaka1.serilog.extensions.formatting": patch
---

Small performance increase by pre-encoding the default property names, to be able to write them to stream as-is.
52 changes: 26 additions & 26 deletions src/Serilog.Extensions.Formatting/JsonLogPropertyNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ internal readonly struct JsonLogPropertyNames
{
public JsonLogPropertyNames(JsonNamingPolicy namingPolicy)
{
Timestamp = namingPolicy.ConvertName(TimestampPropertyName);
Level = namingPolicy.ConvertName(LevelPropertyName);
MessageTemplate = namingPolicy.ConvertName(MessageTemplatePropertyName);
RenderedMessage = namingPolicy.ConvertName(RenderedMessagePropertyName);
TraceId = namingPolicy.ConvertName(TraceIdPropertyName);
SpanId = namingPolicy.ConvertName(SpanIdPropertyName);
Exception = namingPolicy.ConvertName(ExceptionPropertyName);
Properties = namingPolicy.ConvertName(PropertiesPropertyName);
Renderings = namingPolicy.ConvertName(RenderingsPropertyName);
Null = namingPolicy.ConvertName(NullPropertyName);
TypeTag = namingPolicy.ConvertName(TypeTagPropertyName);
Format = namingPolicy.ConvertName(FormatPropertyName);
Rendering = namingPolicy.ConvertName(RenderingPropertyName);
Timestamp = JsonEncodedText.Encode(namingPolicy.ConvertName(TimestampPropertyName));
Level = JsonEncodedText.Encode(namingPolicy.ConvertName(LevelPropertyName));
MessageTemplate = JsonEncodedText.Encode(namingPolicy.ConvertName(MessageTemplatePropertyName));
RenderedMessage = JsonEncodedText.Encode(namingPolicy.ConvertName(RenderedMessagePropertyName));
TraceId = JsonEncodedText.Encode(namingPolicy.ConvertName(TraceIdPropertyName));
SpanId = JsonEncodedText.Encode(namingPolicy.ConvertName(SpanIdPropertyName));
Exception = JsonEncodedText.Encode(namingPolicy.ConvertName(ExceptionPropertyName));
Properties = JsonEncodedText.Encode(namingPolicy.ConvertName(PropertiesPropertyName));
Renderings = JsonEncodedText.Encode(namingPolicy.ConvertName(RenderingsPropertyName));
Null = JsonEncodedText.Encode(namingPolicy.ConvertName(NullPropertyName));
TypeTag = JsonEncodedText.Encode(namingPolicy.ConvertName(TypeTagPropertyName));
Format = JsonEncodedText.Encode(namingPolicy.ConvertName(FormatPropertyName));
Rendering = JsonEncodedText.Encode(namingPolicy.ConvertName(RenderingPropertyName));
}

private const string TimestampPropertyName = "Timestamp";
Expand All @@ -35,18 +35,18 @@ public JsonLogPropertyNames(JsonNamingPolicy namingPolicy)
private const string FormatPropertyName = "Format";
private const string RenderingPropertyName = "Rendering";

public string Timestamp { get; }
public string Level { get; }
public string MessageTemplate { get; }
public string RenderedMessage { get; }
public string TraceId { get; }
public string SpanId { get; }
public string Exception { get; }
public string Properties { get; }
public string Renderings { get; }
public string Null { get; }
public string TypeTag { get; }
public string Format { get; }
public string Rendering { get; }
public JsonEncodedText Timestamp { get; }
public JsonEncodedText Level { get; }
public JsonEncodedText MessageTemplate { get; }
public JsonEncodedText RenderedMessage { get; }
public JsonEncodedText TraceId { get; }
public JsonEncodedText SpanId { get; }
public JsonEncodedText Exception { get; }
public JsonEncodedText Properties { get; }
public JsonEncodedText Renderings { get; }
public JsonEncodedText Null { get; }
public JsonEncodedText TypeTag { get; }
public JsonEncodedText Format { get; }
public JsonEncodedText Rendering { get; }
}
}
10 changes: 9 additions & 1 deletion src/Serilog.Extensions.Formatting/Utf8JsonFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,15 @@ private void VisitDictionaryValue(DictionaryValue dictionary, Utf8JsonWriter wri
foreach (var element in dictionary.Elements)
{
string key = element.Key.Value?.ToString();
writer.WritePropertyName(key != null ? _namingPolicy.ConvertName(key) : _names.Null);
if (key != null)
{
writer.WritePropertyName(_namingPolicy.ConvertName(key));
}
else
{
writer.WritePropertyName(_names.Null);
}

Format(element.Value, writer);
}

Expand Down

0 comments on commit a7ee0b8

Please sign in to comment.