未验证 提交 0713627a 编写于 作者: M Maryam Ariyan 提交者: GitHub

Fixes InvalidCastException when using other attributes (#67179)

- alongside LoggerMessageAttribute

Fixes #67167
上级 e961b109
......@@ -141,15 +141,20 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
}
bool hasMisconfiguredInput = false;
ImmutableArray<AttributeData>? boundAttrbutes = logMethodSymbol?.GetAttributes();
ImmutableArray<AttributeData>? boundAttributes = logMethodSymbol?.GetAttributes();
if (boundAttrbutes == null)
if (boundAttributes == null || boundAttributes!.Value.Length == 0)
{
continue;
}
foreach (AttributeData attributeData in boundAttrbutes)
foreach (AttributeData attributeData in boundAttributes)
{
if (attributeData.AttributeClass?.Equals(loggerMessageAttribute) != true)
{
continue;
}
// supports: [LoggerMessage(0, LogLevel.Warning, "custom message")]
// supports: [LoggerMessage(eventId: 0, level: LogLevel.Warning, message: "custom message")]
if (attributeData.ConstructorArguments.Any())
......
......@@ -16,6 +16,21 @@ namespace Microsoft.Extensions.Logging.Generators.Tests
[ActiveIssue("https://github.com/dotnet/runtime/issues/52062", TestPlatforms.Browser)]
public class LoggerMessageGeneratorParserTests
{
[Fact]
public async Task Valid_AdditionalAttributes()
{
Assert.Empty(await RunGenerator($@"
using System.Diagnostics.CodeAnalysis;
partial class C
{{
[SuppressMessage(""CATEGORY1"", ""SOMEID1"")]
[LoggerMessage(EventId = 0, Level = LogLevel.Debug, Message = ""M1"")]
[SuppressMessage(""CATEGORY2"", ""SOMEID2"")]
static partial void M1(ILogger logger);
}}
"));
}
[Fact]
public async Task InvalidMethodName()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册