// Configure log4net using the .config file [assembly: log4net.Config.XmlConfigurator(Watch=true)] // This will cause log4net to look for a configuration file // called TestApp.exe.config in the application base // directory (i.e. the directory containing TestApp.exe) // The config file will be watched for changes.
This file is named 'app.config' in the source. Visual Studio seems to rename this to 'ApplicationName.exe.config' when it is copied to the bin directory during compilation.
<?xml version="1.0"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <appender name="FileAppender" type="log4net.Appender.FileAppender"> <file value="log-file.txt" /> <appendToFile value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss.fff},%thread,%logger,%level,%message%newline" /> </layout> </appender> <root> <level value="DEBUG" /> <appender-ref ref="FileAppender" /> </root> </log4net> </configuration>
To roll the log files, use something like this:
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="log-file.txt" /> <appendToFile value="true"/> <immediateFlush value="true"/> <maxSizeRollBackups value="100"/> <rollingStyle value="Size"/> <maximumFileSize value="10Mb"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss.fff},%thread,%logger,%level,%message%newline" /> </layout> </appender>
To specify only certain loggers:
<appender ...> ... <filter type="log4net.Filter.LoggerMatchFilter"> <loggerToMatch value="GeneralLog"/> </filter> <filter type="log4net.Filter.DenyAllFilter"/> </appender>
To remove certain loggers:
<log4net> ... <root> ... </root> <logger name="GeneralLog"> <level value="Off" /> </logger> </log4net>
To switch off all logging:
<log4net threshold="OFF" />
namespace FooBar { using log4net; internal static class Logger { private static ILog _general; internal static ILog General { get { if (_general == null) { _general = LogManager.GetLogger("General"); // Ensure the logger gets configured. log4net.Config.XmlConfigurator.Configure(); } return _general; } } } }