What is the behavior of the LogWriter.ShouldLog (LogEntry) based method?

What is LogWriter.ShouldLog(..) based method behavior? Or what is the real intention of using it? His documentation page is rather sparse and does not give much understanding. Quoting this:

Will ask if logEntry shold [sic] will be input.

(Complete with the same typo in all versions of the Enterprise Library, it makes me wonder if the authors paid much attention to this.)

The method seems pretty ethereal to me. Should I always check it before registration?

I am currently checking out LogWriter.IsLoggingEnabled(..) , which is based on an explicit configuration setting. This is a specific scenario: logging is either on or off.

+6
enterprise-library
source share
1 answer

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.
+5
source share

All Articles