472,127 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

How can I create a custom appender in Log4Net?

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

Feb 22 '07 #1
2 19814
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.MessageBoxAppend er,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(n ew
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
"forest demon" wrote:
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

Mar 13 '07 #2
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.

On Mar 13, 10:31 am, Patrick Simpe-Asante
<PatrickSimpeAsa...@discussions.microsoft.comwrote :
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.MessageBoxAppend er,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(n ew
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

"forest demon" wrote:
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- Hide quoted text -

- Show quoted text -

Mar 22 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Larry R Harrison Jr | last post: by
2 posts views Thread by Dennis C. Drumm | last post: by
1 post views Thread by Pablo Salazar | last post: by
2 posts views Thread by Homa | last post: by
6 posts views Thread by John Lau | last post: by
2 posts views Thread by =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.