Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using AutoLoggerMessageGenerator.Models;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

Expand All @@ -12,6 +13,8 @@ internal static class GeneratorOptionsProvider
private const string GenerateSkipNullPropertiesKey = $"{CommonPrefix}_{nameof(SourceGeneratorConfiguration.GenerateSkipNullProperties)}";
private const string GenerateTransitiveKey = $"{CommonPrefix}_{nameof(SourceGeneratorConfiguration.GenerateTransitive)}";
private const string OverrideBeginScopeBehaviorKey = $"{CommonPrefix}_{nameof(SourceGeneratorConfiguration.OverrideBeginScopeBehavior)}";
private const string DefaultEventIdKey = $"{CommonPrefix}_{nameof(SourceGeneratorConfiguration.DefaultEventId)}";
private const string DefaultEventNameKey = $"{CommonPrefix}_{nameof(SourceGeneratorConfiguration.DefaultEventId)}_Name";

public static IncrementalValueProvider<SourceGeneratorConfiguration> Provide(IncrementalGeneratorInitializationContext context) =>
context.AnalyzerConfigOptionsProvider.Select((options, _) => new SourceGeneratorConfiguration(
Expand All @@ -20,7 +23,8 @@ public static IncrementalValueProvider<SourceGeneratorConfiguration> Provide(Inc
GenerateOmitReferenceName: GetValue(options.GlobalOptions, GenerateOmitReferenceNameKey, false),
GenerateSkipNullProperties: GetValue(options.GlobalOptions, GenerateSkipNullPropertiesKey, false),
GenerateTransitive: GetValue(options.GlobalOptions, GenerateTransitiveKey, false),
OverrideBeginScopeBehavior: GetValue(options.GlobalOptions, OverrideBeginScopeBehaviorKey, true)
OverrideBeginScopeBehavior: GetValue(options.GlobalOptions, OverrideBeginScopeBehaviorKey, true),
DefaultEventId: GetDefaultEventId(options.GlobalOptions)
));

private static bool GetValue(AnalyzerConfigOptions options, string key, bool defaultValue = true) =>
Expand All @@ -31,4 +35,15 @@ private static bool IsFeatureEnabled(string value, bool defaultValue) =>
|| StringComparer.OrdinalIgnoreCase.Equals("enabled", value)
|| StringComparer.OrdinalIgnoreCase.Equals("true", value)
|| (bool.TryParse(value, out var boolVal) ? boolVal : defaultValue);

private static EventId? GetDefaultEventId(AnalyzerConfigOptions options)
{
if (options.TryGetValue(DefaultEventIdKey, out var defaultEventIdString) is false ||
int.TryParse(defaultEventIdString, out var parsedDefaultEventId) is false)
return null;

options.TryGetValue(DefaultEventNameKey, out var eventName);

return new EventId(parsedDefaultEventId, eventName);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using AutoLoggerMessageGenerator.Models;

namespace AutoLoggerMessageGenerator.Configuration;

internal record struct SourceGeneratorConfiguration
Expand All @@ -9,5 +11,6 @@ internal record struct SourceGeneratorConfiguration
bool GenerateOmitReferenceName,
bool GenerateSkipNullProperties,
bool GenerateTransitive,
bool OverrideBeginScopeBehavior
bool OverrideBeginScopeBehavior,
EventId? DefaultEventId = null
);
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ namespace AutoLoggerMessageGenerator.Generators;

public partial class AutoLoggerMessageGenerator
{
private static void GenerateLoggerMessages(IncrementalGeneratorInitializationContext context,
IncrementalValueProvider<SourceGeneratorConfiguration> configuration, IncrementalValueProvider<ImmutableArray<Reference>> modulesProvider)
private static void GenerateLoggerMessages(
IncrementalGeneratorInitializationContext context,
IncrementalValueProvider<SourceGeneratorConfiguration> configuration,
IncrementalValueProvider<ImmutableArray<Reference>> modulesProvider)
{
var logCallsProvider = context.SyntaxProvider.CreateSyntaxProvider(
LogMessageCallFilter.IsLogCallInvocation,
Expand Down
3 changes: 3 additions & 0 deletions src/AutoLoggerMessageGenerator/Models/EventId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace AutoLoggerMessageGenerator.Models;

internal record struct EventId(int Id, string? Name);
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,38 @@ private MethodDeclarationSyntax GenerateMethod(LogMessageCall logMessageCall)

private AttributeListSyntax GenerateLoggerMessageAttribute(LogMessageCall logMessageCall)
{
var attributeArguments = SeparatedList(new[]
{
AttributeArgument(
ParseExpression(
$"Level = {Constants.DefaultLoggingNamespace}.LogLevel.{logMessageCall.LogLevel}")),
AttributeArgument(
AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
IdentifierName("Message"),
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(logMessageCall.Message)))),
AttributeArgument(
ParseExpression(
$"SkipEnabledCheck = {configuration.GenerateSkipEnabledCheck.ToLowerBooleanString()}"))
});

if (configuration.DefaultEventId is not null)
{
attributeArguments = attributeArguments.Add(
AttributeArgument(
ParseExpression($"EventId = {configuration.DefaultEventId.Value.Id}")
)
);
attributeArguments = attributeArguments.Add(
AttributeArgument(
ParseExpression($"EventName = \"{configuration.DefaultEventId.Value.Name}\"")
)
);
}

var attribute = Attribute(ParseName(LoggerMessageAttributeName))
.WithArgumentList(
AttributeArgumentList(
SeparatedList(new[]
{
AttributeArgument(
ParseExpression(
$"Level = {Constants.DefaultLoggingNamespace}.LogLevel.{logMessageCall.LogLevel}")),
AttributeArgument(
AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
IdentifierName("Message"),
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(logMessageCall.Message)))),
AttributeArgument(
ParseExpression(
$"SkipEnabledCheck = {configuration.GenerateSkipEnabledCheck.ToLowerBooleanString()}"))
})
SeparatedList(attributeArguments)
)
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Microsoft.Extensions.Logging.AutoLoggerMessage
{
static partial class AutoLoggerMessage
{
// <LogMessageCallMappingLocation>: Guid_1
[Microsoft.Extensions.Logging.LoggerMessageAttribute(Level = Microsoft.Extensions.Logging.LogLevel.Critical, Message = "Hello, World!", SkipEnabledCheck = false, EventId = 0, EventName = "")]
internal static partial void Log_SomeNamespace_SomeClass_Method_2_22(Microsoft.Extensions.Logging.ILogger Logger, int @intParam, string @stringParam, bool @boolParam, SomeClass @classParam, SomeStruct @structParam);
// <LogMessageCallMappingLocation>: Guid_2
[Microsoft.Extensions.Logging.LoggerMessageAttribute(Level = Microsoft.Extensions.Logging.LogLevel.Trace, Message = "Goodbye, World!", SkipEnabledCheck = false, EventId = 0, EventName = "")]
internal static partial void Log_SomeNamespace_SomeClass_Method_3_33(Microsoft.Extensions.Logging.ILogger Logger, int @intParam, string @stringParam, bool @boolParam, SomeClass @classParam, SomeStruct @structParam);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,16 @@ _logMessageCall with
GenerateTransitive: false,
OverrideBeginScopeBehavior: false),
false),
() => (
"with_empty_event_id",
new SourceGeneratorConfiguration(
GenerateInterceptorAttribute: false,
GenerateSkipEnabledCheck: false,
GenerateOmitReferenceName: false,
GenerateSkipNullProperties: false,
GenerateTransitive: false,
OverrideBeginScopeBehavior: false,
DefaultEventId: new EventId(0, null)),
false),
];
}
Loading