5

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.

Randy Levy
  • 22,566
  • 4
  • 68
  • 94
StephenH
  • 55
  • 1
  • 4

2 Answers2

2

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

Grigori Melnik
  • 4,067
  • 2
  • 34
  • 40
2

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
    );
}
Randy Levy
  • 22,566
  • 4
  • 68
  • 94
  • Thanks for pointing me in the right direction; that's exactly what I was looking for. I'd intended to put create a static property as you describe but wasn't sure how to get the boolean - "ShouldLog" is what I was after. – StephenH May 19 '11 at 09:12