Skip to content

Conversation

Meir017
Copy link
Contributor

@Meir017 Meir017 commented Jul 24, 2025

closes #2899

Converts manual log message formatting to LoggerMessage source generator methods for better performance and structured logging support.

Improves logging efficiency by eliminating string allocations when logging is disabled and provides consistent structured data for log analysis tools.

Converts manual log message formatting to LoggerMessage source generator methods for better performance and structured logging support.

Improves logging efficiency by eliminating string allocations when logging is disabled and provides consistent structured data for log analysis tools.
@mgravell mgravell merged commit 45f7af4 into StackExchange:main Jul 25, 2025
6 checks passed
@Meir017 Meir017 deleted the chore/logger-message-attribute branch July 25, 2025 12:33
@NickCraver
Copy link
Collaborator

@Meir017 For long-term info: this won't reduce allocations when logging is disabled, ?. takes care of that ;)

@mgravell
Copy link
Collaborator

mgravell commented Jul 25, 2025

@NickCraver that depends on the definition of "disabled"; if there exists a non-null logger that isn't enabled for the specific log-level being used (generally "information" in our case), then this will indeed prevent those allocations, rather than the old code which creates the string before getting that far

@mgravell
Copy link
Collaborator

examples from the codegen:

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "8.0.9.3103")]
        internal static partial void LogInformationCheckingServerAvailable(this global::Microsoft.Extensions.Logging.ILogger logger, global::StackExchange.Redis.LoggerExtensions.EndPointLogValue endPoint)
        {
            if (logger.IsEnabled(global::Microsoft.Extensions.Logging.LogLevel.Information))
            {
                __LogInformationCheckingServerAvailableCallback(logger, endPoint, null);
            }
        }
       // ...     
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "8.0.9.3103")]
        internal static partial void LogErrorOperationFailedOnServer(this global::Microsoft.Extensions.Logging.ILogger logger, global::System.Exception exception, global::StackExchange.Redis.LoggerExtensions.EndPointLogValue endPoint, global::System.String errorMessage)
        {
            if (logger.IsEnabled(global::Microsoft.Extensions.Logging.LogLevel.Error))
            {
                __LogErrorOperationFailedOnServerCallback(logger, endPoint, errorMessage, exception);
            }
        }

@NickCraver
Copy link
Collaborator

@mgravell Yeah that's fair, when someone says disabled generally that means 100% but agree they could mean a few things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use logging best practices for core logging messages
3 participants