Click here to Skip to main content
15,888,286 members
Articles / Programming Languages / Visual Basic

log4net XmlConfigurator Simplified

Rate me:
Please Sign up or sign in to vote.
4.71/5 (16 votes)
16 Jun 2007CPOL5 min read 95.1K   990   60  
This article covers the configuration of log4net using the XmlConfigurator. This article also demonstrates how to create multiple log files for your application.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO;
using MyCompany;

namespace LoggingApplication
{
    public partial class Form1 : Form
    {
        log4net.ILog m_Log;
        private string logPath;
        
        public Form1()
        {
            InitializeComponent();

            LoadConfiguration();

        }

        #region Button Events
        private void btnTest_Click(object sender, EventArgs e)
        {

            errorText.Text = "";

            m_Log.Info("Generating test messages.");
            MyCompany.MultipleClasses.Class1 class1 = new MyCompany.MultipleClasses.Class1();
            MyCompany.MultipleClasses.Class2 class2 = new MyCompany.MultipleClasses.Class2();
            MyCompany.MultipleClasses.Class3 class3 = new MyCompany.MultipleClasses.Class3();
            MyCompany.Library1.MyFirstClass lib1_class1 = new MyCompany.Library1.MyFirstClass();
            MyCompany.Library2.MySecondClass lib2_class1 = new MyCompany.Library2.MySecondClass();
            MyCompany.Library2.MyThirdClass lib2_class3 = new MyCompany.Library2.MyThirdClass();

            for (int x = 0; x < 10; x++)
            {
                class1.LogMessages(string.Format("Message #{0}", x));
                class2.LogMessages(string.Format("Message #{0}", x));
                class3.LogMessages(string.Format("Message #{0}", x));
                lib1_class1.LogMessages(string.Format("Message #{0}", x));
                lib2_class1.LogMessages(string.Format("Message #{0}", x));
                lib2_class3.LogMessages(string.Format("Message #{0}", x));
            }

            class1 = null;
            class2 = null;
            class3 = null;
            lib1_class1 = null;
            lib2_class1 = null;
            lib2_class3 = null;
        }

        private void btnClean_Click(object sender, EventArgs e)
        {
            errorText.Text = "";
            m_Log.Info("Attempting to remove the log files from the directory");
            try
            {
                foreach (string file in Directory.GetFiles(logPath, "*.log"))
                {
                    File.Delete(file);
                }
            }
            catch (Exception ex)
            {
                errorText.Text = ex.ToString();
                m_Log.Error("An exception occurred while deleting the file.", ex);
            }

        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            errorText.Text = "";
            m_Log.Info("Opening the files in the default editor.");
            try
            {
                foreach (string file in Directory.GetFiles(logPath, "*.log"))
                {
                    System.Diagnostics.Process.Start(file);
                }
            }
            catch (Exception ex)
            {
                errorText.Text = ex.ToString();
                m_Log.Error("An exception occurred while showing the file.", ex);
            }
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            errorText.Text = "";
            m_Log.Info("Opening the log folder");
            System.Diagnostics.Process.Start(logPath);
        }


        private void btnWarn_Click(object sender, EventArgs e)
        {
            m_Log.Warn("This is a sample warning message");
        }  

        #endregion Button Events

        private void LoadConfiguration()
        {

            logPath = Path.Combine(Directory.GetCurrentDirectory(), "logs");

            m_Log = log4net.LogManager.GetLogger(this.GetType().FullName);
            string path = Directory.GetCurrentDirectory();
            string filename = Path.Combine(path, "log4net.config");
            FileInfo file = new FileInfo(filename);

            log4net.Config.XmlConfigurator.Configure(file);
            m_Log.InfoFormat("Application Configuration Completed at {0}", DateTime.Now);
            m_Log.InfoFormat("The logs will be placed in '{0}'.", logPath);

            file = null;

        }
          

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Web Developer
United States United States
I have been in software development for about 15 years or so. I started out with a small book on QuickBASIC, then moved the Visual Basic for DOS, then Visual Basic for Windows, then Visual Basic .NET and eventually Visual C#. When I am not working at my full time job I donate my time to several community efforts like:

Former President of INETA North America, currently Vice President.
President of the Southeast Valley .NET User Group (SEVDNUG) in Chandler, AZ.
Serving on my City's Parks and Recreation board.

I have or have had the following "MVP" awards:

  • Visual Basic MVP in 1996
  • C# MVP since 2009
  • Telerik MVP since 2010

I maintain a Open Source project on CodePlex which wraps the Bing API called BingSharp.

I also help / organize or participate in several community events:

  • Desert Code Camp
  • AZGiveCamp
  • Organizer for the 1st Time MVP event at the MVP Summit
  • MVP 2 MVP Sessions at MVP Summit.
  • Awesome bean pusher at GeekGive at the MVP Summit.

Comments and Discussions