Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Console Logger does not serialize enumerable items in State #111187

Open
tmds opened this issue Jan 8, 2025 · 1 comment
Open

JSON Console Logger does not serialize enumerable items in State #111187

tmds opened this issue Jan 8, 2025 · 1 comment

Comments

@tmds
Copy link
Member

tmds commented Jan 8, 2025

For example:

using Microsoft.Extensions.Logging;

foreach (bool useJsonLogger in new[] { false, true })
{
    using ILoggerFactory loggerFactory = LoggerFactory.Create(
        builder =>
        {
            if (useJsonLogger)
            {
                builder.AddJsonConsole();
            }
            else
            {
                builder.AddConsole();
            }
        });

    var logger = loggerFactory.CreateLogger("Logger");

    logger.LogValues(Enumerable.Range(1, 5));
}

static partial class Logger
{
    [LoggerMessage(
        EventId = 1,
        Level = LogLevel.Information,
        Message = "Values are {Values}")]
    public static partial void LogValues(this ILogger logger, IEnumerable<int> values);
}

Outputs:

info: Logger[1]
      Values are 1, 2, 3, 4, 5
{"EventId":1,"LogLevel":"Information","Category":"Logger","Message":"Values are 1, 2, 3, 4, 5","State":{"Message":"Values are 1, 2, 3, 4, 5","Values":"System.Linq.Enumerable\u002BRangeIterator","{OriginalFormat}":"Values are {Values}"}}

The message contains the items of the enumerable (1, 2, 3, 4, 5) while the JSON State contains ToString of the enumerable: ("Values":"System.Linq.Enumerable\u002BRangeIterator").

The behavior I expect is for the JSON State to include the enumerable items in a JSON array ("Values": [1, 2, 3, 4, 5]).

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jan 8, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-logging
See info in area-owners.md if you want to be subscribed.

@tarekgh tarekgh added this to the Future milestone Jan 8, 2025
@tarekgh tarekgh removed the untriaged New issue has not been triaged by the area owner label Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants