My favourite log4net config settings

It occurred to me, tonight, as I copied and pasted the same generic log4net config to a new project that I do this because this is my favourite log4net configuration. And, if it's a good enough starting point for all my projects then it may be of interest to others.

 

This config writes to two appenders; the console and a file appender. The file appender is pretty neat in that it;

Creates log files in a logs/ folder
The standard file is "general.txt"
This file grows on each iteration, until it reaches a maximum size of 2MB
When it hits 2MB, the file is renamed and a new "general.txt" file is opened
Maintains a maximum of 10 renamed / archived log files (so, a total of 20MB [...]

Adding log4net to a new project - cribsheet

log4net is a fantastic library for adding configurable logging to your project. Configuration is relatively straightforward but I always get caught out when adding it to a new project.

So, here's a cribsheet for adding it in…

Download the latest stable library, and add a reference to it within your own project
Ensure that you have an app.config file
Add the configSection for log4net (see samples, below)
Add the log4net section (another sample)
Add an ILog object to your class
And, the bit I always forget, add the [assembly] entry to the top of your class

In your app.config file, add log4net to the <configSections> (if you don't have one of these, it goes at the next level down from the <configuration> element;

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>

 

Next, [...]