Skip to content

Logging source generator should support generic methods (SYSLIB1011 should be lifted) #90589

Description

@alexrp

From this section:

Logging methods cannot be generic.

This seems to be an unnecessary restriction that also works against the whole notion of high-performance logging.

I would like to be able to write a logging method like so:

[LoggerMessage(0, LogLevel.Trace, "C -> S {EndPoint}: {Channel}:{Code} ({Length} bytes)")]
public static partial void PacketReceived<TCode>(ILogger logger, IPEndPoint endPoint, ConnectionChannel channel, TCode code, int length)
    where TCode : struct, Enum;

Here, the type argument for TCode depends on the value of Channel. Of course, I could make the logging method non-generic and accept an Enum-typed value, but then I get unconditional boxing... certainly not what I want when logging in a fairly hot path. Alternatively, I could duplicate the method for as many TCodes as I have (which is what I'll do for now), but this is not great either, for all the expected maintenance reasons.

I think this restriction should just be lifted. Maybe there's a good reason for it, but if there is, it's not obvious to me.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions