Sadi02’s Weblog

June 29, 2008

Log4Net Tutorial in C# .net (How can I show log in a file?)

Filed under: Computer Science — Tags: , , , — Md. Shaik Sadi @ 7:32 am

For logging service my choice is log4net from Apache Software Foundation. It is easy to use, open source and well documented. There are also so many logging services but they are not open source. So it is an easy and best solution for you.

Write Log in Console procedures are given below-

1.       Download log4net from http://logging.apache.org/log4net/download.html

2.       Open visual studio and create a new console application.

3.       Add to the project a reference to the \bin\net\2.0\release\log4net.dll assembly in the log4net distribution.

4.       write the main method like this  

 

using System;

using System.Collections.Generic;

using System.Text;

using log4net;

using log4net.Config;

 

namespace LogPractice

{

    class Program

    {       

        static void Main(string[] args)

        {

             log4net.Config.BasicConfigurator.Configure();

             log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));

             log.Debug(“THis is sadi’s world!”);

             log.Info(“How beautyful the console looks like “);

             log.Warn(“You are great you did this”);

             log.Error(“Who make you know is the best”);

             log.Fatal(“sadi the great”);          

             Console.ReadLine();  // Hold the output

        }

    }

}

 

Using Log4net Write log in a file, Procedures are given below-

1.       Download log4net from http://logging.apache.org/log4net/download.html

2.      Open visual studio and create an application.

3.       Add to the project a reference to the \bin\net\2.0\release\log4net.dll assembly in the log4net distribution.

4.      Now put this web.config/app.config file in configuration tag.

 

<configSections>

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

      </configSections>

 

      <log4net>        

            <root>

                  <level value=DEBUG />

                  <appender-ref ref=LogFileAppender />

            </root>

            <appender name=LogFileAppender type=log4net.Appender.RollingFileAppender >

                  <param name=File value=C:\temp\log.txt />

                  <param name=AppendToFile value=true />

                  <rollingStyle value=Size />

                  <maxSizeRollBackups value=10 />

                  <maximumFileSize value=10MB />

                  <staticLogFileName value=true />

                  <layout type=log4net.Layout.PatternLayout>

                        <param name=ConversionPattern value=%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n />

                  </layout>

            </appender>

   </log4net>

this configuration creates a log file in C:\temp\log.txt.

5.       To use log4net put this as a local class variable:   protected static readonly ILog log =
LogManager.GetLogger(Program);

6.    And do this to write messages in the log file.   log.Debug(“this text will be in log file”); 

 

 

 

For Example: 

1.  using System;

2.  using System.Collections.Generic;

3.  using System.Text;

4.  using log4net;

5.  using log4net.Config;

6.   

7.  namespace LogPractice

8.  {

9.      class Program

10.    {

11.        protected static readonly ILog log = LogManager.GetLogger(typeof(Program));

12.        static void Main(string[] args)

13.        {

14.            log4net.Config.XmlConfigurator.Configure();

15. 

16.            //————————–

17.            log.Warn(“sadi the great”);

18.   

19.        }

20.    }

21.}

 

7.       Compile and run the application, and you’ll see output to the console

N.B : if you run this code log will show in the c:/temp/log.txt file. 

 

 

16 Comments »

  1. You are awesome. I read same kind of articles in as many as 7 websites.

    I felt something different when i went thru ur webpage. Ah! I was right I got the log4net working… cool

    Comment by Prabhakar Kasi — July 6, 2008 @ 2:05 am

  2. Your way of telling about how to use log 4net is really awesome…

    i visited more than 10 pages about how to use log4net but your page did the trick….

    Comment by Himanshu — August 26, 2008 @ 7:47 am

  3. There are millions of examples of how to use log4net with a file appended. Why can you not give an example of how to log with a database?

    Comment by Lee — September 1, 2008 @ 11:01 am

  4. thanks for ur comment Lee. I was little bit busy with my task.But i posted how to store log in database using log4net.

    you can view my new post
    http://sadi02.wordpress.com/2008/09/15/how-to-store-log-in-database-using-log4net/

    cheers

    Comment by Md. Shaik Sadi — September 15, 2008 @ 10:22 am

  5. Thanks, and I have a question –> My app need to write 2 log files, one for success message, one for failure message, what should I do?
    And idea?

    Comment by James Yeh — October 24, 2008 @ 6:18 am

  6. Nice work. Found this after a long search and numerous other examples that did not work.

    cheers

    Comment by Benjy — January 2, 2009 @ 10:49 pm

  7. Excellent Tutotial about log4net!

    Comment by Muhammad Asif — January 13, 2009 @ 10:30 am

  8. Good.I feel that this is easy understandable and work fine and good example.Keep it up.

    Comment by Arunkumar — June 16, 2009 @ 10:31 am

  9. I am new to Log4Net and have visited so many sites to find step by step tutorial to understand it and make it working but was disappointed. When I read your article and tries to implement LOG4NET it worked. Thanks a lot.

    Comment by Manjeet — June 18, 2009 @ 1:35 am

  10. hey it’s great……….i done with help of you websit….

    Comment by jignesh — June 23, 2009 @ 5:09 am

  11. Your article didn’t work, I got errors…

    Comment by MichaelL — July 17, 2009 @ 4:07 pm

  12. Send the error
    if I can help you in any way ..

    -Author

    Comment by Md. Shaik Sadi — July 20, 2009 @ 11:54 am

  13. Excellent article! just started using directly.
    thanks a lot.

    Vinov

    Comment by vinov — July 23, 2009 @ 2:05 pm

  14. Very simple and nice article for quick start.

    Comment by Nazish Kanwal — September 7, 2009 @ 4:35 am

  15. how can i change the file location in code? does log4net support it?

    Comment by Jack — September 15, 2009 @ 12:43 am

  16. Man..you are great…it helped me a lot…

    Comment by Foyzul Karim — September 29, 2009 @ 2:45 pm


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.