Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a vb.net 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
Posted

1 solution

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

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


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

XML
<log4net>
  <root>
    <level value="ALL" />
    <appender-ref ref="MyAppender" />
  </root>
  <appender name="MyAppender" type="SomeNamespace.MyAppender">
    <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>
</log4net>


Different appenders have different configs.

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

C#
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.");
            Console.ReadKey();
        }
    }
}


Hope that helps.
 
Share this answer
 
Comments
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 1.2.10.0, etc...
log4net:ERROR XmlHierarchyConfigurator: Appender named [MyAppender] not found.

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

Thanks
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