每个命名空间有不同的附加程序
我正在尝试设置一个通用日志记录库,它根据当前堆栈确定 ILog 实例以及要使用的最佳 ILog 实例是什么。
我的配置设置如下:
<log4net>
<!-- appenders omitted -->
<root></root>
<logger name="MyAssembly.MyNamespace">
<level value="WARN" />
<!-- appender list -->
</logger>
</log4net>
我有一个这样的类:
namespace MyAssembly.MyNamespace.SubNamespace {
public class MyClass { ... }
}
当我尝试获取 ILog
的实例时,我传入类型 (var log = LogManager.GetLogger (typeof(MyClass)).Namespace);
),我希望它检测到没有配置任何记录器,因此它将在命名空间树中上升一级(到 MyAssembly.MyNamespace
),然后查看此时是否已配置。
问题是为 MyAssembly.MyNamespace.SubNamespace
返回的 ILog
是为 WARN 事件(及以上)配置的,本质上是我为其父级配置的。 当所需名称包含定义的名称时,而不是当它等于名称时,Log4net 似乎会返回ILog
。
当名称与配置中定义的名称相同时,如何让 Log4net 仅返回有效 记录器?
I'm trying to set up a common logging library which determines the ILog
instance based on the current stack and what is the best instance of ILog to use.
I've got my config set up like this:
<log4net>
<!-- appenders omitted -->
<root></root>
<logger name="MyAssembly.MyNamespace">
<level value="WARN" />
<!-- appender list -->
</logger>
</log4net>
And I have a class like this:
namespace MyAssembly.MyNamespace.SubNamespace {
public class MyClass { ... }
}
When I try and get an instance of ILog
I pass in the type (var log = LogManager.GetLogger(typeof(MyClass)).Namespace);
) and I want it to detect that there isn't any logger configured, so it will then go up one level in the namespace tree (to MyAssembly.MyNamespace
) and then see if it is configured at that point.
The problem is that the ILog
returned for MyAssembly.MyNamespace.SubNamespace
is configured for WARN events (and above), essentially what I configured for it's parent. Log4net seems to be returning the ILog
when the required name contains a defined name, rather than when it equals the name.
How do I get Log4net to only return a valid logger when the name is the same as one defined in the config?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
log4net
根据名称将记录器视为存在于层次结构中,因此名称为MyAssembly.MyNamespace.SubNamespace
的记录器是名称为MyAssembly 的记录器的子级。我的命名空间
。如果没有为记录器指定级别(WARN 等),系统将遍历记录器层次结构,直到找到具有配置级别的记录器 - 并且该级别成为该记录器的有效级别。
在您的情况下,子记录器没有指定级别,因此它继承了其父记录器的级别 - WARN。 如果您指定,
那么您将从子记录器获得 DEBUG 输出(发送到为父记录器配置的附加程序)。
log4net
treats loggers as existing in a hierarchy based on their names, so the logger with nameMyAssembly.MyNamespace.SubNamespace
is a child of the logger with nameMyAssembly.MyNamespace
.If no level (WARN etc.) is specified for a logger, the system traverses the logger hierarchy until it finds a logger with a configured level - and that becomes the effective level for that logger.
In your case, the child logger has no level specified, so it inherits the level of its parent - WARN. If you specify
then you will get DEBUG output from the child logger (sent to the appenders configured for the parent).