Fluent logging has the capability of conditionally adding a property, using AddIf, like so:
public CommonLoggerProperties AddIf(bool condition, string key, string value)
The downside of this method is that the value argument needs to be specified, even when condition is false. If value is the result of an expensive operation:
... then that expensive operation (ComputeExpensiveValueForDebugging(otherParameters) in this case) will be computed regardless of the value of the condition parameter.
To avoid this, this pull request provides for each overload of AddIf a corresponding overload with a function delegate as value parameter. For example:
public CommonLoggerProperties AddIf(bool condition, string key, Func<string> value)
This way, ComputeExpensiveValueForDebugging(otherParameters) will only be called when condition is true. Nothing will be called if someCondition is false.
Fluent logging has the capability of conditionally adding a property, using
AddIf
, like so:The downside of this method is that the
value
argument needs to be specified, even whencondition
isfalse
. Ifvalue
is the result of an expensive operation:... then that expensive operation (
ComputeExpensiveValueForDebugging(otherParameters)
in this case) will be computed regardless of the value of thecondition
parameter.To avoid this, this pull request provides for each overload of
AddIf
a corresponding overload with a function delegate as value parameter. For example:This way, the previous call can be written as:
This way,
ComputeExpensiveValueForDebugging(otherParameters)
will only be called whencondition
istrue
. Nothing will be called ifsomeCondition
isfalse
.