cjbhaines / Log4Net.Async

Asynchronous Log4Net appenders and forwarder
http://www.nuget.org/packages/Log4Net.Async/
MIT License
121 stars 37 forks source link

Add support for buffer count / inspection #41

Open szalkerous opened 6 years ago

szalkerous commented 6 years ago

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 commented 6 years ago

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.

rcollette commented 6 years ago

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?