Geeks With Blogs
Mark Pearl

 

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.

Log4Net

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.

 

Local Disk (C)_2012-01-30_10-28-10

Posted on Monday, January 30, 2012 10:29 AM C# | Back to top


Comments on this post: Log4Net basics with a Console Application (c#)

# re: Log4Net basics with a Console Application (c#)
Requesting Gravatar...
What is causing your blog to be oversized in the browser? I thought maybe it was an image, but it seems to be happening in each of your posts? I don't find it happening on other geek blogs.. Just kind of giving you a heads up... Wesley
Left by Ambit Energy on Feb 23, 2012 6:05 AM

# re: Log4Net basics with a Console Application (c#)
Requesting Gravatar...
Very easy to follow. Thanks
Left by Mike on Aug 17, 2012 4:09 PM

# re: Log4Net basics with a Console Application (c#)
Requesting Gravatar...
Great simple write up. Thanks a lot.
Left by matt on Nov 16, 2012 8:58 PM

# re: Log4Net basics with a Console Application (c#)
Requesting Gravatar...
Awesome tutorial. Great job(simple and step by step).

Thanks
Left by Prem Mishra on Dec 21, 2012 3:55 PM

# re: Log4Net basics with a Console Application (c#)
Requesting Gravatar...
I followed this settings but the stream goes to the console and not creating the file.
Left by Tonda on Jan 15, 2013 6:15 PM

# re: Log4Net basics with a Console Application (c#)
Requesting Gravatar...
Thanks. Saved me time.
Left by AJ on Feb 06, 2013 11:18 PM

# re: Log4Net basics with a Console Application (c#)
Requesting Gravatar...
This is one of the example is working.
Lot of them are not working
Good presentation.
Thanks
Uday Shastri
Left by Uday Shastri on Feb 26, 2013 11:05 PM

# re: Log4Net basics with a Console Application (c#)
Requesting Gravatar...
but whenever i am executing that my exe file out side of directory . file is not creating
Left by ravikumar on Feb 28, 2013 7:29 AM

# re: Log4Net basics with a Console Application (c#)
Requesting Gravatar...
Thanks a lot!!! Great and simple example!
Left by Nadia on May 22, 2014 6:58 PM

# re: Log4Net basics with a Console Application (c#)
Requesting Gravatar...
i suppose im lucky to have found this! I hope you will be adding more in the future...

utah seo company

Left by sasda on Jun 01, 2014 7:49 PM

# re: Log4Net basics with a Console Application (c#)
Requesting Gravatar...
@ravikumar it's because the configuration is not saved in the executable.
Left by Younes van Ruth on Jun 18, 2015 1:35 PM

# re: Log4Net basics with a Console Application (c#)
Requesting Gravatar...
its just started working!! Nicely written ...simple steps to kick-start
..after spending on several posts.. this is godsend for me.
Left by satish on Jun 18, 2015 5:04 PM

# re: Log4Net basics with a Console Application (c#)
Requesting Gravatar...
When I run the code, I can see the logs in console but It doesnot generate logfile... I need a separate file for logs. Please help me.
Left by Bravo on Apr 15, 2016 3:47 PM

# re: Log4Net basics with a Console Application (c#)
Requesting Gravatar...
very clean view of explain ...Thank u for posting...
Left by Rajini on Feb 24, 2017 12:21 PM

Your comment:
 (will show your gravatar)


Copyright © MarkPearl | Powered by: GeeksWithBlogs.net