asp.net-corelog4netlog4net-configuration

Multiple Loggers with Log4net - logger name


I have a Asp.net core blazor app.

I am trying to create 2 separate loggers with Log4net.

The idea is that all errors related to the migration will go in a logger called "Migration", and all general errors will go in a logger called "Errors", but both loggers are catching all errors.

This is my Log4net.config

    <log4net>
    <appender name="Migration" type="log4net.Appender.FileAppender">
        <file value="logs/migration.log" />
        <appendToFile value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date %message%newline" />
        </layout>
    </appender>
    <appender name="Errors" type="log4net.Appender.FileAppender">
        <file value="logs/errors.log" />
        <appendToFile value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date - %level - %message%newline | %exception" />
        </layout>
    </appender>

    <root>
        <level value="DEBUG" />
        <appender-ref ref="Migration" />
        <appender-ref ref="Errors" />
    </root>
</log4net>

and this is how I use it:

    private static readonly ILog _logger = LogManager.GetLogger("Errors");

    _logger.info("blah");


    private static readonly ILog _migLogger = LogManager.GetLogger("Migration");

    migLogger.info("blah");

Solution

  • Your configuration doesn't define the loggers matching the names that you pass to the LogManager.GetLogger calls, being Migration and Errors. You did only define the appenders.

    For that reason it is using the root/default logger, which you configured to be using both appenders, writing to the migration.log and errors.log file.

    To resolve, define the missing named loggers, and only hook the appropriate appender to the corresponding logger.

    One way to do so looks like below. Notice the logger xml tags.
    Since you're only using named loggers, the root/default logger does not need to have any appenders configured.

    <log4net>
        <appender name="Migration" type="log4net.Appender.FileAppender">
            <file value="logs/migration.log" />
            <appendToFile value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date %message%newline" />
            </layout>
        </appender>
    
        <appender name="Errors" type="log4net.Appender.FileAppender">
            <file value="logs/errors.log" />
            <appendToFile value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date - %level - %message%newline | %exception" />
            </layout>
        </appender>
    
        <logger name="Migration">
            <level value="DEBUG" />
            <appender-ref ref="Migration" />
        </logger>
    
        <logger name="Errors">
            <level value="DEBUG" />
            <appender-ref ref="Errors" />
        </logger>
    
        <root></root>
    </log4net>
    

    Alternatively, you can treat the root/default logger as the Errors logger.
    The configuration would then look like below. It is the additivity="false" that prevents that the messages of the root/default logger also appear in the Migration logger

    <log4net>
        <appender name="Migration" type="log4net.Appender.FileAppender">
            <file value="logs/migration.log" />
            <appendToFile value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date %message%newline" />
            </layout>
        </appender>
    
        <appender name="Errors" type="log4net.Appender.FileAppender">
            <file value="logs/errors.log" />
            <appendToFile value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date - %level - %message%newline | %exception" />
            </layout>
        </appender>
    
        <logger name="Migration" additivity="false">
            <level value="DEBUG" />
            <appender-ref ref="Migration" />
        </logger>
    
        <root>
            <level value="DEBUG" />
            <appender-ref ref="Errors" />
        </root>
    </log4net>