FractalDataWorks.MessageLogging.SourceGenerators
0.4.0-preview.6
dotnet add package FractalDataWorks.MessageLogging.SourceGenerators --version 0.4.0-preview.6
NuGet\Install-Package FractalDataWorks.MessageLogging.SourceGenerators -Version 0.4.0-preview.6
<PackageReference Include="FractalDataWorks.MessageLogging.SourceGenerators" Version="0.4.0-preview.6"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="FractalDataWorks.MessageLogging.SourceGenerators" Version="0.4.0-preview.6" />
<PackageReference Include="FractalDataWorks.MessageLogging.SourceGenerators"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add FractalDataWorks.MessageLogging.SourceGenerators --version 0.4.0-preview.6
#r "nuget: FractalDataWorks.MessageLogging.SourceGenerators, 0.4.0-preview.6"
#:package FractalDataWorks.MessageLogging.SourceGenerators@0.4.0-preview.6
#addin nuget:?package=FractalDataWorks.MessageLogging.SourceGenerators&version=0.4.0-preview.6&prerelease
#tool nuget:?package=FractalDataWorks.MessageLogging.SourceGenerators&version=0.4.0-preview.6&prerelease
FractalDataWorks.MessageLogging.SourceGenerators
Source generator that creates logger methods returning IGenericMessage. Fork of Microsoft's LoggerMessage source generator extended for the FractalDataWorks pattern where every logged message is also returned for Result integration.
Overview
This Roslyn incremental source generator processes methods decorated with [MessageLogging] and generates implementation code that:
- Logs to
ILoggerusing Microsoft.Extensions.Logging - Returns an
IGenericMessagecontaining the same message text
Target Framework
- netstandard2.0 (required for Roslyn source generators)
Installation
Reference this package as an analyzer:
From Reference.MessageLogging.csproj:24-26:
<ItemGroup>
<PackageReference Include="FractalDataWorks.MessageLogging.SourceGenerators" />
</ItemGroup>
Usage
Define a partial class with partial methods decorated with [MessageLogging]:
From OrderLog.cs:22-37:
public static partial class OrderLog
{
// =========================================================================
// DEBUG: Detailed internal operations
// =========================================================================
/// <summary>
/// Logs when order processing begins.
/// </summary>
[MessageLogging(
EventId = 8001,
Level = LogLevel.Debug,
Message = "Starting order processing for order '{orderId}'")]
public static partial IGenericMessage ProcessingStarted(
ILogger logger,
string orderId);
From OrderLog.cs:107-120:
// =========================================================================
// ERROR: Explicit failure modes (each one returns IGenericMessage for Result)
// =========================================================================
/// <summary>
/// Logs when an order is not found.
/// </summary>
[MessageLogging(
EventId = 8030,
Level = LogLevel.Error,
Message = "Order '{orderId}' not found")]
public static partial IGenericMessage OrderNotFound(
ILogger logger,
string orderId);
Use in service code with the "log AND return" pattern:
From OrderService.cs:115-126:
public IGenericResult<Order> GetOrder(string orderId)
{
// Simulate order not found
if (orderId.StartsWith("NOTFOUND"))
{
// Log AND return failure - the message is both logged and returned
return GenericResult<Order>.Failure(
OrderLog.OrderNotFound(_logger, orderId));
}
return GenericResult<Order>.Success(new Order { OrderId = orderId, Status = "Found" });
}
Generator Requirements
From LoggerMessageGenerator.Parser.cs:21:
The generator looks for methods with the attribute FractalDataWorks.MessageLogging.MessageLoggingAttribute.
From LoggerMessageGenerator.Parser.cs:275-281:
// Validate return type is IGenericMessage or compatible
if (!IsCompatibleReturnType(logMethodSymbol.ReturnType, genericMessageSymbol))
{
// logging methods must return IGenericMessage or a type that implements it
Diag(DiagnosticDescriptors.LoggingMethodMustReturnVoid, method.ReturnType.GetLocation());
keepMethod = false;
}
Methods must:
- Be
partialmethods - Return
IGenericMessageor a type implementing it - Have an
ILoggerparameter (for static methods) or class field - Not be generic
Generated Code
The generator creates implementation that logs to ILogger and returns IGenericMessage:
From LoggerMessageGenerator.Emitter.cs:487-493:
_builder.Append($@"
{nestedIndentation}return new global::FractalDataWorks.Messages.GenericMessage(
{nestedIndentation} {severityMapping},
{nestedIndentation} __messageText,
{nestedIndentation} {eventName},
{nestedIndentation} null);
{nestedIndentation}}}");
Related Packages
- FractalDataWorks.MessageLogging.Abstractions - Contains the
[MessageLogging]attribute - FractalDataWorks.Messages - Contains
IGenericMessageandGenericMessage - FractalDataWorks.Results - Contains
IGenericResult<T>for Result integration
API Reference
See MessageLogging wiki section for complete documentation.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages (19)
Showing the top 5 NuGet packages that depend on FractalDataWorks.MessageLogging.SourceGenerators:
| Package | Downloads |
|---|---|
|
CyberdyneDevelopment.Mc3Po.Tools.Abstractions
Tool abstractions for FractalDataWorks Roslyn development tools. Provides TypeCollections for tool categories and parameter types. |
|
|
CyberdyneDevelopment.Mc3Po.Protocols
Protocol infrastructure for mc3-po - base classes, provider, and ServiceType integration |
|
|
CyberdyneDevelopment.Mc3Po.Protocols.Plane
Plane protocol implementation for mc3-po - project management integration with Plane |
|
|
CyberdyneDevelopment.Mc3Po.Protocols.GitLab
GitLab protocol implementation for mc3-po - source control and project management integration |
|
|
CyberdyneDevelopment.Mc3Po.Protocols.GitHub
Development tools and utilities for the FractalDataWorks ecosystem. Build: |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|