Skip to content

Commit

Permalink
Fixes InvalidCastException when using other attributes (dotnet#67179)
Browse files Browse the repository at this point in the history
- alongside LoggerMessageAttribute

Fixes dotnet#67167
  • Loading branch information
maryamariyan authored and allantargino committed Dec 20, 2022
1 parent cdf48ab commit 02bfb84
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ namespace Microsoft.Extensions.Logging.Generators.Tests
[ActiveIssue("https://github.com/dotnet/runtime/issues/32743", TestRuntimes.Mono)]
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()
{
Expand Down

0 comments on commit 02bfb84

Please sign in to comment.