I have attached a simple example appender that raises an event for each
LoggingEvent that it receives. You can include this appender in your
application or in a separate assembly. It is simple to configure:

<appender name="FireEventAppender" 
          type="TestConsoleApp.FireEventAppender,TestConsoleApp" />

Update the type attribute to be the assembly qualified type name for
wherever you put the appender.

To hook the event handler up in your code you need to write a method to
receive the event:

private void FireEventAppender_MessageLoggedEventHandler(
    object sender, MessageLoggedEventArgs e)
{
  // Do something with the event
  System.Console.WriteLine("****" + e.LoggingEvent.RenderedMessage +
"****");
}

And then hook up the event like so:

if (FireEventAppender.Instance != null)
{
  FireEventAppender.Instance.MessageLoggedEvent += 
      new MessageLoggedEventHandler(
          FireEventAppender_MessageLoggedEventHandler);
}

Now the only tricky bit is because of the way FireEventAppender.Instance
is written up you cannot wire up the event handler until after the
configuration file has been loaded. You just need to call
LogManager.GetLogger() before using FireEventAppender.Instance. For
example if you declare the following in your main class:

private static readonly log4net.ILog log =
log4net.LogManager.GetLogger(typeof(MyClass));

Then the config file will be read before your Main() method is executed.


Some other things to remember. When you handle the event you are being
called directly from the Append method of the appender on the thread
that is logging. So if you are going to do something that is slow (e.g.
updating a UI) you may want to schedule an asynchronous operation (see
System.Threading.ThreadPool.QueueUserWorkItem). Also if you are going to
try to update a UI you may be on the wrong thread, you need to use
Control.Invoke() to transfer control to the thread that owns the window
handle. 

Hope that is helpful,

Nicko

> -----Original Message-----
> From: John Cole [mailto:[EMAIL PROTECTED] 
> Sent: 23 August 2004 20:31
> To: 'Log4NET User'
> Subject: RE: logging to a textbox...
> 
> Kevin,
>   I think that approach will work nicely, even if we use a 
> custom appender derived from the MemoryAppender.
> 
>   I wish I was to the point these guys who are writing 
> log4net and NAnt, but I'm going to need a few more months on 
> .Net before I'm at that level :-) Right now, I know what I 
> want to do with the platform, I just don't know how to do it 
> yet, so I'm trying to take things like this apart and learn 
> from it.  Getting a custom appender working sounds like a fun 
> and useful exercise.  I just need a good starting place.
> 
> Thanks,
> 
> John Cole
> 
> 
> 
> -----Original Message-----
> From: Kevin Torkelson [mailto:[EMAIL PROTECTED]
> Sent: Monday, August 23, 2004 2:00 PM
> To: log4net-user@logging.apache.org
> Subject: FW: logging to a textbox...
> 
> 
> John,
> 
> It looks like you are trying to do something very similar to 
> what I was interested in (see my earlier messages in the 
> list.)  Basically what I wound up with was a Windows form 
> with a textbox and a refresh button that whenever it was 
> clicked, it could show the contents of the MemoryAppender.  
> Where I left off with Nicko was that I was searching for an 
> event handler that would fire each new time that data was 
> written to the events collection.  Maybe instead of making a 
> new customized Appender, we can somehow figure out how to 
> give that MemoryAppender that event handler?
> 
> I haven't really tried to check out the source much, but I'm 
> game to try just about anything at least once :D
> 
> Kevin
> 
> 
> -----Original Message-----
> From: John Cole [mailto:[EMAIL PROTECTED]
> Sent: Monday, August 23, 2004 11:55 AM
> To: 'Log4NET User'
> Subject: RE: logging to a textbox...
> 
> Nicko,
>   Thanks for the reply, modifying one of those appenders 
> doesn't appear too bad :-)  Is there an example or some 
> documentation on how to get a custom appender up and running? 
>  I've searched the mail archives and google, along with the 
> docs in log4net and didn't see anything.
> 
> Thanks,
> 
> John Cole
> 
> 
> -----Original Message-----
> From: Nicko Cadell [mailto:[EMAIL PROTECTED]
> Sent: Sunday, August 22, 2004 11:10 AM
> To: Log4NET User
> Subject: RE: logging to a textbox...
> 
> 
> John,
> 
> To log into a text area in your application you will need to 
> write a custom appender in your app. You can base this 
> appender on either the TraceAppender (because it is very 
> simple) or the MemoryAppender (because it will buffer the 
> events for you). You can still configure log4net through the 
> config file, just specify the assembly qualified type name 
> for your appender. In order to simplify hooking the appender 
> up to the UI I suggest that you make it a singleton, i.e. 
> have a static property on the appender to lookup the single instance.
> 
> Then either you make the appender write directly to the text 
> area. Or you make the appender fire an event and then the 
> text area can query for the events.
> 
> Cheers,
> Nicko 
> 
> > -----Original Message-----
> > From: John Cole [mailto:[EMAIL PROTECTED]
> > Sent: 20 August 2004 23:11
> > To: 'Log4NET User'
> > Subject: RE: logging to a textbox...
> > 
> > Denis,
> >   Here is an example:
> >  
> >   I have an application that is using log4net and has a main window 
> > with a text area used to display the log.  This application 
> is using a 
> > dll that is also using log4net for logging, to the same file as the 
> > application.
> >  
> >   I would like the dll and the app to log both to the 
> textfile and for 
> > the logging to appear in the text are on the main form, but I don't 
> > want to make the dll dependent on having to log to the text 
> area (my 
> > next app might not have one).
> >  
> >   There may be an obvious .net way to do this, but I'm 
> still taking a 
> > lot of code apart and learning how the neat things work :-)
> >  
> >   Thanks for your quick reply.
> >  
> > John Cole
> > 
> >     -----Original Message-----
> >     From: Denis, Rich [mailto:[EMAIL PROTECTED]
> >     Sent: Friday, August 20, 2004 3:32 PM
> >     To: Log4NET User
> >     Subject: RE: logging to a textbox...
> >     
> >     
> > 
> >     I am not sure I can figure out what you are doing.  Is 
> the textbox 
> > part of the actual application that is logging or is it another 
> > application that is just reading the log file?
> > If it is the first one then once you log a line then you just also 
> > write it to the text box.  If it is the second situation where the 
> > textbox is in a windows application that is just reading 
> the log, well 
> > then you are on your own, reading the file and printing out 
> the lines 
> > is one way.  I would think that since the app reading from 
> the file is 
> > not of the same app domain as the app that is logging, then 
> you will 
> > not be able to hook into the any type of events offered by log4net.
> > 
> >      
> > 
> >     Hope this helps.
> > 
> >      
> > 
> >     Rich Denis
> >     perotsystems(tm) 
> > 
> >     
> > ________________________________
> > 
> > 
> >     From: John Cole [mailto:[EMAIL PROTECTED] 
> >     Sent: Friday, August 20, 2004 2:38 PM
> >     To: 'log4net-user@logging.apache.org'
> >     Subject: logging to a textbox...
> > 
> >      
> > 
> >     Hello,
> > 
> >       I've been able to get simple logging (console and 
> > text file) working, but I would like to have a GUI 
> > application log to a file and a textbox.
> > 
> >      
> > 
> >       Logging to the file is no problem, but I'm a little 
> > confused as to which appender I should use and how I should use it.
> > 
> >      
> > 
> >       I've thought about putting a file watcher on the log 
> > file, but that seems a bit inelegant.  I'd like to have an 
> > event fired from the logger and handle it on my form with the 
> > textbox (or better yet, the RichTextBox).
> > 
> >      
> > 
> >       What is the best way to accomplish logging to a text 
> > box and a text file?
> > 
> >      
> > 
> >     Thanks,
> > 
> >      
> > 
> >     John Cole
> > 
> >     
> >     -------------------------------------
> >     This email and any files transmitted with it are 
> > confidential and intended solely for the use of the 
> > individual or entity to whom they are addressed. If you have 
> > received this email in error please notify the system 
> > manager. This message contains confidential information and 
> > is intended only for the individual named. If you are not the 
> > named addressee you should not disseminate, distribute or 
> > copy this e-mail.
> > 
> > 
> > -------------------------------------
> > This email and any files transmitted with it are confidential 
> > and intended solely for the use of the individual or entity 
> > to whom they are addressed. If you have received this email 
> > in error please notify the system manager. This message 
> > contains confidential information and is intended only for 
> > the individual named. If you are not the named addressee you 
> > should not disseminate, distribute or copy this e-mail.
> > 
> 
> -------------------------------------
> This email and any files transmitted with it are confidential and
> intended solely for the use of the individual or entity to 
> whom they are
> addressed. If you have received this email in error please notify the
> system manager. This message contains confidential information and is
> intended only for the individual named. If you are not the named
> addressee you should not disseminate, distribute or copy this e-mail.
> 
> 
> -------------------------------------
> This email and any files transmitted with it are confidential 
> and intended solely for the use of the individual or entity 
> to whom they are addressed. If you have received this email 
> in error please notify the system manager. This message 
> contains confidential information and is intended only for 
> the individual named. If you are not the named addressee you 
> should not disseminate, distribute or copy this e-mail.
> 

Attachment: FireEventAppender.cs
Description: FireEventAppender.cs

Reply via email to