Skip to content

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

@tmds

Description

@tmds

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]).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions