DevEstacion
DevEstacion

Reputation: 1967

log4net Are the instance created for classes stored? And parameter overhead vs checking if log is enabled

I have questions about the log4net library.

  1. Are the instances created by calling LogManager.GetLogger stored and is there an expiration for it? I am asking because i want to know if its better to wrap the log4net to create an instance on calling a Log<T> method

  2. Since I'm wrapping the log4net library, string parameter construction overhead is there since I'm now just gonna check IsDebugEnabled and so on on the wrapped method. Is the overhead a good price to pay for ease of use or should I just clutter my code with lots of If(IsDebugEnabled) checking?

For better visualization here is a code from the wrapper.

    private static void logOnDebug(string message, Exception ex, bool isAsync)
    {
        if (!_logger.IsDebugEnabled)
            return;

        if (isAsync)
            Task.Factory.StartNew(() => _logger.Debug(message, ex));
        else
            _logger.Debug(message, ex);
    }

Upvotes: 0

Views: 107

Answers (1)

samy
samy

Reputation: 14962

  1. All loggers are kept in a hash table in the Hierarchy class (that implements the IRepository). There is a method Clear on the Hierarchy that empties the hash table. No implicit expiration exists on the hashtable.

  2. This question is subjective and depends on both the cost to construct your messages and your preferences. Castle does something I like a lot, which is to add a new signature for the ILogger (their custom abstraction over logging) that takes a Func<string> as parameter. The lambda is not evaluated before testing the level and seeing whether or not the event is going to be logged:

    Logger.Info(() => VeryExpensiveStringBuilding());

Upvotes: 1

Related Questions