-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Analyzer] Replacing direct LoggerExtensions.Log* usage with source-generated logging methods #78406
Comments
Tagging subscribers to this area: @dotnet/area-extensions-logging Issue DetailsJust 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 |
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. |
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. |
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!) |
I have edited the description to have this analyzer |
CC @Youssef1313 |
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 |
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 Just tried SYSLIB1045 (regex) in code, and it seems to simply add |
@mrahhal yes, please mimic regex behavior and include the method inside the current class with adding partial specifier. |
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.
Performance rules Category
Severity = none (off by default)
The text was updated successfully, but these errors were encountered: