I'M using Log4Net with AdoNetAppender, and I like to flush buffer in particular time, or time period.
May be I'M not wrong, Log4Net do not provide this functionality and for that I have to create my own Appender, but I have no idea or may be I am confuse how to implement this.
Can anyone help me with this?
For flush I am suppose to use following code but it do not have time or timeout functionality.
public void FlushBuffers()
{
ILoggerRepository rep = LogManager.GetRepository();
foreach (IAppender appender in rep.GetAppenders())
{
var buffered = appender as BufferingAppenderSkeleton;
if (buffered != null)
{
buffered.Flush();
}
}
}
EDIT
Log4Net is implemented in Asp.Net website, and I need to flush this throw the site.
May be I can create Thread which execute code block in every 2 hours and flush the buffer.
public class FlushingAdoNetAppender : AdoNetAppender
{
private Timer flushTimer;
private TimeSpan flushInterval = TimeSpan.FromMinutes(5);
public FlushingAdoNetAppender()
{
// Enable for debugging purposes
// LogLog.InternalDebugging = true;
}
public TimeSpan FlushInterval
{
/*
The interval after which the buffer will be flushed. Defaults to 5 minutes
Example config:
<appender name="DatabaseAppender" type="Your.Namespace.FlushingAdoNetAppender">
<flushInterval value="00:30:00" />
</appender>
*/
get { return flushInterval; }
set { flushInterval = value; }
}
public override void ActivateOptions()
{
flushTimer = new Timer(flushInterval.TotalMilliseconds);
LogLog.Debug(GetType(), "Flush timer interval is " + TimeSpan.FromMilliseconds(flushTimer.Interval));
flushTimer.Enabled = true;
flushTimer.Elapsed += FlushLog;
flushTimer.Start();
base.ActivateOptions();
}
protected override void OnClose()
{
// This is called by log4net when reloading the config
flushTimer.Stop();
flushTimer.Dispose();
base.OnClose(); // calls Flush()
}
private void FlushLog(object sender, ElapsedEventArgs e)
{
LogLog.Debug(GetType(), "Flushing logs");
Flush();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.