Skip to content

Conversation

@John-Leitch
Copy link
Contributor

This PR addresses bug #123786 by escaping all non-printable characters (< 0x20) in the emitted message. \r and \n are emitted using the same escape sequence, while other non-printable characters are emitted as hex escape sequences e.g. \x00. Escaping of \ is also addressed. Finally, an additional test was added to cover these behaviors.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jan 30, 2026
@cincuranet cincuranet linked an issue Jan 30, 2026 that may be closed by this pull request
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes LoggerMessageGenerator emitting malformed C# string literals by escaping backslashes and all non-printable characters (< 0x20) when generating source, and adds a regression test covering these escaping scenarios.

Changes:

  • Update the emitter to escape \ and all control characters (< 0x20) using \xNN (while keeping \r/\n as named escapes).
  • Add a new theory test validating the generated source is syntactically valid and contains the expected escaped message literal.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Emitter.cs Extends message escaping to include backslashes and all control characters so generated string literals remain well-formed.
src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorEmitterTests.cs Adds a theory-based regression test for message escaping (backslash, quotes, CR/LF, and \xNN escapes).

cincuranet and others added 2 commits January 30, 2026 10:41
…Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorEmitterTests.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Moved the diagnostics retrieval after the source text assertion to ensure proper validation of generated diagnostics.
@tarekgh
Copy link
Member

tarekgh commented Jan 30, 2026

    private static string ConvertEndOfLineAndQuotationCharactersToEscapeForm(string s)

We should get rid of this method and just call SymbolDisplay.FormatLiteral. Here is example of how the extension source gen does it https://github.com/dotnet/extensions/blob/140e4055f7b12d4cf29ec63924c88520ccfc6064/src/Generators/Microsoft.Gen.Logging/Emission/Emitter.Utils.cs#L16


Refers to: src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Emitter.cs:580 in 2f6a0f4. [](commit_id = 2f6a0f4, deletion_comment = False)

@tarekgh tarekgh added the needs-author-action An issue or pull request that requires more info or actions from the author. label Jan 31, 2026
@dotnet-policy-service dotnet-policy-service bot removed the needs-author-action An issue or pull request that requires more info or actions from the author. label Jan 31, 2026
@John-Leitch
Copy link
Contributor Author

@tarekgh, pushed your suggested changes and added some additional test coverage.

Copy link
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

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

Thanks @John-Leitch. I pushed a small test fix, LGTM otherwise.

@tarekgh tarekgh merged commit 8acd159 into dotnet:main Feb 2, 2026
84 of 89 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Extensions-Logging community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LoggerMessageGenerator emits malformed strings.

4 participants