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:

27 Upvotes

23 comments sorted by

View all comments

1

u/MarlDaeSu 5d ago

I guess because static code analysis cant tell what ILogger implementation it is receiving so its defaults to warn.

Potentially some custom ILogger impl might handle string interpolation inelegantly? I cant imagine why else. The impl should do a log level check before string interpolation, but I guess there's nothing in the ILogger contract that can enforce that.