I've pasted my TextBoxAppender and the applicable section of my log4net
config file below (namespaces changed to protect the innocent). I use the
DOMConfigurator.Configure() to parse the config file when starting the
application and then call TextBoxAppender.SetTextBox(), passing it a
reference to the TextBox control I want to append to. Is this what you're
looking for?
------- BEGIN CODE ----------
using System;
using System.Collections;
using System.Windows.Forms;
using log4net;
using log4net.Appender;
using log4net.Repository.Hierarchy;
namespace MyApp
{
/// <summary>
/// Implements a log4net appender to send output to a TextBox
control.
/// </summary>
public class TextBoxAppender : AppenderSkeleton
{
private System.Windows.Forms.TextBox _textBox;
public TextBoxAppender()
{
}
protected override void Append (log4net.spi.LoggingEvent
LoggingEvent)
{
_textBox.AppendText(Layout.Format(LoggingEvent));
}
public TextBox TextBox
{
set
{
_textBox = value;
}
get
{
return _textBox;
}
}
public static void SetTextBox(TextBox tb)
{
foreach(log4net.Appender.IAppender appender in
GetAppenders())
{
if (appender is TextBoxAppender)
{
((TextBoxAppender)appender).TextBox
= tb;
}
}
}
private static IAppender[] GetAppenders()
{
ArrayList appenders = new ArrayList();
appenders.AddRange(((Hierarchy)LogManager.GetLoggerRepository()).Root.Append
ers);
foreach(ILog log in LogManager.GetCurrentLoggers())
{
appenders.AddRange(((Logger)log.Logger).Appenders);
}
return
(IAppender[])appenders.ToArray(typeof(IAppender));
}
}
}
------ END CODE -----
------ BEGIN CONFIG FILE -----
<appender name="TextBoxAppender" type="MyApp.TextBoxAppender,MyApp">
<threshold value="INFO"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d
%m%n" />
</layout>
</appender>
----- END CONFIG FILE -----
-----Original Message-----
From: John Cole [mailto:[EMAIL PROTECTED]
Sent: Wednesday, October 20, 2004 2:34 PM
To: 'Log4NET User'
Subject: RE: TextBoxAppender
Neils,
I'd be interested in seeing an example app using this textbox rendere. A
few months ago, Nicko helped me with a event appender, which works, but it
sounds like yours might be simpler to use in some cases.
Thanks,
John Cole
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
Sent: Wednesday, October 20, 2004 2:28 PM
To: [email protected]
Subject: RE: TextBoxAppender
I chose the second option (as I am already using DOMConfigurator) and it
worked beautifully. I added your SetTextBox() and GetAppenders() methods to
my TextBoxAppender, so all the code has to do is call
TextBoxAppender.SetTextBox(myTextBox).
Thanks very much for your time.
-----Original Message-----
From: Nicko Cadell [mailto:[EMAIL PROTECTED]
Sent: Wednesday, October 20, 2004 2:06 PM
To: Log4NET User
Subject: RE: TextBoxAppender
Either your TextBoxAppender would have to lookup the TextBox to write to,
i.e. make the TextBox into a singleton that the appender can lookup by
referencing a static field. Or something else has to tell the appender about
the TextBox to use.
This depends on how you want to configure log4net. If you use the
BasicConfigurator you can do something like:
log4net.Config.BasicConfigurator.Configure(new
TextBoxAppender(MyTextBox));
If you want to use the DOMConfigurator to do the configuration your appender
will need to have a default constructor so that the configurator can create
it. After log4net has loaded the configuration you can find your appender
and set the TextBox to use.
public static void SetTextBox(TextBox tb)
{
foreach(log4net.Appender.IAppender appender in GetAppenders())
{
if (appender is TextBoxAppender)
{
((TextBoxAppender)appender).TextBox = tb;
}
}
}
public static log4net.Appender.IAppender[] GetAppenders()
{
ArrayList appenders = new ArrayList();
appenders.AddRange(((log4net.Repository.Hierarchy.Hierarchy)log4net.LogM
anager.GetLoggerRepository()).Root.Appenders);
foreach(log4net.ILog log in log4net.LogManager.GetCurrentLoggers())
{
appenders.AddRange(((log4net.Repository.Hierarchy.Logger)log.Logger).App
enders);
}
return
(log4net.Appender.IAppender[])appenders.ToArray(typeof(log4net.Appender.
IAppender));
}
Hope that helps,
Nicko
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED]
> Sent: 19 October 2004 19:39
> To: [email protected]
> Subject: TextBoxAppender
>
> I'd like to have log4net append messages to a TextBox control. I have
> subclassed AppenderSkeleton into a TextBoxAppender class, but don't
> know how to pass it an instance of TextBox to append to.
>
> I found this same question posted on the old sourceforge list with no
> responses, so I'm asking again. (If anyone has examples of doing
> similar things with other controls I could just work from there).
>
> Here is my simple TextBoxAppender class:
>
> public class TextBoxAppender : AppenderSkeleton
> {
>
> private System.Windows.Forms.TextBox _textBox;
>
> public TextBoxAppender(System.Windows.Forms.TextBox
> TextBox)
> {
> _textBox = TextBox;
> }
>
> protected override void Append
> (log4net.spi.LoggingEvent LoggingEvent)
> {
>
> _textBox.AppendText(LoggingEvent.RenderedMessage);
> }
>
> }
>
>
>
-------------------------------------
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 system manager.
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.