使用多个log4net文件记录器 [英] Using multiple log4net file loggers

查看:64
本文介绍了使用多个log4net文件记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文件附加器FileA,FileB和FileC. FileA我希望将其添加到根元素中(请参见下文中的更多内容). FileB和FileC用于特定消息,并为每个附加程序创建命名记录器.在代码中,我加载用于大多数消息的日志,如下所示:

I have file appenders FileA, FileB, and FileC. FileA I add to the root element as I want it to be a catch all, (more on this below). FileB and FileC I use for specific messages and create named loggers for each of those appenders. In code, I load the log I'm using for most messages like so:

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

其他记录器,我是这样加载的

The other loggers, I load like this

private static readonly log4net.ILog commandLog = log4net.LogManager.GetLogger("LoggerFileB");

正在发生的事情是我在LoggerFileB中得到了我所期望的,即仅特殊消息.问题是这些消息也显示在LoggerFileA中,我添加到root的全部内容.我可以为包罗万象创建一个特定的命名实例,而不是将其添加到根元素中,但是我希望在输出中将调用类型作为记录器名称.创建命名记录器意味着%logger输出日志的名称而不是类型.有没有一种方法可以准确地得到我想要的内容(总体上将记录器名称显示为类型,但不显示记录到其他命名记录器的消息)?希望我能缺少一些东西,并且有一个简单的解决方案.

What's happening is I'm getting what I expect in LoggerFileB, ie, ONLY the special messages. The problem is these messages also showing up in LoggerFileA, my catch-all I added to root. I could create a specific named instance for the catch-all, instead of adding it to the root element, but I want the calling type as the logger name in the output. Creating a named logger means that %logger outputs the name of the log instead of the type. Is there a way to get precisely what I want (the catchall to show the logger name as the type, but not show messages logged to other named loggers)? Hopefully I'm missing something and there is a simple solution.

这是我的log.config在这种情况下的外观示例.

Here's an example of what my log.config looks like for this situation.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <log4net>
        <appender name="FileA" type="log4net.Appender.RollingFileAppender">
            <file value="FileA.txt" />
            ...snip...
        </appender>
        <appender name="FileB" type="log4net.Appender.RollingFileAppender">
            <file value="FileB.txt" />
            ...snip...
        </appender>
        <appender name="FileC" type="log4net.Appender.RollingFileAppender">
            <file value="FileC.txt" />
            ...snip...
        </appender>
        <root>
            <level value="ALL" />
            <appender-ref ref="LoggerFileA" />
        </root>
        <logger name="LoggerFileB">
            <level value="ALL" />
            <appender-ref ref="FileB" />
        </logger>
        <logger name="LoggerFileC">
            <level value="ALL" />
            <appender-ref ref="FileC" />
        </logger>
    </log4net>
</configuration>

推荐答案

您可以将additivity设置为false:

You can use set the additivity to false:

<logger name="LoggerFileB" additivity="false">

这篇关于使用多个log4net文件记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆