开发者

Using multiple log4net file loggers

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");

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.

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>


You can use set the additivity to false:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜