Kevin,

The LoggerCreatedEvent is used to hook the creation of a Logger (ILogger
/ ILog) rather than for individual logging events. Currently there isn't
a global handler that gets fired whenever an event is logged.

The only way to catch logging events is using an appender. If the
behaviour of the memory appender is not suitable you can write your own.
You can even hook it up programmatically to the Root logger. The main
concern here is that the events only reach the appender if the loggers
are configured to allow the appropriate levels through. This may be a
problem if you wanted to take action based on some DEBUG events as in
production they are usually blocked in the config. If however you want
some feedback into your application based on some ERROR events being
logged then writing a custom appender in you assembly would be a good
way to approach it.

Nicko

> -----Original Message-----
> From: Kevin Torkelson [mailto:[EMAIL PROTECTED] 
> Sent: 13 August 2004 00:37
> To: [email protected]
> Subject: FW: MemoryAppender Example
> 
> Nick,
> 
> Thanks for the help.  After converting your code to c# (requires
> downcasting) I managed to read the events data.  The code is 
> as follows:
> 
> 
> Hierarchy hierarchy;
> MemoryAppender memoryAppender;
> 
> // Get the default hierarchy for log4net hierarchy = 
> (Hierarchy)LogManager.GetLoggerRepository();
> 
> // Get the appender named "MemoryAppender" from the <root> 
> logger memoryAppender = 
> (MemoryAppender)hierarchy.Root.GetAppender("MemoryAppender");
> 
> 
> I realized afterwards that the reason I was interested in writing the
> log data with the MemoryAppender activated was so that I could have my
> code potentially take specific actions each time a new entry was
> created.  I attempted to find an event handler.  So far I've tried (to
> no avail):
> 
> hierarchy.LoggerCreatedEvent +=new
> LoggerCreationEventHandler(hierarchy_LoggerCreatedEvent);
> 
> 
> private void hierarchy_LoggerCreatedEvent(object sender,
> LoggerCreationEventArgs e)
>       {
>               MessageBox.Show("TEST: a new event was written to the
> log");
>       }
> 
> Is there an event handler someplace that I am missing?  If 
> not what are
> my options?  I noticed that the class is sealed so I cannot subclass
> it... my only alternative would then be to write a wrapper?
> 
> Thanks Again,
> 
> Kevin
> 
> 
> -----Original Message-----
> From: Nicko Cadell [mailto:[EMAIL PROTECTED] 
> Sent: Tuesday, August 10, 2004 1:25 PM
> To: Log4NET User
> Subject: RE: MemoryAppender Example
> 
> Kevin,
> 
> To get the events out of the MemoryAppender you need to lookup the
> actual instance of the appender being written to.
> The reason that the example suggests creating and configuring the
> MemoryAppender programmatically is because you would then have a
> reference to the right MemoryAppender. If you configure the
> MemoryAppender using the config file you need to find it 
> before you can
> get the events out of it.
> 
> If you do want to configure it through the config file then 
> you need to
> know which <logger> it is attached to as that is the only way 
> of looking
> up appenders.
> 
> using log4net;
> using log4net.spi;
> using log4net.Appender;
> using log4net.Repository.Hierarchy;
> 
> ...
> 
> // Get the default hierarchy
> Hierarchy h = LogManager.GetLoggerRepository() as Hierarchy;
> 
> // Get the appender named "MemoryAppender" from the <root> logger 
> MemoryAppender ma = h.Root.GetAppender("MemoryAppender") as
> MemoryAppender;
> 
> // or get the appender from the <logger> named "my-logger"
> MemoryAppender ma =
> h.GetLogger("my-logger").GetAppender("MemoryAppender") as
> MemoryAppender;
> 
> // Get the events out of the memory appender 
> LoggingEvent[] events = ma.Events; 
> 
> 
> 
> Cheers,
> Nicko
> 
> 
> > -----Original Message-----
> > From: Kevin Torkelson [mailto:[EMAIL PROTECTED] 
> > Sent: 10 August 2004 01:29
> > To: [email protected]
> > Subject: MemoryAppender Example
> > 
> > Hi,
> > 
> >  
> > 
> > I'm trying to enable a MemoryAppender and access the logged 
> > information from within code to be displayed in a form.   
> > Ideally I'd like the MemoryAppender to be configured in the 
> > config file.  I already have a working config file which logs 
> > to a file and the debugger, and I added the following 
> > currently posted example:
> > 
> >  
> > 
> > <appender name="MemoryAppender" 
> > type="log4net.Appender.MemoryAppender" >
> > 
> >             <onlyFixPartialEventData value="true" />
> > 
> > </appender> 
> > 
> >  
> > 
> >  
> > 
> > After looking at the MemoryAppender class documentation which 
> > states the data is found inside MemoryAppender.Events, I 
> > tried code such as the following (assumes LogRtbx is defined 
> > as a rich textbox):
> > 
> >  
> > 
> > MemoryAppender memoryAppender = new MemoryAppender();
> > 
> > Log.Error("Test Error Message");
> > 
> > This.LogRtbx.AppendText(memoryAppender.Events[0].RenderedMessa
> > ge.ToString());
> > 
> >  
> > 
> > When I try this, I get an error that when handled shows that 
> > there is no data at all in Events.  It doesn't work to remove 
> > the declaration. If the MemoryAppender is being created from 
> > the config file, how can I access the MemoryAppender object 
> > or am I completely off course?
> > 
> >  
> > 
> > Recap:
> > 
> > 1)       Why does the current posted example suggest that you 
> > probably would not want to configure the MemoryAppender in 
> > the config file?
> > 
> > 2)       How can I read the logged information from memory to 
> > be displayed in a Windows form?  Are there any examples 
> > besides the config file example?
> > 
> >  
> > 
> > Thanks,
> > 
> > Kevin
> > 
> >  
> > 
> >  
> > 
> > 
> 
> 
> 

Reply via email to