Add MA0226: EventSource class should be sealed - #1446
Merged
Merged
Conversation
A class deriving from EventSource must be sealed or abstract. The runtime validates it: EventSource.GenerateManifest reports "Event source types must be sealed or abstract." under EventManifestOptions.Strict. Deriving from a concrete event source also breaks the derived type, as EventSource only walks through abstract base types when looking for its EventSource base class, and builds the event metadata from the methods declared on the type itself. The abstract "Utility EventSource" pattern is supported, so abstract classes are not reported. The rule also recognizes the Microsoft.Diagnostics.Tracing base type of the EventSource NuGet package. The rule is disabled by default.
…base type Array.TrueForAll and Array.Exists invoke a delegate, and the lambda captured the analyzed symbol, so a closure was allocated for every named type. The two symbols are now properties of the analyzer context, checked directly.
meziantou
enabled auto-merge (squash)
September 10, 2026 00:58
meziantou
disabled auto-merge
September 10, 2026 00:58
meziantou
enabled auto-merge (squash)
September 10, 2026 00:58
This was referenced Sep 10, 2026
Open
Bump Meziantou.Analyzer from 3.0.139 to 3.0.234
Analogy-LogViewer/Analogy.LogViewer.NLog.Targets#568
Closed
Closed
Bump Meziantou.Analyzer from 3.0.139 to 3.0.235
Analogy-LogViewer/Analogy.AspNetCore.LogProvider#554
Closed
Closed
Open
Closed
This was referenced Sep 17, 2026
Open
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
New rule MA0226 - EventSource class should be sealed, reported on a class that inherits from
EventSourceand is neithersealednorabstract. Disabled by default (Warningseverity when enabled), with a code fix adding thesealedmodifier.Why
Sealing a derived event source is not only a documented convention — the runtime validates it:
EventSource.CreateManifestAndDescriptorsreportsEvent source types must be sealed or abstract.when the type is neithersealednorabstract(only underEventManifestOptions.Strict, i.e.GenerateManifestvalidation, so an unsealed source does not throw at construction on its own).GetEventSourceBaseTypewalks up the base chain skipping only abstract types and requires the first non-abstract ancestor to be exactlyEventSource, so a class deriving from a concrete event source is not a valid event source.eventSourceType.GetMethods(BindingFlags.DeclaredOnly | ...), so events declared on a concrete base class are never registered for the derived provider (which also gets its own provider name/GUID).The
abstract"Utility EventSource" pattern is explicitly supported by the framework, so abstract classes are not reported.Notes for reviewers
Microsoft.Diagnostics.Tracing.EventSource(the EventSource NuGet redist type), matching the runtime's own namespace-tolerant handling ofDiagnostics.Tracingattributes.publictypes by default, and isInfo. An unsealed concreteEventSourceis wrong regardless of visibility or whether anything derives from it today.docs/comparison-with-other-analyzers.md: no equivalent or similar rule found in the CA/IDE/Sonar rule sets.Verification
dotnet build(all Roslyn versions): succeeded, 0 warnings.dotnet test tests/Meziantou.Analyzer.Test/Meziantou.Analyzer.Test.roslyn5.9.csproj: 4240/4240 passed.dotnet test tests/Meziantou.Analyzer.Test/Meziantou.Analyzer.Test.roslyn4.8.csproj --filter "FullyQualifiedName~EventSourceMustBeSealedAnalyzerTests": 7/7 passed.dotnet run --project src/DocumentationGeneratorre-run after the doc edits: exit code 0, no further markdown changes.