log4net纯代码配置与c#中的过滤器
我试图纯粹通过代码配置 Log4Net,但是当我使用最小配置进行配置时,来自 NHibernate 和 Fluent 界面的日志消息淹没了我。
所以,我想做的很简单。 告诉 Log4Net 仅显示我的单个类的日志消息。 我玩了一下,但无法弄清楚......
任何人都可以帮忙,我认为下面的代码说明了我的想法:
var filter = new log4net.Filter.LoggerMatchFilter();
filter.LoggerToMatch = typeof(DatabaseDirectory).ToString();
filter.AcceptOnMatch = false;
var x = new log4net.Appender.ConsoleAppender();
x.Layout = new log4net.Layout.SimpleLayout();
x.AddFilter(filter);
log4net.Config.BasicConfigurator.Configure(x);
好的,感谢您的帮助,但这里一定有一些问题。 但我越来越近了。 我尝试了 XML 配置,它有更多的文档。 我设法使用以下 XML 配置实现了预期的结果。 上面的“纯代码”版本肯定存在一些配置错误。
以下 XML 配置提供了“正确”的输出,这与上面代码中的配置不同。 有人看出区别了吗?
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="ConsoleAppender" />
</root>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="Examples.FirstProject.Entities.DatabaseDirectory"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%C.%M] %-5p %m%n" />
</layout>
</appender>
I am trying to configure Log4Net purely by code, but when I did with a minimal configuration, I was flooded by logging messages from NHibernate and the fluent interface.
So, what I am trying to do is simple. Tell Log4Net to show me only log messages of my single class. I toyed around a little bit, but can't figure it out...
Can anyone help, I think the following code illustrates my idea:
var filter = new log4net.Filter.LoggerMatchFilter();
filter.LoggerToMatch = typeof(DatabaseDirectory).ToString();
filter.AcceptOnMatch = false;
var x = new log4net.Appender.ConsoleAppender();
x.Layout = new log4net.Layout.SimpleLayout();
x.AddFilter(filter);
log4net.Config.BasicConfigurator.Configure(x);
Ok, thanks for your help, but there must be some issue here. But I get closer. I tried the XML configuration, which has much more documentation. And I managed to achieve the desired result using the following XML configuration. There must be some misconfiguration in the "pure code" version above.
The following XML configuration provides the "correct" output, which is not the same than the config in code above. Anybody sees the difference?
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="ConsoleAppender" />
</root>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="Examples.FirstProject.Entities.DatabaseDirectory"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%C.%M] %-5p %m%n" />
</layout>
</appender>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想通了..有时,只要写下来就会让你大开眼界...
看看缺少了什么:-) DenyALL 过滤器!
更多代码示例:
确实丑陋但有效(它搞砸了突出显示,不要错过最后几行):
I figured it out.. Sometimes, just writing it down opens your eyes...
See what was missing :-) The denyALL filter!!
Some more code examples:
Really UGLY but working (it screws up the highlighting, do not miss the last lines):
这是通过代码使用 XMLDocument 加载 xml 来配置 log4net 的另一种方法。 与 Christian 的示例的区别在于,我使用的是
XmlConfigurator.Configure
重载,该重载采用XmlElement
作为参数。 我还使用单刻度线而不是双引号。 总而言之,我认为它是最干净的。Here is anohter way to configure log4net with XML via code using XmlDocument to load the xml. The difference from Christian's example is that I am using the
XmlConfigurator.Configure
overload that takes anXmlElement
as a parameter. I also used single tick marks rather than doubling up the double quotation marks. All in all, I think that it is the tiniest bit cleaner.以下代码介绍如何在代码中配置 AdoNetAppender。 如果要添加过滤器,则需要在 GetAppender() 方法中的appender 实例上添加过滤器实例。 为了在应用程序中使用记录器,建议使用依赖注入来隔离记录器实现和接口。 此外,使用 DI 范围概念使其成为单例以避免对象重新创建。
注意:CrmConfigHelper 类用于访问应用程序配置文件 appSettings 部分。
代码示例如下所示
The following codes works on how to config AdoNetAppender in codes. If you want to add filter, you need to add filter instance on the appender instance in GetAppender() method. For using logger in your application, Dependency Injection is recommended to be used to isolate logger implementation and interface. In addition, use DI scope concept to make it singleton to avoid object recreation.
Note: CrmConfigHelper class is used to get access to application configuration file appSettings section.
The codes samples are shown below