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))

}
}
}

Copy link
Member

Choose a reason for hiding this comment

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

Can we generate warning when we run into EventSource that we are not able to generate the ctor for?

Copy link
Member Author

@EgorBo EgorBo Oct 29, 2025

Choose a reason for hiding this comment

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

@jkotas we have 3 that we can't generate for:

  • Microsoft.Extensions.DependencyInjection\src\DependencyInjectionEventSource.cs
  • Microsoft.Extensions.Logging.EventSource\src\LoggingEventSource.cs
  • System.Diagnostics.DiagnosticSource\src\System\Diagnostics\DiagnosticSourceEventSource.cs

they need to pass EventSourceSettings.EtwSelfDescribingEventFormat to the settings argument.

We have 3 options:

  1. Propose an API to add EventSourceSettings to the [EventSource] arg so we can define it there (and use from the SG)
  2. Leave some internal hint for the SG that we need EtwSelfDescribingEventFormat instead of the default (e.g. some constant field?)
  3. Leave these 3 as is

cc @stephentoub @noahfalk

Copy link
Member

Choose a reason for hiding this comment

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

I like (1)

Copy link
Member

Choose a reason for hiding this comment

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

they need to pass EventSourceSettings.EtwSelfDescribingEventFormat to the settings argument.

Why do these need to pass EventSourceSettings.EtwSelfDescribingEventFormat?

I would like to understand whether EtwSelfDescribingEventFormat is a corner-case that is only used in handful of places in runtime repo (3 is ok in that case), or whether it is something that gets used reasonably often (1 is worth doing in that case).

Similar concern about the other EventSource ctor arguments. How often are they used in practice? If they are used reasonably often, we may want to do some sort of extended version of (1).

Copy link
Member Author

@EgorBo EgorBo Oct 30, 2025

Choose a reason for hiding this comment

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

I would like to understand whether EtwSelfDescribingEventFormat is a corner-case that is only used in handful of places in runtime repo (3 is ok in that case), or whether it is something that gets used reasonably often (1 is worth doing in that case).

I'm afraid I don't know, perhaps someone from @dotnet/dotnet-diag can answer?

I also noticed there is an inconsistency among the base EventSource constructors:

protected EventSource() { }          // EtwManifestEventFormat
protected EventSource(bool) { }      // EtwManifestEventFormat
public EventSource(string) { }       // EtwSelfDescribingEventFormat
public EventSource(string, Guid) { } // EtwManifestEventFormat`

so in the end, it does look like we only have 3 ES that uses EtwSelfDescribingEventFormat

Copy link
Member

@noahfalk noahfalk Oct 30, 2025

Choose a reason for hiding this comment

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

Why do these need to pass EventSourceSettings.EtwSelfDescribingEventFormat?

  • DependencyInjectionEventSource - looks compatible with both formats. Changing from SelfDescribing to Manifest could prevent the EventSource from working properly with ETW profilers that don't use the TraceEvent library to abstract the data parsing
  • LoggingEventSource and DiagnosticSourceEventSource - Use IEnumerable<T> as an event parameter type which is only supported by SelfDescribingFormat

I would like to understand whether EtwSelfDescribingEventFormat is a corner-case that is only used in handful of places in runtime repo (3 is ok in that case), or whether it is something that gets used reasonably often (1 is worth doing in that case).

SelfDescribing is more than a corner case for 3P usage - it has more capabilities and better compatibility with ETW profilers that don't use the TraceEvent library. However it has higher per-event serialization overhead which is why most of our runtime EventSources don't use it.

Similar concern about the other EventSource ctor arguments. How often are they used in practice? If they are used reasonably often, we may want to do some sort of extended version of (1).

Aside from name, Guid, and EventSourceSettings enum there are only two other ctor arguments:

  • bool throwOnEventWriteErrors - this is superceded by EventSourceSettings so no reason to include it separately
  • string[] traits - Arbitrary key value pairs that can be associated with an EventSource. I don't recall seeing a real-world use. I believe it is rare but its not easy to back that up with simple code search results because both 'EventSource' and 'traits' find huge numbers of unrelated references.

I like (1)

Ditto 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, then let's put the PR on pause till the API proposal lands

Copy link
Member

@jkotas jkotas Oct 31, 2025

Choose a reason for hiding this comment

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

I do not think you need to wait for that API proposal to land. You can leave the 3 event sources that need that API proposal alone (disable the warning for them), and deal with the rest.

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 ok, then I guess it's ready for review

@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.


private readonly List<WeakReference<ServiceProvider>> _providers = new();

#pragma warning disable ESGEN001 // EventSource classes should not declare constructors.
Copy link
Member

Choose a reason for hiding this comment

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

Is there a tracking issue for these?

[EventSource(Name = "ActivityEventSource")]
class ActivityEventSource : EventSource
{
public ActivityEventSource() { }
Copy link
Member

Choose a reason for hiding this comment

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

Why was this necessary?

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.

Thoughts on what it would take to make this source generator part of the SDK?

private readonly List<WeakReference<ServiceProvider>> _providers = new();

#pragma warning disable ESGEN001 // EventSource classes should not declare constructors.
private DependencyInjectionEventSource() : base(EventSourceSettings.EtwSelfDescribingEventFormat)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
private DependencyInjectionEventSource() : base(EventSourceSettings.EtwSelfDescribingEventFormat)
// Uses EtwSelfDescribingEventFormat for backward compatibility
private DependencyInjectionEventSource() : base(EventSourceSettings.EtwSelfDescribingEventFormat)

Capture insights from PR comments

private static readonly char[] s_colon = new[] { ':' };

#pragma warning disable ESGEN001 // EventSource classes should not declare constructors.
private LoggingEventSource() : base(EventSourceSettings.EtwSelfDescribingEventFormat)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
private LoggingEventSource() : base(EventSourceSettings.EtwSelfDescribingEventFormat)
// This event source uses IEnumerable<T> as an event parameter type which is only supported by SelfDescribingFormat
private LoggingEventSource() : base(EventSourceSettings.EtwSelfDescribingEventFormat)

Comment on lines 408 to 411
private DiagnosticSourceEventSource()
// This constructor uses EventSourceSettings which is only available on V4.6 and above
// Use the EventSourceSettings to turn on support for complex types, if available (v4.6 and above).
: base(DiagnosticSourceEventSourceName, EventSourceSettings.EtwSelfDescribingEventFormat)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
private DiagnosticSourceEventSource()
// This constructor uses EventSourceSettings which is only available on V4.6 and above
// Use the EventSourceSettings to turn on support for complex types, if available (v4.6 and above).
: base(DiagnosticSourceEventSourceName, EventSourceSettings.EtwSelfDescribingEventFormat)
// This event source uses IEnumerable<T> as an event parameter type which is only supported by SelfDescribingFormat
private DiagnosticSourceEventSource() : base(DiagnosticSourceEventSourceName, EventSourceSettings.EtwSelfDescribingEventFormat)

new DiagnosticDescriptor(
"ESGEN001",
"EventSource class contains explicit constructor",
"EventSource class '{0}' contains an explicit constructor. EventSource classes must not declare constructors.",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"EventSource class '{0}' contains an explicit constructor. EventSource classes must not declare constructors.",
"EventSource class '{0}' contains an explicit constructor. EventSource classes must not declare constructors to take advantage of this source generator.",

?

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

Labels

needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add EventSource guid ctors for non-reflection creation

5 participants