Skip to content

Apply the format and alignment of enum values in interpolated strings - #260

Open
aetos382 wants to merge 1 commit into
Cysharp:masterfrom
aetos382:enum-format
Open

aetos382 wants to merge 1 commit into
Cysharp:masterfrom
aetos382:enum-format

Conversation

@aetos382

Copy link
Copy Markdown
Contributor

Since 2.5.0 (#164), enum values in ZLog interpolated strings are written with the names cached in MagicalBox, and those writers ignore the format string: {x:D} writes Orange instead of 2, 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 as Orange without padding, while the message string passed to ILogger.Log is 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.AppendFormatted ignores 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.

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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@aetos382

Copy link
Copy Markdown
Contributor Author

The license-check failure of PR Harness is unrelated to this change. Cysharp/Actions@7dc160a enabled the check by default, and the existing dependencies on master do not pass it, so every PR against master fails the same way. The 32 violations come from:

  • The old System.* packages that xunit 2.x brings in through NETStandard.Library 1.6.1, in the test projects and sandbox/ConsoleApp, and that Serilog.Sinks.TextWriter 2.1.0 brings in, in sandbox/Benchmark. Their licenses are given only as a URL.
  • xunit 2.4.1 and its packages, and xunit.abstractions 2.0.3, whose licenses are not recognized.

#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 MagicalBox and EnumTest. If the fix is wanted earlier, for example in a 2.6.x release, it can be merged before them despite the failing check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants