We’re using Enterprise Library 4.1 for logging (and exception handling/cryptography).
Does anyone know a good way of determining the configured logging level at runtime? I’ve written a LogUtility class to to make the logging calls, and am calling it as per this example:
LogUtility.LogVerbose(
string.Format("Processing event {0}", currentEvent.EventIDImported),
MethodBase.GetCurrentMethod().Name,
this.GetType().Name
);
I understand that it won’t actually get logged to file unless the logging level is set to an appropriate level, in app.config in my case. But I don’t really want the method parameters, i.e. the method and type names, and in some cases the actual strings being logged, to be evaluated unless absolutely necessary.
Does this seem like a valid concern? Our app can have tens of millions of iterations and logging points. If possible I’d like to set a flag based on the configured log level, and check that before making the method call above.
EDIT – I guess in terms of the example above I could hard-code method and type names on every call. But I’d still like to know if there’s a way of determining the level.
Based on the above I think you should take a look at the
ShouldLogmethod ofLogWriter. It will let you determine if aLogEntrywill be logged based on the current configuration and you can (hopefully) avoid creating objects that are not required.To borrow the code from the Enterprise Library 4.1 Walkthrough: Checking Filter Status Before Constructing Log Messages:
Since you are using your own
LogUtilityclass you would probably want to create a static property onLogUtilitycalledShouldLogVerboseorIsVerboseEnabledand inside of that property use a “properly” constructedLogEntry(for your application) to determine if the message would be logged. e.g.