Is there a way to programmatically flush the buffer in log4net?

I am using log4net with AdoNetAppender. It seems that AdoNetAppender has a Flush method . Anyway, can I call this from my code?

I am trying to create an admin page to view all the entries in the database log, and I will need to configure log4net with bufferSize = 100 (or more), and then I want the administrator to click a button on the admin page to force log4net to write entries to buffer log to the database (without shutting down log4net).

Is it possible?

+55
buffer flush log4net adonetappender
Jan 12
source share
2 answers

Assuming you are using log4net out of the box, you can dig your way down and hide the application this way:

public void FlushBuffers() { ILog log = LogManager.GetLogger("whatever"); var logger = log.Logger as Logger; if (logger != null) { foreach (IAppender appender in logger.Appenders) { var buffered = appender as BufferingAppenderSkeleton; if (buffered != null) { buffered.Flush(); } } } } 



Change I wrote above on the assumption that you want to drop applications for a specific ILog (probably a bad assumption now when I re-read the question), but like Stefan in the following comment, you can simplify the code a bit if you want to reset all consoles throughout the repository in the following way:

 public void FlushBuffers() { ILoggerRepository rep = LogManager.GetRepository(); foreach (IAppender appender in rep.GetAppenders()) { var buffered = appender as BufferingAppenderSkeleton; if (buffered != null) { buffered.Flush(); } } } 
+82
Jan 12
source share

Today a simpler option is available:

 LogManager.Flush(); 

Resets logging events buffered in all configured applications in the default repository. https://logging.apache.org/log4net/release/sdk/html/M_log4net_LogManager_Flush.htm

It is highly recommended that you add a timeout, such as

 LogManager.Flush(3000); 
+4
Sep 18 '18 at 9:47
source share



All Articles