log4net为什么会打印两次?

用“log4net 使用”做关键字在bing上搜索,点开排序第一的链接:http://33liuhongwei33.blog.163.com/blog/static/39923778201156101213418/

试用之后可以打印log,但是会打印两次

再用“log4net log twice”做关键字在bing上搜索,同样点开排序第一的链接:http://stackoverflow.com/questions/651277/eliminate-duplicate-logging-in-log4net

找到了问题的根源,关键在这句话:

“You don't need to indicate the appender-ref in the logger myprogram since it will inherit them from the root logger; if you specify them again it will log twice.”

原来自定义的logger设置会从root logger继承下来(我承认我没有仔细读log4net的文档,不过90%以上的人也不会去仔细从头到尾的读文档,总有些东西是能用就行的,人没有精力也没有时间把每件事都研究到最细处,

但是人又需要把某件事研究到最细处,所谓一招鲜吃遍天是也。)

所以上面的文章中的logger节需要这样修改:

    <root>
<level value="INFO"></level>
<appender-ref ref="FileAppender"></appender-ref>
<appender-ref ref="ConsoleAppender"></appender-ref>
</root>
<logger name="MyLogger">
<level value="ERROR"></level>
</logger>

posted on 2015-07-09 17:02  sPhinX  阅读(762)  评论(0编辑  收藏  举报

导航