public void TestSetup()
        {
            eventsWriter = Substitute.For <IEventsWriter>();

            eventsWriterProvider = Substitute.For <IEventsWriterProvider>();
            eventsWriterProvider.ObtainWriterAsync(Arg.Any <CancellationToken>()).Returns(Task.FromResult(eventsWriter));

            writerProviderFactory = Substitute.For <IEventsWriterProviderFactory>();
            writerProviderFactory.CreateProvider(Arg.Any <FilePath>(), Arg.Any <Func <FileLogSettings> >()).Returns(eventsWriterProvider);

            muxer = new SynchronousSingleFileMuxer(writerProviderFactory, new FileLogSettings());
        }
예제 #2
0
        public void TestSetup()
        {
            writer = Substitute.For <IEventsWriter>();

            writerProvider = Substitute.For <IEventsWriterProvider>();
            writerProvider.ObtainWriterAsync(Arg.Any <CancellationToken>()).Returns(writer);

            worker = new SingleFileWorker();

            events     = new ConcurrentBoundedQueue <LogEventInfo>(2);
            buffer     = new LogEventInfo[1];
            eventsLost = new AtomicLong(0);
        }
        public bool TryAdd(LogEventInfo info, bool fromOwner)
        {
            if (fromOwner)
            {
                settings = info.Settings;
            }

            try
            {
                if (writerProvider == null)
                {
                    return(false);
                }

                lock (sync)
                {
                    var writer = writerProvider?.ObtainWriterAsync(workerCancellation.Token).GetAwaiter().GetResult();

                    if (writer == null)
                    {
                        return(false);
                    }

                    buffer[0] = info;

                    writer.WriteEvents(buffer, 1);
                }

                return(true);
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception error)
            {
                SafeConsole.ReportError($"Failure in writing log event to file '{settings.FilePath}':", error);
            }

            return(false);
        }
예제 #4
0
        public void TestSetup()
        {
            eventsWriter = Substitute.For <IEventsWriter>();

            eventsWriterProvider = Substitute.For <IEventsWriterProvider>();
            eventsWriterProvider.ObtainWriterAsync(Arg.Any <CancellationToken>()).Returns(Task.FromResult(eventsWriter));

            writerProviderFactory = Substitute.For <IEventsWriterProviderFactory>();
            writerProviderFactory.CreateProvider(Arg.Any <FilePath>(), Arg.Do <Func <FileLogSettings> >(x => settingsInsideMuxer = x)).Returns(eventsWriterProvider);

            singleFileWorker = Substitute.For <ISingleFileWorker>();
            singleFileWorker.WritePendingEventsAsync(
                Arg.Any <IEventsWriterProvider>(),
                Arg.Any <ConcurrentBoundedQueue <LogEventInfo> >(),
                Arg.Any <LogEventInfo[]>(),
                Arg.Any <AtomicLong>(),
                Arg.Any <AtomicLong>(),
                Arg.Any <CancellationToken>())
            .Returns(Task.FromResult(true));

            muxer = new SingleFileMuxer(writerProviderFactory, singleFileWorker, new FileLogSettings());
        }
예제 #5
0
        public async Task <bool> WritePendingEventsAsync(
            IEventsWriterProvider writerProvider,
            ConcurrentBoundedQueue <LogEventInfo> queue,
            LogEventInfo[] buffer,
            AtomicLong eventsLostCurrently,
            AtomicLong eventsLostSinceLastIteration,
            CancellationToken cancellation)
        {
            var eventsToDrain = queue.Count;

            if (eventsToDrain == 0)
            {
                return(true);
            }

            var writer = await writerProvider.ObtainWriterAsync(cancellation).ConfigureAwait(false);

            if (writer == null)
            {
                return(false);
            }

            while (eventsToDrain > 0)
            {
                var eventsDrained = queue.Drain(buffer, 0, buffer.Length);
                if (eventsDrained == 0)
                {
                    break;
                }

                eventsToDrain -= eventsDrained;

                try
                {
                    writer.WriteEvents(buffer, eventsDrained);
                }
                catch (Exception error)
                {
                    eventsLostCurrently.Add(eventsDrained);

                    SafeConsole.ReportError("Failure in writing log events to a file:", error);

                    break;
                }
                finally
                {
                    Array.Clear(buffer, 0, eventsDrained);
                }
            }

            var lostEventsAfterWriting = eventsLostCurrently.Value;

            if (lostEventsAfterWriting > eventsLostSinceLastIteration)
            {
                buffer[0] = CreateOverflowEvent(queue, lostEventsAfterWriting - eventsLostSinceLastIteration);

                try
                {
                    writer.WriteEvents(buffer, 1);
                }
                catch
                {
                    // ignored
                }

                eventsLostSinceLastIteration.Value = lostEventsAfterWriting;

                return(false);
            }

            return(true);
        }