Skip to content

Suppress EventSource trim warnings on BeginExecute and EndExecute - #4703

Open
charlesroddie wants to merge 1 commit into
dotnet:mainfrom
charlesroddie:trim-eventsource
Open

charlesroddie wants to merge 1 commit into
dotnet:mainfrom
charlesroddie:trim-eventsource

Conversation

@charlesroddie

Copy link
Copy Markdown

Part of the work toward #1947.

Problem

SqlClientEventSource.BeginExecute and EndExecute are the only events without a typed EventSource.WriteEvent overload for their argument lists, so they call WriteEvent(int, params object[]). That overload is marked RequiresUnreferencedCode, so trimming or Native AOT publishing an application that uses SqlClient reports IL2026 for both.

The library analyzer never shows these, because the Logging project targets netstandard2.0, whose reference assemblies don't carry the annotation. They appear only when an application is published.

Change

Suppress IL2026 on both methods. The runtime's message for the overload says the warning "can be suppressed if the object is a primitive type", and these payloads are only int and string. This is the same justification the runtime uses for its own typed overloads.

Rewriting the methods with WriteEventCore would not avoid the suppression: WriteEventCore carries the same annotation. It would also need unsafe code, and the payload encoding would need to stay exactly as it is for OpenTelemetry and Application Insights.

netstandard2.0 has no UnconditionalSuppressMessageAttribute, so the Logging project gets an internal copy, as dotnet/runtime does for its own netstandard2.0 builds. The trimmer recognizes the attribute by name. The Logging assembly grants internals access only to a test assembly that doesn't exist, so the copy can't clash with the public attribute anywhere.

No change to the events' names, opcodes or payloads.

Testing

  • Native AOT publish, with TrimmerSingleWarn=false, of a small ASP.NET Core application that opens a connection, runs a stored procedure and reads results: SqlClient's reachable warnings go from 42 to 40, the two removed being these.
  • An EventListener on Microsoft.Data.SqlClient.EventSource receives both events with the same names, opcodes, payload names and values, and no EventSource error events.
  • The manual EventSourceTest needs a SQL Server instance and was not run.

🤖 Generated with Claude Code

These events have no typed EventSource.WriteEvent overload, so they use
WriteEvent(int, object[]), which requires unreferenced code.  Its message
says the warning can be suppressed when the arguments are primitive, and
these payloads are only int and string.  WriteEventCore carries the same
annotation, so rewriting them would still need the suppression.

The Logging project targets netstandard2.0, which lacks
UnconditionalSuppressMessageAttribute, so add an internal copy; the
trimmer recognizes it by name.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings September 16, 2026 18:43
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Sep 16, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The warning suppression lacks an automated trimming or Native AOT regression test.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Suppresses two Native AOT trimming warnings, advancing issue #1947.

Changes:

  • Adds a netstandard2.0 suppression-attribute polyfill.
  • Suppresses IL2026 for primitive-only execution events.
File summaries
File Description
UnconditionalSuppressMessageAttribute.cs Adds the trimmer-recognized polyfill.
SqlClientEventSource.cs Suppresses warnings on execution events.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +1810 to +1811
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026",
Justification = "The payload is only int and string values, which EventSource writes without reflection.")]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Trim warnings are being tested locally in order to make the reports in the descriptions. A small app could be checked in so there is confirmation in the repo but perhaps in a separate PR?

@charlesroddie
charlesroddie marked this pull request as ready for review September 16, 2026 20:16
@charlesroddie
charlesroddie requested a review from a team as a code owner September 16, 2026 20:16
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@mdaigle mdaigle added this to the 8.0.0-preview1 milestone Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To triage

Development

Successfully merging this pull request may close these issues.

4 participants