log4net - getting appenders specific to only one logger

喜夏-厌秋 提交于 2019-12-10 17:49:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!