How to set up Log4Net

Log4Net is a logging framework written by our good friends at Apache. It’s a .Net port from their Java logging framework Log4J.

Check out their other amazing logging frameworks!


Why did I choose Log4Net above all other logging frameworks for .Net?

A couple of reasons:

  1. It’s free (hooray!)
  2. It’s easy to use. It’s as simple as
    App.Log.Info("This is some information");
    App.Log.Debug("This is some debug message");
    catch (Exception ex)

    and so on..

  3. It’s practically the same syntax for all their logging frameworks. Whether you use PHP, Java or .Net.

Why would I want to write about Log4Net? Well, simply because I believe that young developers such as myself lack the ability to debug properly.
Usually you write a small program, you think it’s ready to be deployed and you tell all your friends about it.

Of course, the program hasn’t been tested properly and it’s most likely still full of bugs. Now, as a good developer you should be able to fix those nasty bugs.
Usually that’s where it fails. They don’t know what the problem is. The user is mostly simply presented with a general exception.

And that’s where Log4Net comes in. It’s an easy to use framework to log your exceptions.

NOTE: It’s not some kind of magical framework that does everything for you. It simply logs the exceptions/messages that you want to be logged.
In other words, YOU as a developer still have to tell the program when to log an exception.

Enough intro, let’s get on with it!


When you download the binaries from their website, you’ll notice in the bin folder that there are a few folders. If you are using Mono.Net, you can get the libraries from the “mono” folder.
I’m only interested in the default “net” folder. Then choose your .Net framework version. (I’m currently coding using .Net 4)

Anyway, you’ll find a .dll file in there. Just include that file in your project and you’ve installed Log4Net. Simple eh?


Configuration can be somewhat more confusing. I’d advise you to check their documentation first.

As you can see, Log4Net has a lot of Logging appenders.

Logging appenders are methods of logging something. For example, Log4Net can log to a MS SQL Server, SQLite database, the console, a simple .log file and much more!

Let’s get practical

The configuration goes into your app.config file.

First, you’ll have to add a config section for Log4Net.

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>

Once that is done, we can add a new configuration section.


Now, inside this configuration section, we will add our appenders and our root logger.
I always start with defining the root logger.

 <level value="DEBUG"/>
 <appender-ref ref="RollingLogFileAppender"/>

Inside the root element, you can specify the minimum logging level. In this case I will log all DEBUG messages and above.

After that, you will have to specify all the appenders that you want to use. (In this case I’m only using the rolling log file appender. You can read more about it in the documentation.

Once that’s done we can start with the configuration of the actual appenders.

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
 <param name="File" value="log.xml"/>
 <param name="AppendToFile" value="true"/>
 <rollingStyle value="Size"/>
 <maxSizeRollBackups value="10"/>
 <maximumFileSize value="10MB"/>
 <staticLogFileName value="true"/>
 <layout type="log4net.Layout.PatternLayout">
 <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>

I’m not going to explain all the details of this configuration, if you want to know more, just check out the documentation. (

What’s next?

Of course we still have to import our namespaces.

// Import log4net classes.
using log4net;
using log4net.Config;

Once that’s done, open up App.xaml.cs (using XAML) and declare the global “log” variable.

public static readonly ILog Log = LogManager.GetLogger(typeof(App));

One more thing, tell Log4Net to use the configuration.


So, how do I use it?

It’s really simple. I’ve already shown you a few examples in the intro."Your info message here");

etc. to log simple strings.

catch (Exception ex)

etc. to log exceptions thrown by .Net.
You can log the following levels. The following levels are defined in order of increasing priority:

  • ALL
  • INFO
  • WARN
  • OFF

It that it?

Well, that’s it for the basics. There are a lot more things to do with this framework. Such as adding filters and setting up your own debug layout, but I’m going to leave that to you.

Remember, it’s important to read the documentation! (

~Michiel De Mey

Michiel De Mey

Full-time geek, Full Stack Engineer and Full Metal Hero. NodeJs, AngularJs, API design, WebSockets, WebSec & IoT enthusiast. Former San Francisco resident.

More Posts - Website - Twitter - Facebook - LinkedIn - Google Plus