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] Replacing direct LoggerExtensions.Log* usage with source-generated logging methods #78406
api-approvedAPI was approved in API review, it can be implementedarea-Extensions-Loggingcode-analyzerMarks an issue that suggests a Roslyn analyzercode-fixerMarks an issue that suggests a Roslyn code fixerhelp wanted[up-for-grabs] Good issue for external contributorspartner-impactThis issue impacts a partner who needs to be kept updated
Just as we have a regex source generator analyzer that flags use of Regex and creates a replacement that uses the source generator, we should consider an analyzer that will push code to use the logging source generator. The analyzer would flag direct use of LoggerExtensions.Log* methods and recommend replacing them with use of the logging generator, with the fixer generating a partial method stub for the generator and changing the call site to use it.
I'll be careful doing that. using LoggerExtensions.Log still legitimate and many users already using it. Even producing analyzer info and not warning for this is not desirable for the users. I wouldn't mind having this analyzer, but it should be off by default. @stephentoub what you think about that?
Please off by default...especially for quick utility apps that don't really care about perf gains from logging. I'd hate to suddenly have a ton of diagnostics... at any level. Regex is one thing, but log messages are typically at a totally different scale.
And please not before fixing issues with object destructuring syntax in the logger messages, ie. "My {@Thing} did something" and the various other requests still open that prevent using the generator in some cases.
I'd hate to suddenly have a ton of analyzer warnings (or broken builds with TreatWarningsAsErrors) like I did with the Regex generator
The regex analyzer defaults to info level; that wouldn't show up in command-line builds at all, and in VS would show up only as suggestions. Unless you changed the default level, it should never break the build. If we added an analyzer for logging it similarly wouldn't be warning or error; none of our performance-focused analyzers are.
And please not before fixing issues with object destructuring syntax in the logger messages, ie. "My {@thing} did something" and the various other requests still open that prevent using the generator in some cases.
An analyzer shouldn't fire if it the thing it's targeting can't actually be replaced. For example, the regex analyzer won't raise a diagnostic if the pattern isn't a const, because that can't be rewritten to use the source generator. Similarly, if there are messages that can't statically be proven to work with the logging generator, the analyzer shouldn't raise a diagnostic.
or logging it similarly wouldn't be warning or error
Logging users didn't even like suggestions to be shown either and they were confused between suggestions and warning. For this rule, I believe it should be off by default and users can opt-in using it. The suggested methods we need to analyze are used a lot across the board and are still legitimate to use.
You're right... my mistake. But the info messages that didn't show up when building locally triggered during Sonar Cloud analysis and apparently they're picking up SYSLIB info messages as code smells, failing my build. Sorry, Misinterpreted what happened on my end....
I fixed the regex generator ones anyways, so all good. But I'd hate to have a ton of info/suggestion-level diags for all the logger calls showing up in VS (or apparently angering sonar cloud!)
Since the fixer will have trouble creating the partial method + attribute when the message template is not a string literal, a different diagnostic ID should be used for literal templates and non-literal templates.
Consider using a second pair of diagnostic IDs for LoggerMessage.Define-based logging.
Have a clarifying question before deciding to work on this.
The fixer would have to create a partial method stub as explained, but this needs to be in a partial class (in the docs, this is shown as a static partial Log class). How do we know where to generate this method stub in?
Just tried SYSLIB1045 (regex) in code, and it seems to simply add partial to the current class. Guessing we can just do the same?
@tarekgh@anicka-net I'd like to work on this in dotnet/sdk if a PR is still welcome. Per the approval (Performance, severity none):
Diagnostic 1: direct LoggerExtensions.Log* call with a literal message template. The fixer adds a [LoggerMessage] partial method to the containing class (marking the class partial, as you suggested, mirroring the Regex generator fixer) and rewrites the call site.
Diagnostic 2: the same call with a non-literal template, with no fixer.
I'd leave the optional LoggerMessage.Define pair out of the first PR unless you'd like it included.
One question: CA1848 already flags these call sites (recommending LoggerMessage delegates) but has no fixer. Should these be new IDs alongside CA1848, as approved, or would you rather the fixer attach to CA1848?
api-approvedAPI was approved in API review, it can be implementedarea-Extensions-Loggingcode-analyzerMarks an issue that suggests a Roslyn analyzercode-fixerMarks an issue that suggests a Roslyn code fixerhelp wanted[up-for-grabs] Good issue for external contributorspartner-impactThis issue impacts a partner who needs to be kept updated
Activity
tarekgh commentedon Nov 15, 2022
I'll be careful doing that. using
LoggerExtensions.Logstill legitimate and many users already using it. Even producing analyzer info and not warning for this is not desirable for the users. I wouldn't mind having this analyzer, but it should be off by default. @stephentoub what you think about that?CC @ericstj @Youssef1313
pinkfloydx33 commentedon Nov 15, 2022
Please off by default...especially for quick utility apps that don't really care about perf gains from logging. I'd hate to suddenly have a ton of diagnostics... at any level. Regex is one thing, but log messages are typically at a totally different scale.
And please not before fixing issues with object destructuring syntax in the logger messages, ie.
"My {@Thing} did something"and the various other requests still open that prevent using the generator in some cases.stephentoub commentedon Nov 15, 2022
The regex analyzer defaults to info level; that wouldn't show up in command-line builds at all, and in VS would show up only as suggestions. Unless you changed the default level, it should never break the build. If we added an analyzer for logging it similarly wouldn't be warning or error; none of our performance-focused analyzers are.
An analyzer shouldn't fire if it the thing it's targeting can't actually be replaced. For example, the regex analyzer won't raise a diagnostic if the pattern isn't a const, because that can't be rewritten to use the source generator. Similarly, if there are messages that can't statically be proven to work with the logging generator, the analyzer shouldn't raise a diagnostic.
tarekgh commentedon Nov 15, 2022
Logging users didn't even like suggestions to be shown either and they were confused between suggestions and warning. For this rule, I believe it should be off by default and users can opt-in using it. The suggested methods we need to analyze are used a lot across the board and are still legitimate to use.
pinkfloydx33 commentedon Nov 15, 2022
You're right... my mistake. But the info messages that didn't show up when building locally triggered during Sonar Cloud analysis and apparently they're picking up SYSLIB info messages as code smells, failing my build. Sorry, Misinterpreted what happened on my end....
I fixed the regex generator ones anyways, so all good. But I'd hate to have a ton of info/suggestion-level diags for all the logger calls showing up in VS (or apparently angering sonar cloud!)
3 remaining items
tarekgh commentedon Nov 21, 2022
CC @Youssef1313
bartonjs commentedon Nov 22, 2022
Looks good as proposed.
Since the fixer will have trouble creating the partial method + attribute when the message template is not a string literal, a different diagnostic ID should be used for literal templates and non-literal templates.
Consider using a second pair of diagnostic IDs for LoggerMessage.Define-based logging.
Category:Performance
Severity:none (requires manual opt-in)
mrahhal commentedon Jul 27, 2023
Have a clarifying question before deciding to work on this.
The fixer would have to create a partial method stub as explained, but this needs to be in a partial class (in the docs, this is shown as a static partial
Logclass). How do we know where to generate this method stub in?Just tried SYSLIB1045 (regex) in code, and it seems to simply add
partialto the current class. Guessing we can just do the same?tarekgh commentedon Jul 27, 2023
@mrahhal yes, please mimic regex behavior and include the method inside the current class with adding partial specifier.
Manish-1kumar commentedon Nov 25, 2026
@tarekgh @anicka-net I'd like to work on this in
dotnet/sdkif a PR is still welcome. Per the approval (Performance, severity none):LoggerExtensions.Log*call with a literal message template. The fixer adds a[LoggerMessage]partial method to the containing class (marking the classpartial, as you suggested, mirroring the Regex generator fixer) and rewrites the call site.LoggerMessage.Definepair out of the first PR unless you'd like it included.One question: CA1848 already flags these call sites (recommending LoggerMessage delegates) but has no fixer. Should these be new IDs alongside CA1848, as approved, or would you rather the fixer attach to CA1848?