4 Comments

If you want to show the log4net logging direct in your application by using a textbox, you can use the following link:

http://weblogs.asp.net/psteele/archive/2010/01/25/live-capture-of-log4net-logging.aspx

I tried it and it works great:

 

using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using log4net.Appender; using System.Threading; namespace Rvl.Demo.WindowsFormsApplication.UserControls { public partial class SettingsUserControl : UserControl, IAppender {

        private static ILog _logger = LogManager.GetLogger(typeof(SettingsUserControl));
        /// <summary>
        /// This logger property is used to log events
        /// </summary>
        public static ILog Logger
        {
            get { return _logger; }
        }

/// <summary>
///
Constructor
/// </summary>
public SettingsUserControl()
{
InitializeComponent();
}
/// <summary>
///
Show progress in outputTextBox
/// </summary>
/// <param name="loggingEvent"></param>
public void DoAppend(log4net.Core.LoggingEvent loggingEvent)
{
outPutTextBox.AppendText(loggingEvent.MessageObject.ToString() + Environment.NewLine);
}
/// <summary>
///
Close the log4net appender
/// </summary>
public void Close()
{
//throw new NotImplementedException();
}
/// <summary>
///
Configure the databases
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ConfigureDatabasesButton_Click(object sender, EventArgs e)
{
try
{
Cursor = Cursors.WaitCursor;

outPutTextBox.Clear();
outPutTextBox.AppendText("Start configuration of the databases" + Environment.NewLine);
Logger.Debug("Sp 1 execute...");
Thread.Sleep(2000);
Logger.Debug("Sp 2 execute...");
Thread.Sleep(2000);
Logger.Debug("Sp 3 execute...");
Thread.Sleep(2000);
Logger.Debug("Sp 4 execute...");
Thread.Sleep(2000);
Logger.Debug("Finished");
}
finally
{
Cursor = Cursors.Default;
}
}
}
}

 

Result

clip_image002

4 Replies to “Showing your log4net logging in your C# application”

  1. Where you call the function “DoAppend”? is associated with a function?

  2. The DoAppend method is the implementation of IAppender interface. This method is called by log4net. Of course, at log4net initialization you have to configure it to use your control as the appender.

  3. Hey, can you help me? I don’t know, how I must write the config file. Many thanks in advance!

  4. DoAppend will fire when logs are written.
    What if i close the form and open and want to see the logs that are already written?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts