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
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Logging;
using Microsoft.Shared.DiagnosticIds;
Expand All @@ -14,7 +13,6 @@ namespace Microsoft.Extensions.Logging;
/// </summary>
/// <seealso cref="LoggerMessageAttribute"/>
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property)]
[Conditional("CODE_GENERATION_ATTRIBUTES")]
public sealed class LogPropertiesAttribute : Attribute
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using Microsoft.Extensions.Logging;

namespace Microsoft.Extensions.Logging;
Expand All @@ -12,7 +11,6 @@ namespace Microsoft.Extensions.Logging;
/// </summary>
/// <seealso cref="LoggerMessageAttribute"/>.
[AttributeUsage(AttributeTargets.Property)]
[Conditional("CODE_GENERATION_ATTRIBUTES")]
public sealed class LogPropertyIgnoreAttribute : Attribute
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -587,4 +587,26 @@ public void LogPropertiesReadonlyRecordStructArgument()

latestRecord.StructuredState.Should().NotBeNull().And.Equal(expectedState);
}

[Fact]
public void LogPropertiesCorrectlyLogsObjectFromAnotherAssembly()
{
LogObjectFromAnotherAssembly(_logger, new ObjectToLog
{
PropertyToIgnore = "Foo",
PropertyToLog = "Bar",
FieldToLog = new FieldToLog { Name = "Fizz", Value = "Buzz" }
});

Assert.Equal(1, _logger.Collector.Count);

var state = _logger.Collector.LatestRecord.StructuredState!
.ToDictionary(p => p.Key, p => p.Value);

Assert.Equal(4, state.Count);
Assert.Contains("{OriginalFormat}", state);
Assert.Contains("logObject.PropertyToLog", state);
Assert.Contains("logObject.FieldToLog.Name", state);
Assert.Contains("logObject.FieldToLog.Value", state);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
<ProjectReference Include="..\..\..\..\src\Libraries\Microsoft.Extensions.Compliance.Testing\Microsoft.Extensions.Compliance.Testing.csproj" />
<ProjectReference Include="..\..\..\..\src\Libraries\Microsoft.Extensions.Compliance.Redaction\Microsoft.Extensions.Compliance.Redaction.csproj" />
<ProjectReference Include="..\..\..\..\src\Libraries\Microsoft.Extensions.Telemetry\Microsoft.Extensions.Telemetry.csproj" />
<ProjectReference Include="..\HelperLibrary\Microsoft.Gen.Logging.HelperLibrary.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.Gen.Logging.Test;

public class FieldToLog
{
public string? Name { get; set; }
public string? Value { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RootNamespace>Microsoft.Gen.Logging.Test</RootNamespace>
<Description>Test classes for Microsoft.Gen.Logging.Generated.Tests.</Description>
</PropertyGroup>

<PropertyGroup>
<TargetFrameworks>$(TestNetCoreTargetFrameworks)</TargetFrameworks>
<TargetFrameworks Condition=" '$(IsWindowsBuild)' == 'true' ">$(TestNetCoreTargetFrameworks)$(ConditionalNet462)</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\src\Libraries\Microsoft.Extensions.Telemetry.Abstractions\Microsoft.Extensions.Telemetry.Abstractions.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.Logging;

namespace Microsoft.Gen.Logging.Test;

public class ObjectToLog
{
[LogPropertyIgnore]
public string? PropertyToIgnore { get; set; }

public string? PropertyToLog { get; set; }

[LogProperties]
public FieldToLog? FieldToLog { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Microsoft.Extensions.Logging;
using Microsoft.Gen.Logging.Test;

namespace TestClasses
{
Expand Down Expand Up @@ -244,5 +245,8 @@ public override string ToString()

[LoggerMessage(6, LogLevel.Information, "Testing interface-typed argument here...")]
public static partial void LogMethodInterfaceArg(ILogger logger, [LogProperties] IMyInterface complexParam);

[LoggerMessage(7, LogLevel.Information, "Testing logging a complex object residing in another assembly...")]
public static partial void LogObjectFromAnotherAssembly(ILogger logger, [LogProperties] ObjectToLog logObject);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public async Task TestEmitter()
Assembly.GetAssembly(typeof(IRedactorProvider))!,
Assembly.GetAssembly(typeof(PrivateDataAttribute))!,
Assembly.GetAssembly(typeof(BigInteger))!,
Assembly.GetAssembly(typeof(ObjectToLog))!
},
sources,
symbols)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<ProjectReference Include="..\..\..\..\src\Libraries\Microsoft.Extensions.Telemetry.Abstractions\Microsoft.Extensions.Telemetry.Abstractions.csproj" />
<ProjectReference Include="..\..\..\..\src\Libraries\Microsoft.Extensions.Compliance.Testing\Microsoft.Extensions.Compliance.Testing.csproj" />
<ProjectReference Include="..\..\..\..\src\Generators\Microsoft.Gen.Logging\Microsoft.Gen.Logging.csproj" />
<ProjectReference Include="..\HelperLibrary\Microsoft.Gen.Logging.HelperLibrary.csproj" />
<!--
<ProjectReference Include="..\Generated\Microsoft.Gen.Logging.Generated.Tests.csproj" />
-->
Expand Down
Loading