Skip to content

Logging generator - for enumerable input argument, add missing helper method #51965

Closed
@maryamariyan

Description

@maryamariyan

If an argument is IEnumerable, we get error:

The name '__Enumerate' does not exist in the current context

For the tests I used

<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0-preview.4.21216.3" />

Repro:

internal static partial class TestWithMoreThan6Params
{
    [LoggerMessage(EventId = 8, Level = LogLevel.Error, Message = "M9 {p1} {p2} {p3} {p4} {p5} {p6} {p7}")]
    public static partial void Method9(ILogger logger, int p1, int p2, int p3, int p4, int p5, int p6, System.Collections.Generic.IEnumerable<int> p7);
}
Generated code
// <auto-generated/>
#nullable enable

namespace ConsoleApp66
{
    partial class TestWithMoreThan6Params 
    {
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "6.0.0.0")]
        private readonly struct __Method9Struct : global::System.Collections.Generic.IReadOnlyList<global::System.Collections.Generic.KeyValuePair<string, object?>>
        {
            private readonly global::System.Int32 _p1;
            private readonly global::System.Int32 _p2;
            private readonly global::System.Int32 _p3;
            private readonly global::System.Int32 _p4;
            private readonly global::System.Int32 _p5;
            private readonly global::System.Int32 _p6;
            private readonly global::System.Collections.Generic.IEnumerable<global::System.Int32> _p7;

            public __Method9Struct(global::System.Int32 p1, global::System.Int32 p2, global::System.Int32 p3, global::System.Int32 p4, global::System.Int32 p5, global::System.Int32 p6, global::System.Collections.Generic.IEnumerable<global::System.Int32> p7)
            {
                this._p1 = p1;
                this._p2 = p2;
                this._p3 = p3;
                this._p4 = p4;
                this._p5 = p5;
                this._p6 = p6;
                this._p7 = p7;

            }

            public override string ToString()
            {
                var p1 = this._p1;
                var p2 = this._p2;
                var p3 = this._p3;
                var p4 = this._p4;
                var p5 = this._p5;
                var p6 = this._p6;
                var p7 = __Enumerate((global::System.Collections.IEnumerable ?)this._p7);

                return $"M9 {p1} {p2} {p3} {p4} {p5} {p6} {p7}";
            }

            public static string Format(__Method9Struct state, global::System.Exception? ex) => state.ToString();

            public int Count => 8;

            public global::System.Collections.Generic.KeyValuePair<string, object?> this[int index]
            {
                get => index switch
                {
                    0 => new global::System.Collections.Generic.KeyValuePair<string, object?>("p1", this._p1),
                    1 => new global::System.Collections.Generic.KeyValuePair<string, object?>("p2", this._p2),
                    2 => new global::System.Collections.Generic.KeyValuePair<string, object?>("p3", this._p3),
                    3 => new global::System.Collections.Generic.KeyValuePair<string, object?>("p4", this._p4),
                    4 => new global::System.Collections.Generic.KeyValuePair<string, object?>("p5", this._p5),
                    5 => new global::System.Collections.Generic.KeyValuePair<string, object?>("p6", this._p6),
                    6 => new global::System.Collections.Generic.KeyValuePair<string, object?>("p7", this._p7),
                    7 => new global::System.Collections.Generic.KeyValuePair<string, object?>("{OriginalFormat}", "M9 {p1} {p2} {p3} {p4} {p5} {p6} {p7}"),

                    _ => throw new global::System.IndexOutOfRangeException(nameof(index)),  // return the same exception LoggerMessage.Define returns in this case
                };
            }

            public global::System.Collections.Generic.IEnumerator<global::System.Collections.Generic.KeyValuePair<string, object?>> GetEnumerator()
            {
                for (int i = 0; i < 8; i++)
                {
                    yield return this[i];
                }
            }

            global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();
        }

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "6.0.0.0")]
        public static partial void Method9(global::Microsoft.Extensions.Logging.ILogger logger, global::System.Int32 p1, global::System.Int32 p2, global::System.Int32 p3, global::System.Int32 p4, global::System.Int32 p5, global::System.Int32 p6, global::System.Collections.Generic.IEnumerable<global::System.Int32> p7)
        {
            if (logger.IsEnabled(global::Microsoft.Extensions.Logging.LogLevel.Error))
            {
                logger.Log(
                    global::Microsoft.Extensions.Logging.LogLevel.Error,
                    new global::Microsoft.Extensions.Logging.EventId(8, nameof(Method9)),
                    new __Method9Struct(p1, p2, p3, p4, p5, p6, p7),
                    null,
                    __Method9Struct.Format);
            }
        }
    }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions