If you are looking for a great introductory article on Log4Net, I would recommend reading the Log4Net tutorial by Tim Corey.
Tim goes through quite a bit, I just want to cover the very bare minimum for getting log4net to work in a console application.
Step 0 – Reference Log4Net
Using NuGet this is really easy – but no matter how you do it, you should end up with a reference to log4net in your project.

Step 1 – Add an entry to AssemblyInfo.cs
Add the assembly for the log4net.config to AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Step 2 – Config settings file for App.config
Add a config file called App.config to your solution if it is not there already…
I added the following config settings within this file as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" />
</configSections>
<!-- Log4net Logging Setup -->
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender,log4net">
<file value="c:\\mylogfile.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="FileAppender"/>
</root>
</log4net>
</configuration>
Step 3 – Create an instance of a logger and call logging
To create an instance of a logger, there are a couple of ways you can do this….
One suggestion by Tim is to add the following line within each class where you want to have logging…
private static readonly log4net.ILog log = log4net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
I am not sold on this approach, this is something where I would use Dependency Injection to inject the logger, but for a simple console app example this would complicate things, so I would go with the following approach just to get things working…
class Program
{
static void Main(string[] args)
{
log4net.Config.BasicConfigurator.Configure();
ILog log = log4net.LogManager.GetLogger(typeof(Program));
log.Debug("This is a debug message");
log.Warn("This is a warn message");
log.Error("This is a error message");
log.Fatal("This is a fatal message");
Console.ReadLine();
}
}
Step 4 - Run the application
Simply run the application and you should see log file being created.
_2012-01-30_10-28-10_thumb.png)
The Decimal & Binary System and Converting between Binary and Decimal
I am assuming that a basic knowledge of binary and decimal notation is known, if you are unfamiliar with it, read up on binary and decimal
- The decimal system has a base or radix of 10 – this means that each digit in the number is multiplied by 10 raised to a power corresponding to that digits position.
- The same principles holds for decimal fractions but negative powers of 10 are used
- A number with both an integer and fractional part has digits raised to both positive and negative powers of 10
- In the binary system we have only two digits, 1 & 0 to represent numbers
- To convert from binary to decimal, all that is required is to multiply each binary digit by the appropriate power of 2 and add the results
- To convert from decimal to binary, the integer and fractional parts are handled separately
Hexadecimal Notation
- Because of the inherent binary nature of digital computer components, all forms of data within computers are represented by various binary codes, however the binary system is cumbersome for human beings, thus we use a more compact notation called hexadecimal.
- Binary digits are grouped into sets of four, each possible combination of four binary digits is given a symbol as follows
| 0000 = 0 | 0100 = 4 | 1000 = 8 | 1100 = C |
| 0001 = 1 | 0101 = 5 | 1001 = 9 | 1101 = D |
| 0010 = 2 | 0110 = 6 | 1010 = A | 1110 = E |
| 0011 = 3 | 0111 = 7 | 1011 = B | 1111 = F |
- A sequence of hexadecimal digits can be thought of as representing an integer in base 16, thus 2C = (2H * 16^1) + (CH * 16^0) = (2 * 16^1) + (12 x 16^0) = 44
- Hexadecimal notation is used not only to represent integers, it is also used as a concise notation for representing any sequence of binary digits
| Decimal | Binary | Hex |
| 0 | 0000 | 0 |
| 5 | 0011 | 5 |
| 15 | 1111 | F |
| 16 | 0001 0000 | 10 |
| 31 | 0001 0000 | 1F |
| 255 | 1111 0000 | FF |
| 256 | 0001 0000 0000 | 100 |
Benefits of using hexadecimal include
- It is more compact than binary notation
- In most computers, binary data occupy some multiple of 4 bits, and hence some multiple of a single hexadecimal digit
- It is extremely easy to convert between binary and hexadecimal