I recently wanted to set up log4net on a project I’ve been working on and found the documentation somewhat lacking when trying to find a quick and easy setup guide. So, once I had everything set up and working, I resolved to create one myself.
So, in less time than it takes to get a pizza delivered, here is how to quickly get log4net setup on your project.
What you’ll need:
- The log4net dll (you can find the most recent version here)
- A .Net project/solution that you’d like to add logging to (you’re on your own here)
- Access to the Assembly.cs file for said project
- Access to the app.config or web.config file for said project
- A place to store your logging files (I will be using C:\ in this example, but this is one area in which the documentation actually excels)
Implementation:
- Place the log4net dll somewhere that you can reference it easily. I like to keep a Libraries folder around in most of my projects to hold any 3rd party dlls, but the choice is up to you.
- Add a reference to the dll in your project solution:

- Add the following code to the Assembly.cs file. More documentation here:
- Add the following section to the web/app.config file in the <configuration> node:
- Create a new section in the web/app.config using log4net as the node name:
- At the top of the class in which you want to implement logging, add the using log4net; directive.
- Define a static logger variable at the top of your class. Something like this will work:
- Start adding logging statements to your code, for example:
- Altogether then, your class might look something like this:
- Which would produce this output when run:
[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.
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="C:\logfile.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
using System.Collections.Generic;
using System.Text;
using log4net; namespace log4netDemo
{
class Program
{
// Define a static logger variable so that it references the name of your class
private static readonly ILog log = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
log.Info("Entering application.");
for (int i = 0; i < 10; i++)
{
log.DebugFormat("Inside of the loop (i = {0})", i);
}
log.Info("Exiting application.");
}
}
}
2008-05-12 19:53:28,260 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i = 0)
2008-05-12 19:53:28,280 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i = 1)
2008-05-12 19:53:28,280 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i = 2)
2008-05-12 19:53:28,280 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i = 3)
2008-05-12 19:53:28,280 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i = 4)
2008-05-12 19:53:28,280 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i = 5)
2008-05-12 19:53:28,280 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i = 6)
2008-05-12 19:53:28,280 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i = 7)
2008-05-12 19:53:28,280 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i =
2008-05-12 19:53:28,280 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i = 9)
2008-05-12 19:53:28,280 [11] INFO log4netDemo.Program [(null)] – Exiting application.
That really should be all there is to it. Although I did find the lack of a simple, quickstart guide daunting, there is actually a lot of good documentation on the log4net site. Particularly, if you want to use a different appender or read a more general introduction.
If you’re interested, I’ve zipped up a sample solution using the code above. You can download it here.


Entries (RSS)
Thanks for the example!
Thanks for the info.
Hi,
Thanks for the example and explanation you’ve provided.
I did the same you mentioned but I have a problem.
I have a windows service, i went to the App.config and added the needed:
and in the cs class:
i added the using:
using log4net;
and in the class i added:
private static readonly ILog log = LogManager.GetLogger(typeof(Program));
then in each method in this i added:
log.info(“entering method x”);
Result: When installing the windows service, an Exception is thrown:
TypeInitializationException
Usually this exception occurs when there’s sthg wrong in the app.config file.
Also on Build, the following messages are displayed :
Message 1 Could not find schema information for the element ‘log4net’. C:\Documents and Settings\Pv-User\My Documents\Visual Studio 2008\Projects\MyNewService\MyNewService\App.config 10 4 MyNewService
Message 2 Could not find schema information for the element ‘appender’. C:\Documents and Settings\Pv-User\My Documents\Visual Studio 2008\Projects\MyNewService\MyNewService\App.config 11 6 MyNewService
Message 3 Could not find schema information for the attribute ‘name’. C:\Documents and Settings\Pv-User\My Documents\Visual Studio 2008\Projects\MyNewService\MyNewService\App.config 11 15 MyNewService
Message 4 Could not find schema information for the attribute ‘type’. C:\Documents and Settings\Pv-User\My Documents\Visual Studio 2008\Projects\MyNewService\MyNewService\App.config 11 35 MyNewService
Message 5 Could not find schema information for the element ‘file’. C:\Documents and Settings\Pv-User\My Documents\Visual Studio 2008\Projects\MyNewService\MyNewService\App.config 12 8 MyNewService
Message 6 Could not find schema information for the attribute ‘value’. C:\Documents and Settings\Pv-User\My Documents\Visual Studio 2008\Projects\MyNewService\MyNewService\App.config 12 13 MyNewService
Message 7 Could not find schema information for the element ‘appendToFile’. C:\Documents and Settings\Pv-User\My Documents\Visual Studio 2008\Projects\MyNewService\MyNewService\App.config 13 8 MyNewService
Message 8 Could not find schema information for the attribute ‘value’. C:\Documents and Settings\Pv-User\My Documents\Visual Studio 2008\Projects\MyNewService\MyNewService\App.config 13 21 MyNewService
Message 9 Could not find schema information for the element ‘layout’. C:\Documents and Settings\Pv-User\My Documents\Visual Studio 2008\Projects\MyNewService\MyNewService\App.config 14 8 MyNewService
Message 10 Could not find schema information for the attribute ‘type’. C:\Documents and Settings\Pv-User\My Documents\Visual Studio 2008\Projects\MyNewService\MyNewService\App.config 14 15 MyNewService
Message 11 Could not find schema information for the element ‘conversionPattern’. C:\Documents and Settings\Pv-User\My Documents\Visual Studio 2008\Projects\MyNewService\MyNewService\App.config 15 10 MyNewService
Message 12 Could not find schema information for the attribute ‘value’. C:\Documents and Settings\Pv-User\My Documents\Visual Studio 2008\Projects\MyNewService\MyNewService\App.config 15 28 MyNewService
Message 13 Could not find schema information for the element ‘root’. C:\Documents and Settings\Pv-User\My Documents\Visual Studio 2008\Projects\MyNewService\MyNewService\App.config 18 6 MyNewService
Message 14 Could not find schema information for the element ‘level’. C:\Documents and Settings\Pv-User\My Documents\Visual Studio 2008\Projects\MyNewService\MyNewService\App.config 19 8 MyNewService
Message 15 Could not find schema information for the attribute ‘value’. C:\Documents and Settings\Pv-User\My Documents\Visual Studio 2008\Projects\MyNewService\MyNewService\App.config 19 14 MyNewService
Message 16 Could not find schema information for the element ‘appender-ref’. C:\Documents and Settings\Pv-User\My Documents\Visual Studio 2008\Projects\MyNewService\MyNewService\App.config 20 8 MyNewService
Message 17 Could not find schema information for the attribute ‘ref’. C:\Documents and Settings\Pv-User\My Documents\Visual Studio 2008\Projects\MyNewService\MyNewService\App.config 20 21 MyNewService
do you have any idea of why this error is occuring
Thanks for the example, I had log4net up and running smoothly thanks to your article.
Thx Justin,
Nice information provided by u…..
Great post ..yes and it really took just 15 minutes to see the log4net working.
thanks for you post.. it is really nice
Thanks.
Log4net made easy by you.
Hi,
Really good post.. I am planning to use Log4net in my WCF project, there i don’t want to specify the config settings is it possible to specify below setting in different file apart from app.config of WCF file?
Please confirm.