Click here to Skip to main content
12,695,739 members (34,400 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads

Stats

67K views
856 downloads
60 bookmarked
Posted

log4net XmlConfigurator Simplified

, 16 Jun 2007 CPOL
This article covers the configuration of log4net using the XmlConfigurator. This article also demonstrates how to create multiple log files for your application.
MultipleLoggers
bin
Debug
log4net.dll
logs
MyCompany.MultipleLoggers.exe
MyCompany.MultipleLoggers.vshost.exe
MyCompany.MyLibrary1.dll
MyCompany.MyLibrary2.dll
Properties
MyLibrary1
bin
Debug
MyCompany.MyLibrary1.dll
Properties
MyLibrary2
bin
Debug
MyCompany.MyLibrary2.dll
Properties
log4net_objects.gif
MultipleLoggers.suo
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)

Share

About the Author

Joseph Guadagno
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.

You may also be interested in...

Pro
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 16 Jun 2007
Article Copyright 2007 by Joseph Guadagno
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid