logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lee Keel <lee.k...@uai.com>
Subject RE: ListView Appender
Date Wed, 18 Oct 2006 01:38:41 GMT
Here is what I have done so far.  Please let me know if anyone has a reason
that this won't work.

Config settings:
        <appender name="ListViewAppender"
type="log4net.ListViewAppender,ListViewAppender">
            <threshold value="ALL"/>
            <layout type="log4net.Layout.PatternLayout">
                <param name="ConversionPattern" value="%m%n" />
            </layout>            
        </appender>

Class:
/*
 * Created by SharpDevelop.
 * User: jcole
 * Date: 10/21/2004
 * Time: 8:16 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard
Headers.
 */

using System;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;
using log4net;
using log4net.Appender;
using log4net.Repository.Hierarchy;

namespace log4net
{
	/// <summary>
	/// Description of ListViewAppender.
	/// </summary>
	public class ListViewAppender : AppenderSkeleton
	{
		private System.Windows.Forms.ListView m_pLVW;

        public ListViewAppender()
		{
		}
		
		private delegate void
UpdateControlDelegate(log4net.Core.LoggingEvent loggingEvent);

		private void UpdateControl(log4net.Core.LoggingEvent
loggingEvent)
		{
            ListViewItem pItem = new ListViewItem();
	        
			switch(loggingEvent.Level.ToString()) {
				case "INFO":
                    pItem.ForeColor = System.Drawing.Color.Black;
					break;
				case "WARN":
                    pItem.ForeColor = System.Drawing.Color.Blue;
					break;
				case "ERROR":
                    pItem.ForeColor = System.Drawing.Color.Red;
					break;
				case "FATAL":
                    pItem.ForeColor = System.Drawing.Color.DarkOrange;
					break;
				case "DEBUG":
                    pItem.ForeColor = System.Drawing.Color.DarkGreen;
					break;
				default:
                    pItem.ForeColor = System.Drawing.Color.Black;
					break;
			}

            pItem.Text = loggingEvent.TimeStamp.ToString( "G" );
            pItem.SubItems.Add( loggingEvent.UserName );
            pItem.SubItems.Add( loggingEvent.Level.DisplayName );
            pItem.SubItems.Add( string.Format("{0}.{1}.{2}",
loggingEvent.LocationInformation.ClassName,loggingEvent.LocationInformation.
MethodName, loggingEvent.LocationInformation.LineNumber)  );
            pItem.SubItems.Add( loggingEvent.RenderedMessage );
            m_pLVW.Items.Add( pItem );
            pItem.EnsureVisible();
        }

        

		protected override void Append (log4net.Core.LoggingEvent
LoggingEvent)
		{
			// prevent exceptions
			if (m_pLVW != null)
			{
				// make thread safe
                if ( m_pLVW.InvokeRequired )
				{
                    m_pLVW.Invoke(
						new
UpdateControlDelegate(UpdateControl),
						new object[]
{LoggingEvent});
				} 
                else 
                {
					UpdateControl(LoggingEvent);
				}
			}
		}


        public ListView ListView
		{
			set 
			{
                m_pLVW = value;
			}
			get
			{
                return m_pLVW;
			}
		}

        public static void SetListView( ListView lvw )
		{
            lvw.HideSelection = false;
            lvw.Items.Clear();

			foreach(log4net.Appender.IAppender appender in
GetAppenders())
			{
				if (appender is ListViewAppender)
				{
                    ((ListViewAppender) appender).ListView = lvw;
				}
			}
		}

		private static IAppender[] GetAppenders()
		{
			ArrayList appenders = new ArrayList();

	
	
appenders.AddRange(((Hierarchy)LogManager.GetRepository()).Root.Appenders);

			foreach(ILog log in LogManager.GetCurrentLoggers())
			{
	
appenders.AddRange(((Logger)log.Logger).Appenders);
			}

			return
(IAppender[])appenders.ToArray(typeof(IAppender));
		}

	}
}

-----Original Message-----
From: Ron Grabowski [mailto:rongrabowski@yahoo.com]
Sent: Tuesday, October 17, 2006 6:24 AM
To: Log4NET User
Subject: Re: ListView Appender


The code should be similiar to a RichTextBox. You may have to think about
limiting the ListView to display only the last X log messages so you don't
use up all the memory (maybe use CyclicBuffer?). I remember there being a
few log4net viewers out there. You may want to see if any of those programs
have special code that uses log4net in conjunction with a ListView control.

----- Original Message ----
From: Lee Keel <lee.keel@uai.com>
To: log4net-user@logging.apache.org
Sent: Monday, October 16, 2006 2:41:09 PM
Subject: ListView Appender

Hello all!

I am trying to find out if anyone has written an appender for a ListView or
if there is some reason that I shouldn't do so.  I currently have an
appender for a RichTextBox that I found on the net, so it seemed a fairly
logical progression to go with a ListView.  But I didn't know someone had
done this already and found some major pitfall that I hadn't encountered
yet.

Thanks,
Lee Keel
This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the sender. This
message contains confidential information and is intended only for the
individual named. If you are not the named addressee you should not
disseminate, distribute or copy this e-mail.



Mime
View raw message