Behavior
LogWriter.ShouldLog( LogEntry logEntry ) queries all configured filters for data in LogEntry to determine if a particular LogEntry should be logged. If all filters return true (for ShouldLog), then LogEntry will be logged if the Write method was called. Exclusive filters are categories, priority, and LoggingEnabled, although you can create your own filters.
Contrast Check IsLoggingEnabled checks only one filter, while a ShouldLog call checks all filters (including IsLoggingEnabled ).
Intention
The purpose of the call is to allow the developer to avoid costly operations if LogEntry not registered. Which "expensive" will depend on the application and requirements. for example, excessive string manipulation in a narrow loop or, possibly, calling from a process to get some information (although this may be a good candidate for caching!).
Should I always check it before registering?
In general, I would not call ShouldLog if there is no good reason for this.
I can think of several reasons:
- It clutters the code
- If you try to de-clutter the code using a helper method, then LogEntry will usually be completely full, so you probably would not have escaped any operations (although you could pass the delegate and not refer to it, m not sure makes things easier).
- Inside, the Write method already calls
ShouldLog , so if you register, then ShouldLog will be called twice for each registered message.
Randy levy
source share