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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Take a look at [benchmark](https://github.com/stbychkov/AutoLoggerMessage/wiki/B
so if you pass more than that, the default `Logger.Log(*, params object[] args)` will be executed.
* As this solution is based on interceptors, only .NET 8+ is supported
* Hash-based interceptors are incompatible with .NET SDK versions earlier than 8.0.8, most likely due to differences in the compiler version. To resolve this issue, please update your SDK to version [8.0.8](https://dotnet.microsoft.com/en-us/download/dotnet/8.0) or later.
* Generic arguments are https://github.com/dotnet/extensions/blob/ca2fe808b3d6c55817467f46ca58657456b4a928/docs/list-of-diagnostics.md?plain=1#L66C4-L66C13. If you pass a generic argument to the log function, the default `Logger.Log(*, params object[] args)` will be executed.

## Is something wrong?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@ private static void Log<T1, T2>(
""";

var (compilation, syntaxTree) = await CompileSourceCode($"Log({parameters});", extensionDeclaration);
var invocationExpression = syntaxTree.GetRoot().DescendantNodes().OfType<InvocationExpressionSyntax>().First();

var semanticModel = compilation.GetSemanticModel(syntaxTree);
var methodSymbol = (IMethodSymbol)semanticModel.GetSymbolInfo(invocationExpression).Symbol!;
var (_, methodSymbol, _) = FindLoggerMethodInvocation(compilation, syntaxTree);

var sut = new LogCallParametersExtractor();

var result = sut.Extract(message, methodSymbol);
var result = sut.Extract(message, methodSymbol!);

await Assert.That(result).IsEquivalentTo(new LogCallParameter[]
{
Expand Down Expand Up @@ -144,4 +141,29 @@ await Assert.That(result).IsEquivalentTo(new LogCallParameter[]
new("global::System.Int32", "@time", LogCallParameterType.Others),
});
}

[Test]
[Arguments("T")]
[Arguments("List<T>")]
public async Task Extract_WithGenericParameters_ShouldSkipExtractingParameters(string genericType)
{
var message = "{GenericParameter}";
var (compilation, syntaxTree) = await CompileSourceCode(string.Empty,
$$"""
private static void Log<T>(string {{MessageParameterName}}, T {{ParameterName}}) {}

public void Foo<T>({{genericType}} arg)
{
Log<{{genericType}}>("{{message}}", arg);
}
""");
var (_, methodSymbol, _) = FindLoggerMethodInvocation(compilation, syntaxTree);

var sut = new LogCallParametersExtractor();

var result = sut.Extract(message, methodSymbol!);

await Assert.That(result).IsNull();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ internal class LogCallParametersExtractor(LogPropertiesCheck? logPropertiesCheck
if (templateParametersNames.Length < methodParameters.Length)
return null;

// https://github.com/dotnet/extensions/blob/ca2fe808b3d6c55817467f46ca58657456b4a928/docs/list-of-diagnostics.md?plain=1#L66C4-L66C13
if (methodParameters.Any(IsGenericParameter))
return null;

var uniqueNameSuffix = ReservedParameterNameResolver.GenerateUniqueIdentifierSuffix(templateParametersNames);

var utilityParameters = methodSymbol.Parameters
Expand Down Expand Up @@ -70,4 +74,7 @@ private static LogCallParameter CreateLogCallParameter(ITypeSymbol @nativeType,

private static string TransformParameterName(string parameterName) =>
parameterName.StartsWith("@") ? parameterName : '@' + parameterName;

private static bool IsGenericParameter(IParameterSymbol parameterSymbol) =>
parameterSymbol.Type is INamedTypeSymbol { IsGenericType: true } or ITypeParameterSymbol;
}