Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-encode property names of the JSON #93

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -220,7 +220,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