Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a application and I would like to use Log4Net for logging.
I have found loads of examples of it in use but can't find anything that explains of the basics. What I need to know is where does the default config file need to be and what should it be called. I am sure this is simple but I am unable to find any documentation anywhere.
Many thanks

1 solution

First of all you neet to add the following in the web.config or app.config.

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

Secondly you need to specify what appender to use, also in the config file.

    <level value="ALL" />
    <appender-ref ref="MyAppender" />
  <appender name="MyAppender" type="SomeNamespace.MyAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionpattern value="%date [%thread] %level %logger - %message%newline" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelmin value="INFO" />
        <levelmax value="FATAL" />

Different appenders have different configs.

Finally, here is an example on how to use it all from code.

using System;

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

namespace ConsoleApplication7
    class Program
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger (System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        static void Main(string[] args)
            log.Info("Info logging");

            Console.WriteLine("Press any key to exit.");

Hope that helps.
Share this answer
dnibbo 19-May-14 6:46am    
Hi Daniel

Thanks for taking the time to reply.
I am a bit closer but still am unable to get any logs.
When the code executes the GetLogger method I get an error from log4net:

log4net:ERROR XmlHierarchyConfigurator: Could not create Appender [MyAppender] of type [MyModuleName.MyAppender]. Reported error follows.
System.TypeLoadException: Could not load type [MyModuleName.MyAppender]. Tried assemembly [log4net, Version, etc...
log4net:ERROR XmlHierarchyConfigurator: Appender named [MyAppender] not found.

I am using so I may have converted the code incorrectly:
_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
_log.Debug("Module Initialised")

Daniel Bohlin 19-May-14 9:43am    
The "MyAppender" was just an example of an appender. What you have to put there is a real appender.

There are many different appenders and you have to choose one depending on where you want your messages to be sent. Where do you want your messages to go?

There is a real cool appender called Quilt4Log4Net. It sends log messages to a service where you get a real nice summary. It is easy to configure too, I can tell you how if you are interested.
dnibbo 19-May-14 11:11am    
Thanks Daniel, I did not realise quite what these config settings were.
I have changed it to a RollingFileAppender and I getting my log messages now.
Thanks so much for your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900