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
Add CA1873: Avoid potentially expensive logging #7290
base: main
Are you sure you want to change the base?
Conversation
This analyzer detects calls to 'ILogger.Log', extension methods in 'Microsoft.Extensions.Logging.LoggerExtensions' and methods decorated with '[LoggerMessage]'. It then checks if they evaluate expensive arguments without checking if logging is enabled with 'ILogger.IsEnabled'.
// Check the argument value after conversions to prevent noise (e.g. implicit conversions from null or from int to EventId). | ||
if (IsPotentiallyExpensive(argument.Value.WalkDownConversion())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit of a deviation from the description in the runtime issue, but without this, there would be a lot of noise.
The following lines would be flagged:
void M(ILogger logger, EventId eventId, Exception exception, Func<object, Exception, string> formatter)
{
logger.Log(LogLevel.Trace, eventId, null, exception, formatter);
logger.Log(LogLevel.Trace, 0, "literal", exception, formatter);
}
// Implicit params array creation is treated as not expensive. This would otherwise cause a lot of noise. | ||
or IArrayCreationOperation { IsImplicit: true, Initializer.ElementValues.IsEmpty: true } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, without this, all log calls that take a params array would always be flagged, even if they do not capture anything, e.g.:
void M(ILogger logger)
{
logger.LogInformation("literal");
}
var conditionInvocations = conditional.Condition | ||
.DescendantsAndSelf() | ||
.OfType<IInvocationOperation>(); | ||
|
||
// Check each invocation in the condition to see if it is a valid guard invocation, i.e. same instance and same log level. | ||
// This is not perfect (e.g. 'if (logger.IsEnabled(LogLevel.Debug) || true)' is treated as guarded), but should be good enough to prevent false positives. | ||
if (conditionInvocations.Any(IsValidIsEnabledGuardInvocation)) | ||
{ | ||
return true; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As stated in the comment, this is not perfect, but should be good enough to prevent false positives.
return AreInvocationsOnSameInstance(logInvocation, invocation) && IsSameLogLevel(invocation.Arguments[0]); | ||
} | ||
|
||
static bool AreInvocationsOnSameInstance(IInvocationOperation invocation1, IInvocationOperation invocation2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A variation of this is used in other analyzers. I think this could be a good candidate for another extension method.
[Fact] | ||
public async Task WrongLogLevelGuardedWorkInLog_ReportsDiagnostic_CS() | ||
{ | ||
string source = """ | ||
using System; | ||
using Microsoft.Extensions.Logging; | ||
|
||
class C | ||
{ | ||
void M(ILogger logger, EventId eventId, Exception exception, Func<string, Exception, string> formatter) | ||
{ | ||
if (logger.IsEnabled(LogLevel.Critical)) | ||
logger.Log(LogLevel.Trace, eventId, [|ExpensiveMethodCall()|], exception, formatter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering if we should report another diagnostic if the log level does not match, as this is most likely a bug.
[Fact] | ||
public async Task WrongInstanceGuardedWorkInLog_ReportsDiagnostic_CS() | ||
{ | ||
string source = """ | ||
using System; | ||
using Microsoft.Extensions.Logging; | ||
|
||
class C | ||
{ | ||
private ILogger _otherLogger; | ||
|
||
void M(ILogger logger, EventId eventId, Exception exception, Func<string, Exception, string> formatter) | ||
{ | ||
if (_otherLogger.IsEnabled(LogLevel.Trace)) | ||
logger.Log(LogLevel.Trace, eventId, [|ExpensiveMethodCall()|], exception, formatter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another candidate for a diagnostic: Checking the wrong logger instance.
cc @stephentoub @Youssef1313 (thanks for providing the prototype 👍) |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7290 +/- ##
==========================================
+ Coverage 96.47% 96.50% +0.02%
==========================================
Files 1443 1445 +2
Lines 345394 348521 +3127
Branches 11364 11404 +40
==========================================
+ Hits 333229 336340 +3111
- Misses 9284 9286 +2
- Partials 2881 2895 +14 |
Thanks. The LoggerExtensions.cs ones are all false positives in that we'll want to suppress them, but that's also effectively the implementation of logging rather than consumption of logging, and we frequently have to suppress rules in such implementations. The others for runtime look like valid diagnostics. |
Fixes dotnet/runtime#78402.
This analyzer detects
ILogger.Log
,Microsoft.Extensions.Logging.LoggerExtensions
and[LoggerMessage]
and flags them if they evaluate expensive arguments without checking if logging is enabled with
ILogger.IsEnabled
.There are 12 findings in
dotnet/runtime
(8 of them inMicrosoft.Extensions.Logging.Abstractions
which will probably disable this warning), 157 findings indotnet/roslyn
and 497 indotnet/aspnetcore
(including testing code).I skimmed through them and could not find any false positives.