dotnet / extensions

This repository contains a suite of libraries that provide facilities commonly needed when creating production-ready applications.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[TagProvider] doesn't work on IEnumerable properties

xakep139 opened this issue · comments

Description

Currently the [TagProvider] isn't applied if a property of IEnumerable was annotated with it.
It seems that the IEnumerable logic takes precedence (but the [TagProvider] validation logic is still applied).

Reproduction Steps

Program.cs:

using Microsoft.Extensions.Logging;

using var loggerFactory = LoggerFactory.Create(x => x.AddJsonConsole(o => o.JsonWriterOptions = new() { Indented = true }));
var logger = loggerFactory.CreateLogger<Program>();
var valueToLog = new MyType
{
    FancyDictionary = new Dictionary<string, ISet<string>>
    {
        ["key1"] = new HashSet<string> { "value1", "value2" },
        ["key2"] = new HashSet<string> { "value3", "value4" },
    },
};

Log.LogData(logger, valueToLog);

static partial class Log
{
    [LoggerMessage(Level = LogLevel.Information)]
    public static partial void LogData(ILogger logger, [LogProperties] MyType obj);
}

public class MyType
{
    [TagProvider(typeof(MyType), nameof(ProvideTags))]
    public required IDictionary<string, ISet<string>> FancyDictionary { get; init; }

    public static void ProvideTags(ITagCollector tagCollector, IDictionary<string, ISet<string>> value)
    {
        foreach (var (key, values) in value)
        {
            tagCollector.Add(key, string.Join(',', values));
        }
    }
}

.csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
    <PackageReference Include="Microsoft.Extensions.Telemetry.Abstractions" Version="8.1.0" />
  </ItemGroup>

</Project>

Expected behavior

The tag provider is called

Actual behavior

Here's what I see in the console:

{
  "EventId": 0,
  "LogLevel": "Information",
  "Category": "Program",
  "Message": "",
  "State": {
    "Message": "obj.FancyDictionary={\u0022key1\u0022=\u0022System.Collections.Generic.HashSet\u00601[System.String]\u0022,\u0022key2\u0022=\u0022System.Collections.Generic.HashSet\u00601[System.String]\u0022}",
    "obj.FancyDictionary": "{\u0022key1\u0022=\u0022System.Collections.Generic.HashSet\u00601[System.String]\u0022,\u0022key2\u0022=\u0022System.Collections.Generic.HashSet\u00601[System.String]\u0022}"
  }
}

Regression?

No

Known Workarounds

No response

Configuration

No response

Other information

No response