Skip to content

Add SkipEnabledCheck to ConnectionSelected log message #661

Description

@imperugo

Suggested by @LeaFrock.

Proposal

Now that the call-site guards the log call explicitly, set SkipEnabledCheck = true on the source-generated log method to remove the redundant generated check:

[LoggerMessage(EventId = 1002, Level = LogLevel.Debug, SkipEnabledCheck = true,
    Message = "Using connection {ConnectionHash} with {Outstanding} outstanding commands")]
public static partial void ConnectionSelected(ILogger logger, int connectionHash, long outstanding);

Rationale

In #659 the call-site was wrapped in an explicit guard, because the argument connection.TotalOutstanding() is expensive — it goes through IConnectionMultiplexer.GetCounters(), which allocates a ServerCounters snapshot on every call, and log arguments are evaluated at the call-site regardless of whether the level is enabled:

if (logger.IsEnabled(LogLevel.Debug))
    LogMessages.ConnectionSelected(logger, connection.Connection.GetHashCode(), connection.TotalOutstanding());

By default the [LoggerMessage] source generator emits its own IsEnabled check inside the generated method, so we now pay that check twice. This sits in RedisConnectionPoolManager.GetConnection(), which runs on every single Redis operation, so it is the hottest path in the library. SkipEnabledCheck = true is the documented way to opt out when the caller already guards, and it also makes the intent explicit to the next reader.

Verified: ConnectionSelected has exactly one call-site (RedisConnectionPoolManager.cs:147), and it is already guarded.

Note for the implementer

SkipEnabledCheck = true turns "the caller must guard" into a contract for this method. Any future call-site added without a guard would pay the full formatting cost unconditionally. Worth a short comment on the declaration so that constraint is not lost.

Acceptance criteria

  • SkipEnabledCheck = true added to the ConnectionSelected declaration in LogMessages.cs
  • Comment documenting the caller-must-guard contract
  • Existing call-site guard left in place

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions