How can I create a custom appender in Log4Net?

  • Thread starter Thread starter forest demon
  • Start date Start date
F

forest demon

I need to be able to create an appender to direct output to a
MessageBox.

The API does not have a MessageBox appender like nLog does. What
would be the easiest way to do this. The examples supplied with the
source are anything but helpful.

Thanks for any input.

-fd
 
Hi forest demon,

It's fairly straightforward to create a messagebox appender, which is
probably why you haven't found a prefab one... The example below assumes
that you're putting the appender in a class library of it's own, and the
client code that will use the new appender is in a Windows form.

Basically, what you need to do is:

(a) create a new custom class library
(a) add log4net & System.Windows.Forms as new references to the new library
(b) inherit a new/custom appender class from log4net.Appender.AppenderSkeleton
(c) override the Append event handler from the skeleton class, and in it
show the RenderedMessage, something like this:
using System;
using log4net;
using System.Windows.Forms;

namespace SomeCompany.Logger.Appender
{
public class MessageBoxAppender : log4net.Appender.AppenderSkeleton
{
protected override void Append(log4net.spi.LoggingEvent log)
{
// Clearly, you can check out other properties on the log event
object to see what else you might want to display
MessageBox.Show("Message Sent: " + log.RenderedMessage);
}
}
}

(e) in your win forms app, create a new config file and call it something
like Log.config, and set it up so that log4net knows where/how to instantiate
your new appender):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>

<appender name="MessageBoxAppender"
type="SomeCompany.Logger.Appender.MessageBoxAppender,MessageBoxAppender">
<threshold value="DEBUG"/>
</appender>

<root>
<level value="DEBUG" />
<appender-ref ref="MessageBoxAppender" />
</root>

</log4net>
</configuration>

(f) In the c'tor for your win form (the code that will use the logger), set
up log4net to locate your Log.config file:
// Set up the logger, and configure - you'll need to change this
to point to wherever you end up putting the Log.config file
log4net.Config.DOMConfigurator.ConfigureAndWatch(new
FileInfo(@"C:\temp\MessageBoxLogger\Log.config"));
log4net.Config.BasicConfigurator.Configure();


(g) In your win form app, declare a logger class member, for example:
protected static readonly ILog logger =
LogManager.GetLogger(typeof(Form1));

(h) Now, anywhere you want to in your win forms app wherever you want to log
to the MessageBox, simply call:
logger.Debug("Hello world")

and you should find the message box popping up...

I have a working sample of exactly the above that I can mail you if you post
an email addr....

Hope this helps...

Kind regards
Patrick
 
hey patrick, thanks for the reply. this seems straight forward, but i
was unable to get it to work. i had to make a few mods to fit my
app(naming wise, etc), but to no avail. i tried both a separate config
file and also my app.conf. i also had to change
log4net.spi.LoggingEvent, to use the Core namespace of Log4Net.
 
Back
Top