Skip to content

[Analyzer] Usage of ToString in logging source generator method call sites #78402

Closed
dotnet/roslyn-analyzers
#7290

Description

@stephentoub

We should consider an analyzer that flags use of ToString() in call sites to logging methods and suggests reconsidering the shape of the logging method such that it takes a strongly-typed value. In many situations, logging is disabled or set at a log level that will result in a particular event being disabled, and doing the ToString() at the call site is then often unnecessary allocation / expense. For example, given:

[LoggerMessage(EventId = 0, Level = LogLevel.Information, Message = "Excessively large timeout `{timeout}`")]
public static partial void ExcessivelyLargeTimeout(this ILogger logger, string timeout);
...
TimeSpan timeout = ...;
logger.ExcessivelyLargeTimeout(timeout.ToString());

the analyzer would flag the .ToString() and suggest the ExcessivelyLargeTimeout method should be changed to take a TimeSpan instead of a string, with the .ToString() removed from the call site.

Performance rules Category
Severity = suggestion

Activity

  1. added
    code-analyzer
    on Nov 15, 2022
  2. ghost added
    untriaged
    on Nov 15, 2022
  3. added this to the Future milestone on Nov 15, 2022
  4. ghost removed
    untriaged
    on Nov 15, 2022
  5. tarekgh commented on Nov 15, 2022

    @tarekgh
    Member
  6. Youssef1313 commented on Nov 15, 2022

    @Youssef1313
    Member

    Happy to implement once the analyzer is approved.

  7. modified the milestones: Future, 8.0.0 on Nov 16, 2022
  8. added
    api-suggestion
    on Nov 21, 2022
  9. added
    blocking
    api-ready-for-review
    on Nov 21, 2022
  10. added
    partner-impact
    on Nov 21, 2022
  11. bartonjs commented on Nov 22, 2022

    @bartonjs
    Member

    Analyzer would detect when "work" is being done in an argument call to an ILogger logging method. This basically boils down to anything other than a literal-expression, local-expression, parameter-expression, field-expression, property-expression, or indexer-expression (potentially recursively applied).

    Note that this list includes identifying any parameters being wrapped in a new array to a params method call.

    The analyzer should not fire if it identifies that it is in a scope that has already checked on log levels.

    A logging method is any of:

    • A method decorated with [LoggerMessage]
    • Any of the extension methods on Microsoft.Extensions.Logging.LoggerExtensions
    • A call to Microsoft.Extensions.Logging.ILogger.Log
    • Anything else that looks like a logging method that we didn't think of today

    Category: Performance
    Severity: Suggestion

  12. 9 remaining items

  13. added
    in-pr
    on Jun 17, 2023
  14. modified the milestones: 8.0.0, 9.0.0 on Aug 2, 2023
  15. mpidash commented on Apr 3, 2024

    @mpidash
    Contributor

    I'd like to finish this analyzer :)

  16. tarekgh commented on Apr 3, 2024

    @tarekgh
    Member

    @mpidash thanks for willing to help with that. Please ensure doing it as described in #78402 (comment).

  17. modified the milestones: 9.0.0, Future on Jun 20, 2024
  18. locked and limited conversation to collaborators on Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-Extensions-Loggingcode-analyzerhelp wantedin-prpartner-impact

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions