How davidfineboym/loggingdecoratorgenerator works
Project Type and Purpose
• This is a .NET Source Generator library.
• It plugs into the Roslyn compiler and, at compile-time, scans annotated interfaces to emit “logging decorator” classes that wrap user interfaces with Microsoft.Extensions.Logging calls (and, optionally, metric instrumentation).
Top-Level Components (to be nodes
A. Developer Project Code (Input)
• Interfaces annotated with [DecorateWithLogger], [LogMethod], and [NotLogged].
B. Roslyn Compiler Platform
• Hosts ISourceGenerator implementations.
• Loads the “Fineboym.Logging.Generator” analyzer package.
C. LoggingDecoratorGenerator Package
1. Attributes.cs
2. Parser.cs – reads interface syntax trees.
3. MethodToGenerate.cs / Parameter.cs – data models representing methods and their parameters.
4. DecoratorGenerator.cs – entry point implementing ISourceGenerator.
5. DecoratorClass.cs & SourceGenerationHelper.cs – build the C# code nodes.
6. DiagnosticDescriptors.cs – defines compile-time warnings/errors.
7. RoslynExtensions.cs – helper methods for syntax analysis.
8. LogLevelConverter.cs – maps attribute settings to LogLevel.
D. Generated Output
• .g.cs files such as SomeServiceLoggingDecorator in the user’s namespace.
• Each method uses LoggerMessage.Define<> actions, optional try-catch, stopwatch for duration, and optional IMeterFactory metrics.
E. Test Projects
1. LoggingDecoratorGenerator.Tests
• Snapshot tests (Verify) that the generator produces the expected code.
2. LoggingDecoratorGenerator.IntegrationTests
• Compiles real sample interfaces and executes code to validate runtime behavior (e.g., metrics, exception logging).
Relationships and Data Flow
• Snapshot Tests drive the generator in isolation and compare output text
• Integration Tests run the generated decorator at runtime and verify logs/metrics
Architectural Patterns & Principles
• Plugin Pattern: the generator is a Roslyn analyzer plugin loaded by the compiler.
• Code-First Generation: source attributes drive code emission—no reflection at runtime.
• Single Responsibility: distinct modules for parsing, diagnostics, code construction, and log-level conversion.
• High-Performance Logging: uses LoggerMessage.Define<> and SkipEnabledCheck pattern.
• Optional Metrics: conditional injection of IMeterFactory and Histogram<double> based on ReportDurationAsMetric.
Must-Include Elements
• Clear component names (e.g., DecoratorGenerator, Parser, SourceGenerationHelper).
• Call out external dependencies: Microsoft.CodeAnalysis, Microsoft.Extensions.Logging, System.Diagnostics.Metrics.