Registering with the corporate library - how do I get a customized logging level at runtime?

We use Enterprise Library 4.1 for logging (and exception handling / cryptography).

Does anyone know a good way to determine the configured logging level at runtime? I wrote the LogUtility class to make registration calls and call it according to this example:

LogUtility.LogVerbose( string.Format("Processing event {0}", currentEvent.EventIDImported), MethodBase.GetCurrentMethod().Name, this.GetType().Name ); 

I understand that it will not actually be registered to the file if the registration level is not set to the appropriate level in app.config in my case. But I really do not need the parameters of the method, that is, the names of the methods and types, and in some cases the actual lines that are registered should be evaluated if absolutely necessary.

Does this sound like a valid problem? Our application can have tens of millions of iterations and registration points. If possible, I would like to set a flag based on the configured log level and check this before calling the method call above.

EDIT. I think in terms of the above example, I can use the method and type names for each call. But I still would like to know if there is a way to determine the level.

+4
source share
2 answers

I really do not want the method parameters, that is, the method and type of names, and in some cases the recordable strings to be evaluated if absolutely necessary.

Based on the foregoing, I think you should take a look at the ShouldLog method. This will allow you to determine whether LogEntry will be registered based on the current configuration, and you can (hopefully) avoid creating objects that are not required.

Borrow code from the Enterprise Library 4.1 Walkthrough: Checking Filter Status Before Creating Log Messages :

 LogEntry logEntry = new LogEntry(); logEntry.Priority = 2; logEntry.Categories.Add("Trace"); logEntry.Categories.Add("UI Events"); if (Logger.ShouldLog(logEntry)) { // Perform operations (possibly expensive) to gather additional information // for the event to be logged. } else { // Event will not be logged. Your application can avoid the performance // penalty of collecting information for an event that will not be // logged. } 

Since you are using your own LogUtility class, you probably want to create a static property on LogUtility called ShouldLogVerbose or IsVerboseEnabled , and inside this property use the properly built LogEntry (for your application) to determine if the message will be logged. eg.

 if (LogUtility.IsVerboseEnabled) { LogUtility.LogVerbose( string.Format("Processing event {0}", currentEvent.EventIDImported), MethodBase.GetCurrentMethod().Name, this.GetType().Name ); } 
+2
source

For this category you can do the following:

 logWriter.TraceSources["category"].Level 

See http://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.logging.logsource_members.aspx

+1
source

All Articles