What is the LogWriter.ShouldLog(..)
method behaviour based on? Or what is the real intention of its usage? Its documentation page is rather sparse and doesn't provide much insight. Quoting it:
Queries whether a LogEntry shold[sic] be logged.
(Complete with same typo in all versions of the Enterprise Library, makes me wonder if the authors paid much attention to it.)
The method seems rather ethereal to me. Should I always check it before logging?
Currently I only check LogWriter.IsLoggingEnabled(..)
which is based on an explicit setting in configuration. This represents a concrete scenario: the logging is either turned on or off.
LogWriter.ShouldLog( LogEntry logEntry )
queries all of the configured filters against the data in the LogEntry to determine if the specific LogEntry should be logged. If all filters return true (for ShouldLog) then the LogEntry would be logged if the Write
method were called. The out of the box filters are Category, Priority, and LoggingEnabled although you can create your own custom filters.
The IsLoggingEnabled
check in contrast just checks one filter whereas the ShouldLog
call checks all filters (including IsLoggingEnabled
).
The intention of the call is to allow the developer to avoid expensive operations if the LogEntry
will not be logged. What "expensive" is will depend on the application and requirements. e.g. excessive string manipulation in a tight loop or perhaps an out of process call to retrieve some information (although that might be a good candidate for caching!).
In general, I would avoid calling ShouldLog
unless there is a compelling reason to do otherwise.
I can think of a few reasons:
ShouldLog
so if you are logging then ShouldLog
will be called twice for every message logged