Repository navigation
Conversation
Since 2.5.0 (Cysharp#164), enum values in ZLog interpolated strings are written with the cached names in MagicalBox, which ignored the format string, so `{x:D}` wrote the name instead of the underlying value. The UTF-8 output also ignored the alignment, so `{x,10}` was not padded. - Use the cached name only when no format string is specified. - With a format string, format the value to a string before writing it as UTF-8, because Utf8StringWriter.AppendFormatted ignores the format of enum values on .NET 8 or later. - With an alignment, write the name through the string overload, which pads by the char count. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the enum formatting and alignment updates.
No open issues 🎉
What changed in this PR
Updates ZLogger’s runtime interpolated-string handling so enum formats and alignment match C# interpolation in both message strings and UTF-8 output.
Changes:
- Applies explicit enum formats instead of always using cached names.
- Adds UTF-8 padding for cached enum names while preserving the fast path for unformatted, unaligned values.
- Adds regression tests for formats, alignment, combined formatting, and undefined enum values across both outputs.
| File | Description |
|---|---|
tests/ZLogger.Tests/EnumTest.cs |
Adds enum formatting and alignment tests against C# interpolation. |
src/ZLogger/Internal/MagicalBox.cs |
Honors explicit enum formats and pads cached names in UTF-8 output. |
Review effort: Balanced
|
The
#252 moves the tests to xunit v3 and the sandbox to Serilog.Sinks.TextWriter 3.0.0, and the check passes there. Once #252 is merged, I will merge master into this branch. This PR does not depend on #252 or #253: it changes only |
Since 2.5.0 (#164), enum values in
ZLoginterpolated strings are written with the names cached in MagicalBox, and those writers ignore the format string:{x:D}writesOrangeinstead of2, both in the message string and in the UTF-8 output. The UTF-8 writer also ignores the alignment, so through the ZLogger providers{x,10}is written asOrangewithout padding, while the message string passed toILogger.Logis padded. Before 2.5.0, enum values took the path of their underlying type, so{x:D}happened to work, and{x}was the bug that #164 reported.The cached name is now used only when no format string is specified, the only case in which it is what the value formats to. With a format string, the UTF-8 writer formats the value to a string first, because
Utf8StringWriter.AppendFormattedignores the format of enum values on .NET 8 or later. The same problem affects the UTF-8 output of[ZLoggerMessage]methods, which needs a fix in Utf8StringInterpolation. With an alignment, the name is written through the string overload, which pads by the char count.{x}without an alignment still copies the cached UTF-8 name, so the common case is unchanged; only enum values with a format string allocate a string.The tests compare both the message string and the UTF-8 output with C# interpolated strings, for format strings, alignments, both combined, and an undefined value.