From 01f9d6aaa7b0c8e8f554c9841705f826852f876f Mon Sep 17 00:00:00 2001 From: Martossy Alex Date: Thu, 29 Aug 2024 15:04:47 +0200 Subject: [PATCH] Pre-encode property names of the JSON --- .changeset/grumpy-paws-vanish.md | 5 ++ .../JsonLogPropertyNames.cs | 52 +++++++++---------- .../Utf8JsonFormatter.cs | 10 +++- 3 files changed, 40 insertions(+), 27 deletions(-) create mode 100644 .changeset/grumpy-paws-vanish.md diff --git a/.changeset/grumpy-paws-vanish.md b/.changeset/grumpy-paws-vanish.md new file mode 100644 index 0000000..b394dbb --- /dev/null +++ b/.changeset/grumpy-paws-vanish.md @@ -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. diff --git a/src/Serilog.Extensions.Formatting/JsonLogPropertyNames.cs b/src/Serilog.Extensions.Formatting/JsonLogPropertyNames.cs index bae2408..99c3544 100644 --- a/src/Serilog.Extensions.Formatting/JsonLogPropertyNames.cs +++ b/src/Serilog.Extensions.Formatting/JsonLogPropertyNames.cs @@ -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"; @@ -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; } } } diff --git a/src/Serilog.Extensions.Formatting/Utf8JsonFormatter.cs b/src/Serilog.Extensions.Formatting/Utf8JsonFormatter.cs index e8fc261..bcc501e 100644 --- a/src/Serilog.Extensions.Formatting/Utf8JsonFormatter.cs +++ b/src/Serilog.Extensions.Formatting/Utf8JsonFormatter.cs @@ -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); }