Log4Net.Async icon indicating copy to clipboard operation
Log4Net.Async copied to clipboard

Add support for buffer count / inspection

Open szalkerous opened this issue 7 years ago • 2 comments

I have run into a situation where one of my appender's target data is being accessed before the buffer is emptied resulting in a loss of logging data.

It would be very handy to have a way to ask for a buffer/queue count, or a boolean flag to determine if the buffer/queue is empty.

This way I could block certain areas of my application until the logging queue is empty before trying to access that log data.

I realize there are ways around this, such as destroying the AsyncForwardingAppender and recreating it, but that seems to be a drastic approach to a simple problem.

My only other alternative is to fork the Log4net.Async project and modify it for my situation, but then it becomes orphaned code that I must maintain manually.

Thanks for your consideration.

szalkerous avatar Feb 24 '18 20:02 szalkerous

If it's of any help, what I did for now was do a local fork and make the following changes:

IQueue.cs -- add int Count(); RingBuffer.cs -- add public int Count() { AsyncForwardingAppenderBase.cs - add public abstract int BufferCount { get; } AsyncForwardingAppender.cs - add

public override int BufferCount
{
   get { return buffer.Count(); }
}

and

public override bool Flush(int millisecondsTimeout)
{
	DateTime starttime = DateTime.Now;

	while (buffer.Count() != 0)
	{
		Thread.Sleep(10);
		
		if (shutDownRequested)
		{
			return false;
		}

		if (DateTime.Now.Subtract(starttime).TotalMilliseconds >= millisecondsTimeout)
		{
			return false;
		}
	}

	return true;
}

Not sure if there's a better way to do this, but I needed something for now.

szalkerous avatar Feb 24 '18 21:02 szalkerous

If you want lossless, why not use the ParallelForwardingAppender? Aside from that, what not create a pull request and a test for the code that you've already written?

rcollette avatar Feb 25 '18 01:02 rcollette