473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 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 20001
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jai | last post by:
Dear All How to create Custom Controls in ASP.Net,Can u Please Help Me out in this context.
2
by: Larry R Harrison Jr | last post by:
I have Access 97. I know how to create custom command bars. I typically create pull-down menus; seldom do I create toolbars. But I have a case where I would; in fact, I would like to have a menu...
2
by: Dennis C. Drumm | last post by:
This is a restatement of an earlier post that evidently wasn't clear. I am building a custom MessageBox dialog that has, among other things, a few new button options (yes to all, no to all, etc.)...
0
by: Shailaja Kulkarni | last post by:
Hi All, I am new to component development. I want to create custom control to arrange contained controls in form of polygonal shape. The objects are placed in separate panel on the some form....
1
by: Pablo Salazar | last post by:
Hi People. Somebody can help me. Some time ago I began to develop database project for a college. Actually control don't adjust that I need. I need to create a control from beginning ( from 0)....
2
by: Homa | last post by:
Hi all, Does any one know how to create a custom window like the control panel of PowerDVD and WinAmp in C#? If I need to use C++ Library to do this, where should I head to? Thanks for...
6
by: John Lau | last post by:
Hi, I am looking at the MS KB Article 306355: HOW TO: Create Custom Error Reporting Pages in ASP.NET by Using Visual C# .NET This article describes how to redirect errors to a custom html...
0
by: th12345 | last post by:
Hi All, I want to create custom excel sheet page in my web application like how Google spreadsheet was created. I do not want load data into Microsoft excel sheet. Please give some idea...
2
by: =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= | last post by:
Hello, I have an existing (WPF) Class Library project that I'd like to add a WPF Custom Control to. However, when I try to Add New Item, that isn't one of the available options. Perhaps this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.