Using multiple log4net loggers

I have file applications FileA, FileB and FileC. I add FileA to the root element, because I want it to be a trick (more on this below). FileB and FileC I use for specific messages and create log names for each of these add-ons. In the code, I load the log that I use for most posts, for example:

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

Other registrars, I upload like this

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

What happens, I get what I expect in LoggerFileB, that is, ONLY special messages. The problem is that these messages also appear in LoggerFileA, my catch-all, which I added to root. I could create a specific named instance for catch-all, instead of adding it to the root element, but I want the call type to be the name of the log in the output file. Creating a named logger means that% logger displays the log name instead of type. Is there a way to get exactly what I want (catch it to show the registrar name as a type, but not show messages registered in other named registrars)? Hope I missed something and there is a simple solution.

Here is 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>
+3
1

additivity false:

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

Source: https://habr.com/ru/post/1771825/


All Articles