Skip to content

Commit cc0d010

Browse files
Fixes #6598 (#6600)
Removes Conditional attribute from the definition of LogProperties and LogPropertyIgnore attributes
1 parent d651ccc commit cc0d010

File tree

10 files changed

+71
-4
lines changed

10 files changed

+71
-4
lines changed

src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/LogPropertiesAttribute.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5-
using System.Diagnostics;
65
using System.Diagnostics.CodeAnalysis;
76
using Microsoft.Extensions.Logging;
87
using Microsoft.Shared.DiagnosticIds;
@@ -14,7 +13,6 @@ namespace Microsoft.Extensions.Logging;
1413
/// </summary>
1514
/// <seealso cref="LoggerMessageAttribute"/>
1615
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property)]
17-
[Conditional("CODE_GENERATION_ATTRIBUTES")]
1816
public sealed class LogPropertiesAttribute : Attribute
1917
{
2018
/// <summary>

src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/LogPropertyIgnoreAttribute.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5-
using System.Diagnostics;
65
using Microsoft.Extensions.Logging;
76

87
namespace Microsoft.Extensions.Logging;
@@ -12,7 +11,6 @@ namespace Microsoft.Extensions.Logging;
1211
/// </summary>
1312
/// <seealso cref="LoggerMessageAttribute"/>.
1413
[AttributeUsage(AttributeTargets.Property)]
15-
[Conditional("CODE_GENERATION_ATTRIBUTES")]
1614
public sealed class LogPropertyIgnoreAttribute : Attribute
1715
{
1816
}

test/Generators/Microsoft.Gen.Logging/Generated/LogPropertiesTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,4 +587,26 @@ public void LogPropertiesReadonlyRecordStructArgument()
587587

588588
latestRecord.StructuredState.Should().NotBeNull().And.Equal(expectedState);
589589
}
590+
591+
[Fact]
592+
public void LogPropertiesCorrectlyLogsObjectFromAnotherAssembly()
593+
{
594+
LogObjectFromAnotherAssembly(_logger, new ObjectToLog
595+
{
596+
PropertyToIgnore = "Foo",
597+
PropertyToLog = "Bar",
598+
FieldToLog = new FieldToLog { Name = "Fizz", Value = "Buzz" }
599+
});
600+
601+
Assert.Equal(1, _logger.Collector.Count);
602+
603+
var state = _logger.Collector.LatestRecord.StructuredState!
604+
.ToDictionary(p => p.Key, p => p.Value);
605+
606+
Assert.Equal(4, state.Count);
607+
Assert.Contains("{OriginalFormat}", state);
608+
Assert.Contains("logObject.PropertyToLog", state);
609+
Assert.Contains("logObject.FieldToLog.Name", state);
610+
Assert.Contains("logObject.FieldToLog.Value", state);
611+
}
590612
}

test/Generators/Microsoft.Gen.Logging/Generated/Microsoft.Gen.Logging.Generated.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
<ProjectReference Include="..\..\..\..\src\Libraries\Microsoft.Extensions.Compliance.Testing\Microsoft.Extensions.Compliance.Testing.csproj" />
2525
<ProjectReference Include="..\..\..\..\src\Libraries\Microsoft.Extensions.Compliance.Redaction\Microsoft.Extensions.Compliance.Redaction.csproj" />
2626
<ProjectReference Include="..\..\..\..\src\Libraries\Microsoft.Extensions.Telemetry\Microsoft.Extensions.Telemetry.csproj" />
27+
<ProjectReference Include="..\HelperLibrary\Microsoft.Gen.Logging.HelperLibrary.csproj" />
2728
</ItemGroup>
2829
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.Gen.Logging.Test;
5+
6+
public class FieldToLog
7+
{
8+
public string? Name { get; set; }
9+
public string? Value { get; set; }
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<RootNamespace>Microsoft.Gen.Logging.Test</RootNamespace>
4+
<Description>Test classes for Microsoft.Gen.Logging.Generated.Tests.</Description>
5+
</PropertyGroup>
6+
7+
<PropertyGroup>
8+
<TargetFrameworks>$(TestNetCoreTargetFrameworks)</TargetFrameworks>
9+
<TargetFrameworks Condition=" '$(IsWindowsBuild)' == 'true' ">$(TestNetCoreTargetFrameworks)$(ConditionalNet462)</TargetFrameworks>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\..\..\..\src\Libraries\Microsoft.Extensions.Telemetry.Abstractions\Microsoft.Extensions.Telemetry.Abstractions.csproj" />
14+
</ItemGroup>
15+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace Microsoft.Gen.Logging.Test;
7+
8+
public class ObjectToLog
9+
{
10+
[LogPropertyIgnore]
11+
public string? PropertyToIgnore { get; set; }
12+
13+
public string? PropertyToLog { get; set; }
14+
15+
[LogProperties]
16+
public FieldToLog? FieldToLog { get; set; }
17+
}

test/Generators/Microsoft.Gen.Logging/TestClasses/LogPropertiesExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics.CodeAnalysis;
66
using System.Globalization;
77
using Microsoft.Extensions.Logging;
8+
using Microsoft.Gen.Logging.Test;
89

910
namespace TestClasses
1011
{
@@ -244,5 +245,8 @@ public override string ToString()
244245

245246
[LoggerMessage(6, LogLevel.Information, "Testing interface-typed argument here...")]
246247
public static partial void LogMethodInterfaceArg(ILogger logger, [LogProperties] IMyInterface complexParam);
248+
249+
[LoggerMessage(7, LogLevel.Information, "Testing logging a complex object residing in another assembly...")]
250+
public static partial void LogObjectFromAnotherAssembly(ILogger logger, [LogProperties] ObjectToLog logObject);
247251
}
248252
}

test/Generators/Microsoft.Gen.Logging/Unit/EmitterTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public async Task TestEmitter()
4545
Assembly.GetAssembly(typeof(IRedactorProvider))!,
4646
Assembly.GetAssembly(typeof(PrivateDataAttribute))!,
4747
Assembly.GetAssembly(typeof(BigInteger))!,
48+
Assembly.GetAssembly(typeof(ObjectToLog))!
4849
},
4950
sources,
5051
symbols)

test/Generators/Microsoft.Gen.Logging/Unit/Microsoft.Gen.Logging.Unit.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<ProjectReference Include="..\..\..\..\src\Libraries\Microsoft.Extensions.Telemetry.Abstractions\Microsoft.Extensions.Telemetry.Abstractions.csproj" />
2121
<ProjectReference Include="..\..\..\..\src\Libraries\Microsoft.Extensions.Compliance.Testing\Microsoft.Extensions.Compliance.Testing.csproj" />
2222
<ProjectReference Include="..\..\..\..\src\Generators\Microsoft.Gen.Logging\Microsoft.Gen.Logging.csproj" />
23+
<ProjectReference Include="..\HelperLibrary\Microsoft.Gen.Logging.HelperLibrary.csproj" />
2324
<!--
2425
<ProjectReference Include="..\Generated\Microsoft.Gen.Logging.Generated.Tests.csproj" />
2526
-->

0 commit comments

Comments
 (0)