log4Net - mcbride-clint/DeveloperCurriculum GitHub Wiki

Log4Net is an older 3rd party logging library made by Apache. It is lacking some of the more modern features but it remains a powerful tool.

Without additional code and customization, log4Net uses a static function to get a logger in each class that you want to log from.

ILog x = LogManager.GetLogger("wombat");

This can be extremely simple to add into an existing application but it directly ties your code into a dependency on log4Net, making testing and sharing code that may not use log4net difficult.

Configuration

Configuration is accomplished mainly through XML .config files. Logging destinations are called Appenders. Log4Net can send messages to many destinations including files, in-memory, databases, and the console.

<log4net>
    <!-- A1 is set to be a ConsoleAppender -->
    <appender name="A1" type="log4net.Appender.ConsoleAppender">
 
        <!-- A1 uses PatternLayout -->
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" />
        </layout>
    </appender>
    
    <!-- Set root logger level to DEBUG and its only appender to A1 -->
    <root>
        <level value="DEBUG" />
        <appender-ref ref="A1" />
    </root>
</log4net>

See Also

⚠️ **GitHub.com Fallback** ⚠️