Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enterprise Library logging - how to get the configured logging level at runtime?

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.

like image 886
StephenH Avatar asked Oct 12 '22 00:10

StephenH


2 Answers

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.

Based on the above I think you should take a look at the ShouldLog method of LogWriter. It will let you determine if a LogEntry will 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:

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 would probably want to create a static property on LogUtility called ShouldLogVerbose or IsVerboseEnabled and inside of that property use a "properly" constructed LogEntry (for your application) to determine if the message would be logged. e.g.

if (LogUtility.IsVerboseEnabled)
{
    LogUtility.LogVerbose(
        string.Format("Processing event {0}", currentEvent.EventIDImported), 
        MethodBase.GetCurrentMethod().Name, 
        this.GetType().Name
    );
}
like image 147
Randy supports Monica Avatar answered Nov 09 '22 19:11

Randy supports Monica


For a given 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

like image 26
Grigori Melnik Avatar answered Nov 09 '22 19:11

Grigori Melnik