问题
I'm looking for a way to get all appenders attached to one logger instance.
I tried:
Hierarchy hierarchy = LogManager.GetRepository() as Hierarchy;
hierarchy.GetAppenders()
as per documentation this returns all appenders for all loggers currently configured.
When I try this:
LogManager.GetLogger("MyLoggerName").Logger.Repository.GetAppenders();
I get the same result.
I would like to retrieve only appenders attached to one logger ("MyLoggerName" in this case)
Where am I wrong?
回答1:
When you call the following code
LogManager.GetLogger("MyLoggerName").Logger.Repository.GetAppenders();
you are in fact asking the exact same data as hierarchy.GetAppenders()
because Hierarchy
inherits LoggerRepositorySkeleton
, which implements ILoggerRepository
, the type returned by Logger.Repository
.
You can however get the list of "first level" appenders by using the Logger
class that lives in the Hierarchy
namespace:
var h = LogManager.GetRepository() as Hierarchy;
var l = h.GetLogger("MyLoggerName", h.LoggerFactory);
// do something with the l.Appenders property
You will have to handle special cases like bufefring or filtering appenders from there
来源:https://stackoverflow.com/questions/25364535/log4net-getting-appenders-specific-to-only-one-logger