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

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

Closed
maryamariyan opened this issue Apr 27, 2021 · 3 comments
Closed
Assignees
Milestone

Comments

@maryamariyan
Copy link
Member

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);
            }
        }
    }
}
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Apr 27, 2021
@ghost
Copy link

ghost commented Apr 27, 2021

Tagging subscribers to this area: @maryamariyan
See info in area-owners.md if you want to be subscribed.

Issue Details

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);
            }
        }
    }
}
Author: maryamariyan
Assignees: -
Labels:

area-Extensions-Logging, untriaged

Milestone: -

@maryamariyan maryamariyan removed the untriaged New issue has not been triaged by the area owner label Apr 27, 2021
@ghost ghost added this to Untriaged in ML, Extensions, Globalization, etc, POD. Apr 27, 2021
@maryamariyan maryamariyan added this to the 6.0.0 milestone Apr 27, 2021
@ghost ghost moved this from Untriaged to 6.0.0 in ML, Extensions, Globalization, etc, POD. Apr 27, 2021
@maryamariyan maryamariyan self-assigned this Apr 27, 2021
@maryamariyan
Copy link
Member Author

Fixed in #51963

ML, Extensions, Globalization, etc, POD. automation moved this from 6.0.0 to Done Apr 29, 2021
@ghost ghost locked as resolved and limited conversation to collaborators May 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant