I'm sure this is well documented, but for my own reference and your convenience, here's one from my list of favorite log4net tips and tricks: how to instrument your code so that log4net automatically picks up your configuration.
On average, I've been so happy with how well log4net has fit my application logging needs that most of my projects end up using it: console apps, web applications, class libraries. Needless to say I use it a lot, and I get tired of writing the same configuration code over and over:
private static void Main()
string basePath = AppDomain.CurrentDomain.BaseDirectory;
string filePath = Path.Combine(basePath, "FileName.log4net");
log4net documentation refers to a Configuration Attribute (XmlConfiguratorAttribute), but it can be frustrating to use if you're not sure how to set it up. The trick is how you name your configuration file and where you put it. I'll walk through how I set it up...
log4net using XmlConfiguratorAttribute Walkthrough
- Add an Assembly Configuration Attribute: log4net will look for this configuration attribute the first time you make a call to a logger. I typically give my configuration file a "log4net" extension. Place the following configuration attribute in the AssemblyInfo.cs file in the assembly that contains the main entry point for the application.
[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension = "log4net",Watch = true)]
- Create your configuration file: As mentioned previously, the name of the configuration file is important as is where you put it. In general, the name of the configuration file should follow the convention: full-assembly-name.extension.log4net. The file needs to be at the base folder of the application, so for WinForms and Console applications it resides in the same folder as the main executable, for ASP.NET applications it's the root of the web-site along side the web.config file.
Project Type Project Output log4net file name Location WinForm App Program.exe Program.exe.log4net with exe Console App Console.exe Console.exe.log4net with exe Class Library Library.dll N/A ASP.NET /bin/Web.dll /Web.dll.log4net Web root (/)
- Define your Configuration Settings: Copy and paste the following sample into a new file. I'm using the Rolling Appender as this creates a new log file every time the app is restarted.
<?xml version="1.0" encoding="utf-8" ?>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<!-- Define output appenders -->
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<rollingStyle value="Once" /> <!-- new log file on restart -->
<maxSizeRollBackups value="10"/> <!-- renames rolled files on startup 1-10, no more than 10 -->
<datePattern value="yyyyMMdd" />
<param name="Header" value="[START LOG] " />
<param name="Footer" value="[END LOG] " />
<conversionPattern value="%d [%t] %-5p %c [%x] - %m%n" />
<!-- Setup the root category, add the appenders and set the default level -->
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
- Make a logging call as early as possible: In order for the configuration attribute to be invoked, you need to make a logging call in the assembly that contains that attribute. Note I declare the logger as static readonly as a JIT optimization.
public class Global : System.Web.HttpApplication
private static readonly ILog log = LogManager.GetLogger(typeof(Global));
protected void Application_Start(object sender, EventArgs e)
log.Info("Web Application Start.");