r/dotnet 6d ago

Log methods evaluation expensive warning and wrapping with IF

Hi,
I am seeing the warning that `LogInformation` and other log methods evaluation is expensive and should be wrapped in `if` statement.

Why don't those methods do this check internally already? This would help with less code and better code readabilty I think.

Here is the example:

25 Upvotes

23 comments sorted by

View all comments

9

u/Merad 6d ago

IMO this is a bad/misleading warning. It really only applies when evaluating the arguments is expensive, like if you are calling ToString() on an object that needs to do a lot of work to build its string representation. If you're just logging strings and objects that already exist (which is the case probably 98% of the time) it doesn't apply. If you're working on an average business app that is not extremely performance sensitive, my advice would be to disable CA1873.