Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net - getting appenders specific to only one logger

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?

like image 425
andreav Avatar asked Aug 18 '14 13:08

andreav


1 Answers

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

like image 177
samy Avatar answered Oct 27 '22 01:10

samy