You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Analyzer] Usage of ToString in logging source generator method call sites #78402
api-approvedAPI was approved in API review, it can be implementedarea-Extensions-Loggingcode-analyzerMarks an issue that suggests a Roslyn analyzerhelp wanted[up-for-grabs] Good issue for external contributorsin-prThere is an active PR which will close this issue when it is mergedpartner-impactThis issue impacts a partner who needs to be kept updated
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}`")]publicstaticpartialvoidExcessivelyLargeTimeout(thisILoggerlogger,stringtimeout);...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.
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
api-approvedAPI was approved in API review, it can be implementedarea-Extensions-Loggingcode-analyzerMarks an issue that suggests a Roslyn analyzerhelp wanted[up-for-grabs] Good issue for external contributorsin-prThere is an active PR which will close this issue when it is mergedpartner-impactThis issue impacts a partner who needs to be kept updated
Activity
tarekgh commentedon Nov 15, 2022
CC @Youssef1313
Youssef1313 commentedon Nov 15, 2022
Happy to implement once the analyzer is approved.
bartonjs commentedon Nov 22, 2022
Analyzer would detect when "work" is being done in an argument call to an
ILoggerlogging 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
paramsmethod 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:
[LoggerMessage]Microsoft.Extensions.Logging.LoggerExtensionsMicrosoft.Extensions.Logging.ILogger.LogCategory: Performance
Severity: Suggestion
9 remaining items
mpidash commentedon Apr 3, 2024
I'd like to finish this analyzer :)
tarekgh commentedon Apr 3, 2024
@mpidash thanks for willing to help with that. Please ensure doing it as described in #78402 (comment).