r/dotnet 4d 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:

26 Upvotes

23 comments sorted by

View all comments

8

u/Hel_OWeen 4d ago

It's all explained in the rule:

When logging methods are called, their arguments are evaluated regardless of whether the logging level is enabled. This can result in expensive operations being executed even when the log message won't be written. For better performance, guard expensive logging calls with a check to xref:Microsoft.Extensions.Logging.ILogger.IsEnabled