469,306 Members | 2,504 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

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 19598
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 zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.