Skip to content

[API Proposal]: Better Source comments on generated code from LoggerMessageAttribute #118164

@fabsenet

Description

@fabsenet

Background and motivation

I started using the LoggerMessage attributes to source generate logging calls. I would ask you to make it generate a documentation which includes at least the actual message and the log level.

I am constantly wondering in bigger classes what it would output and a simple mouseover would help me here!

You did this recently for the RegExAttribute source generator and was always the case for strings in resource files as long as i can remember.

API Proposal

simple sample:

    public sealed partial class Sample(ILogger logger)
    {
        [LoggerMessage(Message = "Hello {Name}]", Level = LogLevel.Information)]
        public partial void LogHello(string name);

        public void DoSomething()
        {
            LogHello("World");
        }
    }

generated partial call:

    partial class Sample
    {
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "9.0.12.31616")]
        private static readonly global::System.Action<global::Microsoft.Extensions.Logging.ILogger, global::System.String, global::System.Exception?> __LogHelloCallback =
            global::Microsoft.Extensions.Logging.LoggerMessage.Define<global::System.String>(global::Microsoft.Extensions.Logging.LogLevel.Information, new global::Microsoft.Extensions.Logging.EventId(871370049, nameof(LogHello)), "Hello {Name}]", new global::Microsoft.Extensions.Logging.LogDefineOptions() { SkipEnabledCheck = true }); 

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "9.0.12.31616")]
        public partial void LogHello(global::System.String name)
        {
            if (logger.IsEnabled(global::Microsoft.Extensions.Logging.LogLevel.Information))
            {
                __LogHelloCallback(logger, name, null);
            }
        }
    }

API Usage

(I faked it here)

But i want the mouse over to look like this:

Image

Alternative Designs

No response

Risks

Only the documentation would change, so risk should be minimal. Not sure, what to do if the partial declaration already has documentation?!

Metadata

Metadata

Labels

api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-Extensions-Loggingsource-generatorIndicates an issue with a source generator feature

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions