Skip to content

Formatted log values convert enumerable values to string #36025

@kapetan

Description

@kapetan

Describe the bug

Stringifying FormattedLogValues converts all the enumerable values to strings. Other types don't seem to be affected by this. I'm not sure if this is expected, it is not intuitive at least.

To Reproduce

Run the following with dotnet run.

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Logging" Version="3.0.0" />
  </ItemGroup>

</Project>
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;

namespace TestLogging {
    class Program {
        static void Main(string[] args) {
            ILogger logger = new Logger();

            logger.LogInformation("My array {Array} and my double {Double}", new int[] { 1, 2, 3 }, 4.4);
        }
    }

    public class Logger : ILogger {
        public IDisposable BeginScope<TState>(TState state) {
            return null;
        }

        public bool IsEnabled(LogLevel logLevel) {
            return true;
        }

        public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) {
            if (state is IEnumerable<KeyValuePair<string, object>> properties) {
                foreach (KeyValuePair<string, object> pair in properties) {
                    Console.WriteLine($"{pair.Key} = {pair.Value.GetType()}");
                }

                Console.WriteLine();
                Console.WriteLine(state);
                Console.WriteLine();

                foreach (KeyValuePair<string, object> pair in properties) {
                    Console.WriteLine($"{pair.Key} = {pair.Value.GetType()}");
                }
            }
        }
    }
}

Terminal output

Array = System.Int32[]
Double = System.Double
{OriginalFormat} = System.String

My array 1, 2, 3 and my double 4.4

Array = System.String
Double = System.Double
{OriginalFormat} = System.String

Note that Array is System.Int32[] in the first case, as expected, but it's converted to a string after stringifying state.

Expected behavior

Expected enumerables not be converted to strings after stringifying formatted log values instance.

Additional context

.NET Core SDK (reflecting any global.json):
 Version:   2.2.401
 Commit:    729b316c13

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.14
 OS Platform: Darwin
 RID:         osx.10.14-x64
 Base Path:   /usr/local/share/dotnet/sdk/2.2.401/

Host (useful for support):
  Version: 2.2.6
  Commit:  7dac9b1b51

.NET Core SDKs installed:
  2.2.401 [/usr/local/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.2.6 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.2.6 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.2.6 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

I suspect this line to be the offender https://github.com/aspnet/Extensions/blob/5ba0926cb3001a69cb5eecac7466a5e9284e6fd3/src/Logging/Logging.Abstractions/src/LogValuesFormatter.cs#L122.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Extensions-LoggingenhancementProduct code improvement that does NOT require public API changes/additions

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions