473,734 Members | 2,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 20037
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.Appende r.AppenderSkele ton
(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.Log ger.Appender
{
public class MessageBoxAppen der : log4net.Appende r.AppenderSkele ton
{
protected override void Append(log4net. spi.LoggingEven t 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.RenderedMes sage);
}
}
}

(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" ?>
<configuratio n>
<log4net>

<appender name="MessageBo xAppender"
type="SomeCompa ny.Logger.Appen der.MessageBoxA ppender,Message BoxAppender">
<threshold value="DEBUG"/>
</appender>

<root>
<level value="DEBUG" />
<appender-ref ref="MessageBox Appender" />
</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 .ConfigureAndWa tch(new
FileInfo(@"C:\t emp\MessageBoxL ogger\Log.confi g"));
log4net.Config. BasicConfigurat or.Configure();
(g) In your win form app, declare a logger class member, for example:
protected static readonly ILog logger =
LogManager.GetL ogger(typeof(Fo rm1));

(h) Now, anywhere you want to in your win forms app wherever you want to log
to the MessageBox, simply call:
logger.Debug("H ello 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.Log gingEvent, to use the Core namespace of Log4Net.

On Mar 13, 10:31 am, Patrick Simpe-Asante
<PatrickSimpeAs a...@discussion s.microsoft.com wrote:
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.Appende r.AppenderSkele ton
(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.Log ger.Appender
{
public class MessageBoxAppen der : log4net.Appende r.AppenderSkele ton
{
protected override void Append(log4net. spi.LoggingEven t 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.RenderedMes sage);
}
}

}

(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" ?>
<configuratio n>
<log4net>

<appender name="MessageBo xAppender"
type="SomeCompa ny.Logger.Appen der.MessageBoxA ppender,Message BoxAppender">
<threshold value="DEBUG"/>
</appender>

<root>
<level value="DEBUG" />
<appender-ref ref="MessageBox Appender" />
</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 .ConfigureAndWa tch(new
FileInfo(@"C:\t emp\MessageBoxL ogger\Log.confi g"));
log4net.Config. BasicConfigurat or.Configure();

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

(h) Now, anywhere you want to in your win forms app wherever you want to log
to the MessageBox, simply call:
logger.Debug("H ello 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
1240
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
6528
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 bar AND a toolbar, BOTH types of command bars for this form. But in the PROPERTIES of this form it doesn't allow but one at the time apparently. Anyway around this?
2
30160
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.) and would like to make the dialog look and act like the standard MessageBox. Therefore, I would like to put in the message section the same type of icons found in the MessageBox dialog, such as MessageBoxIcon.Exclamation. In another post here I had...
0
1633
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. At one place collection of controls representing objects is present. User should be able to click on respective control that time cursor should change. And whenever mouse comes in this control area locations where control can be placed should get...
1
1468
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). can you tell me where can i find information about create custom control? Thanks. Pablo Salazar ( sorry by my English).
2
9417
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 Concern, Homa Wong
6
3562
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 error page. All six steps in the article work just fine. Then at the end of the article, there is a little comment about redirecting errors to an aspx page (instead of an html page):
0
1258
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 on this, how do start my work . Either create client side table using javascript or Server side table using Gridview or Server Table. If anyone have same experience on this, Please suggest me how do approach.
2
5223
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 is a brain freeze, but what could I possibly be missing? WtS
0
8776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6735
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.