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