Skip to content

Conversation

@EgorBo
Copy link
Member

@EgorBo EgorBo commented Oct 29, 2025

Closes #28290

  1. Remove [EventSourceAutoGenerate], we rely on just [EventSource] + no explicit ctors
  2. Introduce a new global SourceGenerators - EventSourceGenerator (and move the existing impl from SPC.Generators to this new project)
  3. Update slnx files (via the UpdateSolutionFiles task)
  4. Migrate all classes with [EventSource] to use the SG. Except for the ones with EventSourceSettings.EtwSelfDescribingEventFormat -- there are 3 ES impl defining this settings 🤔

@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Oct 29, 2025
@EgorBo EgorBo changed the title Es cleanup Introduce a new internal EventSourceGenerator and use it for all ES implementations Oct 29, 2025
@EgorBo
Copy link
Member Author

EgorBo commented Oct 29, 2025

hm.. probably should move the SLNX update to a separate (last) commit (or maybe even PR?)

…+ no explicit ctors

2) Introduce a new global SourceGenerators - EventSourceGenerator (and move the existing impl from SPC.Generators to this new project)
3) Update slnx files (via the UpdateSolutionFiles task)
4) Migrate all classes with [EventSource] to use the SG. Except for the ones with EtwSelfDescribingEventFormat
@stephentoub
Copy link
Member

we rely on just [EventSource] + no explicit ctors

We will likely want more than just this, e.g. it'll need to be partial.

@stephentoub
Copy link
Member

Update slnx files

Do we need to modify the slnx files? Could we instead globally include via .props?

@EgorBo
Copy link
Member Author

EgorBo commented Oct 29, 2025

Update slnx files

Do we need to modify the slnx files? Could we instead globally include via .props?

it's not needed to build the projects, but needed for people opening slnx files for the SG project to show up there in slnx. All other source generator projects are already added there (added automatically by the UpdateSolutionFile task, which is manually invoked (it's slow))

@EgorBo EgorBo marked this pull request as ready for review November 4, 2025 22:45
Copilot AI review requested due to automatic review settings November 4, 2025 22:45
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

This PR introduces a source generator for EventSource classes to automatically generate constructors and metadata, eliminating the need for manual boilerplate code. The generator creates parameterless constructors that call the base EventSource constructor with the correct parameter order (name, guid).

Key changes:

  • Introduces EventSourceGenerator source generator that triggers on classes with [EventSource] attribute
  • Removes [EventSourceAutoGenerate] attribute and makes affected EventSource classes partial
  • Changes EventSource base constructor parameter order from (Guid, string) to (string, Guid) for consistency
  • Removes manual constructors from EventSource classes across the codebase

Reviewed Changes

Copilot reviewed 50 out of 50 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
EventSourceGenerator.cs New source generator that creates constructors for EventSource classes
EventSourceGenerator.Parser.cs Parser logic to extract EventSource metadata and validate constructors
EventSourceGenerator.Emitter.cs Code generation logic for constructors and provider metadata
EventSource.cs Changes constructor parameter order and visibility for source generator
TraceLoggingEventSource.cs Updates constructor call to match new parameter order
Multiple EventSource classes Removes manual constructors and adds partial keyword
NetCoreAppLibrary.props Registers EventSourceGenerator
generators.targets Enables generator for source projects
Directory.Build.props Suppresses ESGEN001 warnings globally
Comments suppressed due to low confidence (1)

src/libraries/System.Private.CoreLib/gen/EventSourceGenerator.Parser.cs:24

  • The parser only checks for NamespaceDeclarationSyntax but doesn't check for FileScopedNamespaceDeclarationSyntax. This will cause the generator to skip classes in file-scoped namespaces. Use BaseNamespaceDeclarationSyntax as the base type to handle both traditional and file-scoped namespace declarations.

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

Delete redundant comments

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

Unused?

public EventSource(string eventSourceName, System.Diagnostics.Tracing.EventSourceSettings config) { }
public EventSource(string eventSourceName, System.Diagnostics.Tracing.EventSourceSettings config, params string[]? traits) { }
public EventSource(string eventSourceName, Guid eventSourceGuid) { }
public EventSource(string eventSourceName, Guid eventSourceGuid, System.Diagnostics.Tracing.EventSourceSettings settings, params string[]? traits) { }
Copy link
Member

Choose a reason for hiding this comment

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

The approved API shape (and the implementation) has default null value for traits, but this has params and no null value. Which one is correct?

Do we need to add a test to validate that the new constructor works as expected?

Copy link
Member Author

Choose a reason for hiding this comment

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

@jkotas sorry, I am not following - what that test is expected to cover?

PS: added = null and removed params to match exactly the approved shape. Presumably, this particular API is mostly for the SG.

Copy link
Member Author

@EgorBo EgorBo Nov 10, 2025

Choose a reason for hiding this comment

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

Although, isn't params string[] x and string[] x = null are the same thing? 🤔
I guess adding params (but not removing) shouldn't be a breaking change. However, feels like collections literals effectively made params redundant

Copy link
Member

Choose a reason for hiding this comment

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

Although, isn't params string[] x and string[] x = null are the same thing?

No, if you don't pass an argument for the latter, x will be null, but if you don't pass an argument for the former, x will be Array.Empty<string>().

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, indeed

Copy link
Member

Choose a reason for hiding this comment

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

what that test is expected to cover?

The tests for the existing APIs are in https://github.com/dotnet/runtime/blob/5bceb8184fcde55143d2102a65c72bd6233e950b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsTraits.cs and related files.

Add some tests like that for the new APIs?

}
}

// FrameworkEventSource is on the startup path for the framework, so we have this internal overload that it can use
Copy link
Member

Choose a reason for hiding this comment

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

This comment should be deleted and replaced with tripple slash comment that will be used to populate the docs.

{ }

// Used by the internal FrameworkEventSource constructor and the TraceLogging-style event source constructor
internal EventSource(Guid eventSourceGuid, string eventSourceName, EventSourceSettings settings, string[]? traits = null)
Copy link
Member

Choose a reason for hiding this comment

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

Same here

@EgorBo
Copy link
Member Author

EgorBo commented Nov 11, 2025

@copilot address the recent feedback

@EgorBo
Copy link
Member Author

EgorBo commented Nov 11, 2025

oh, that doesn't work that way?

@jkotas
Copy link
Member

jkotas commented Nov 11, 2025

oh, that doesn't work that way?

You can ask it to create a PR against your branch, but it will never do a direct push into your branch currently.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add EventSource guid ctors for non-reflection creation

6 participants