Reputation: 122052
I would like to create an area in my application that will let sysadmins modify logger levels directly.
This answer from 2009 implies that I can do it with
foreach (ILog logger in log4net.LogManager.GetCurrentLoggers())
{
((log4net.Repository.Hierarchy.Logger)logger).Level =
log4net.Core.Level.Error;
}
My application uses log4net 2.0.3 (1.2.13) and this seems to not be available as no implementation of ILog
inherits that class.
Instead I can grab each ILog
's Logger
property for an instance of ILogger
. This gives me the name but will not let me query or set the Level. While this is an ability that IS available on the implementation I cannot simply do a cast as the implementation is an internal class.
How do I do this in recent versions?
Upvotes: 0
Views: 460
Reputation: 6279
You'll have to grab the Logger
property off of each logger
and cast it as a log4net.Repository.Hierarchy.Logger
.
foreach (ILog logger in log4net.LogManager.GetCurrentLoggers())
{
var log = logger.Logger as log4net.Repository.Hierarchy.Logger;
Console.WriteLine("Name: {0}, Level: {1}", log.Name, log.Level);
log.Level = log4net.Core.Level.Error;
}
Upvotes: 1
Reputation: 124696
You're right that you have to get each ILog
's Logger property: then you need to cast it to log4net.Repository.Hierarchy.Logger
.
ILog.Logger
returns an ILogger
, which as you've seen doesn't expose the writeable Level
property, which I guess is considered to be an internal implementation detail. But casting it to the concrete log4net.Repository.Hierarchy.Logger
type gives you access to what you need.
From the sample in the question whose answer you linked to:
foreach (log4net.ILog log in log4net.LogManager.GetCurrentLoggers())
{
log4net.Repository.Hierarchy.Logger logger =
(log4net.Repository.Hierarchy.Logger)log.Logger;
Debug.WriteLine(
String.Format("{0} Parent {1} Level {2} EffectiveLevel {3}<br>",
logger.Name,
logger.Parent.Name,
logger.Level == null ? "<null>" : logger.Level.Name,
logger.EffectiveLevel
)
);
}
Upvotes: 1