Beispiel #1
0
        public SingleFileMuxer(
            [NotNull] IEventsWriterProviderFactory writerProviderFactory,
            [NotNull] ISingleFileWorker worker,
            [NotNull] FileLogSettings settings)
        {
            this.settings = settings;
            this.worker   = worker;

            writerProvider = writerProviderFactory.CreateProvider(settings.FilePath, () => this.settings);

            eventsQueue = new Lazy <ConcurrentBoundedQueue <LogEventInfo> >(
                () => new ConcurrentBoundedQueue <LogEventInfo>(settings.EventsQueueCapacity),
                LazyThreadSafetyMode.ExecutionAndPublication);

            eventsBuffer = new Lazy <LogEventInfo[]>(
                () => new LogEventInfo[settings.EventsBufferCapacity],
                LazyThreadSafetyMode.ExecutionAndPublication);

            eventsLostCurrently          = new AtomicLong(0);
            eventsLostSinceLastIteration = new AtomicLong(0);

            flushSignal  = new AsyncManualResetEvent(true);
            flushWaiters = new List <Waiter>();

            workerInitLock           = new object();
            workerCancellationWaiter = new Waiter(TaskCreationOptions.RunContinuationsAsynchronously);
            workerCancellation       = new CancellationTokenSource();
            workerCancellation.Token.Register(() => workerCancellationWaiter.TrySetResult(true));
        }
        public void Should_write_to_new_file_after_settings_update(bool cache)
        {
            var firstLog  = Folder.GetFileName("firstLog");
            var secondLog = Folder.GetFileName("secondLog");

            var firstLogMessages  = GenerateMessages(0, 3);
            var secondLogMessages = GenerateMessages(3, 6);

            var currentSettings = new FileLogSettings {
                FilePath = firstLog, EnableFileLogSettingsCache = cache
            };

            using (var log = new FileLog(() => currentSettings))
            {
                WriteMessagesWithTimeout(log, firstLogMessages, 0);

                currentSettings = new FileLogSettings {
                    FilePath = secondLog
                };
                FileLog.RefreshAllSettings();

                WriteMessagesWithTimeout(log, secondLogMessages, 0);
            }

            ShouldContainMessages(firstLog, firstLogMessages);
            ShouldContainMessages(secondLog, secondLogMessages);
        }
Beispiel #3
0
        public IMuxerRegistration Register(FilePath file, FileLogSettings settings, WeakReference initiator)
        {
            while (true)
            {
                if (states.TryGetValue(file, out var currentState))
                {
                    if (states.TryUpdate(file, currentState.AddParticipant(initiator), currentState))
                    {
                        break;
                    }
                }
                else
                {
                    var initialState = FileState.CreateInitial(factory.Create(settings), initiator);

                    if (states.TryAdd(file, initialState))
                    {
                        break;
                    }
                }
            }

            var registration = new MuxerRegistration(states, registrations, file, initiator);

            registrations.TryAdd(registration, registration);

            return(registration);
        }
        static void Main(string[] args)
        {
            var configuration = new ContainerConfiguration(typeof(CassandraFileSystem).Assembly);
            var container     = new Container(configuration);

            var settings = new FileLogSettings();
            var logger   = new CompositeLog(new ConsoleLog(), new FileLog(settings));

            container.Configurator.ForAbstraction <ILog>().UseInstances(logger);

            var config = JsonConvert.DeserializeObject <Config>(File.ReadAllText("config.json"));

            if (!config.DefaultTTL.HasValue || config.DefaultTTL.Value == 0)
            {
                config.DefaultTTL = new TimeSpan(14, 0, 0, 0).Seconds;
            }
            config.DefaultDataBufferSize ??= 4194304;
            container.Configurator.ForAbstraction <Config>().UseInstances(config);

            CassandraConfigurator.ConfigureCassandra(container, logger);

            logger.Info("Creating filesystem...");

            var constructor = container.GetCreationFunc <string[], CassandraFileSystem>();

            using var fs = constructor(args);
            logger.Info("Starting filesystem");
            fs.Start();
        }
Beispiel #5
0
        public bool TryAdd(LogEventInfo info, bool fromOwner)
        {
            if (fromOwner)
            {
                settings = info.Settings;
            }

            InitializeIfNeeded();

            if (settings.WaitIfQueueIsFull)
            {
                while (eventsQueue?.Value.TryAdd(info) == false)
                {
                    Thread.Sleep(100);
                }
                return(true);
            }

            if (eventsQueue.Value.TryAdd(info))
            {
                return(true);
            }

            eventsLostCurrently.Increment();

            return(false);
        }
        public void Should_respect_writer_format_setting_updates(bool cache)
        {
            var logFile = Folder.GetFileName("firstLog");

            var firstMessages  = GenerateMessages(0, 3);
            var secondMessages = GenerateMessages(3, 6);

            var currentSettings = new FileLogSettings {
                FilePath = logFile, OutputTemplate = OutputTemplate.Default, EnableFileLogSettingsCache = cache
            };

            using (var log = new FileLog(() => currentSettings))
            {
                WriteMessagesWithTimeout(log, firstMessages, 0);

                log.Flush();

                currentSettings = new FileLogSettings {
                    FilePath = logFile, OutputTemplate = OutputTemplate.Empty, EnableFileLogSettingsCache = cache
                };
                FileLog.RefreshAllSettings();

                WriteMessagesWithTimeout(log, secondMessages, 0);
            }

            ShouldContainMessages(logFile, firstMessages);
            ShouldNotContainMessages(logFile, secondMessages);
        }
Beispiel #7
0
        public void ObtainWriterAsync_should_perform_actions_in_order()
        {
            ObtainWriter();

            settings = new FileLogSettings {
                OutputBufferSize = 42
            };
            ObtainWriter();

            Received.InOrder(
                () =>
            {
                strategyProvider.ObtainStrategy();
                strategy.GetCurrentFile(Arg.Any <FilePath>());
                eventsWriterFactory.TryCreateWriter(Arg.Any <FilePath>(), Arg.Any <FileLogSettings>());
                garbageCollector.RemoveStaleFiles(Arg.Any <FilePath[]>());
                cooldownController.IncurCooldown(Arg.Any <TimeSpan>(), Arg.Any <CancellationToken>());

                strategyProvider.ObtainStrategy();
                strategy.GetCurrentFile(Arg.Any <FilePath>());
                eventsWriter.Dispose();
                eventsWriterFactory.TryCreateWriter(Arg.Any <FilePath>(), Arg.Any <FileLogSettings>());
                garbageCollector.RemoveStaleFiles(Arg.Any <FilePath[]>());
                cooldownController.IncurCooldown(Arg.Any <TimeSpan>(), Arg.Any <CancellationToken>());
            });
        }
Beispiel #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FileLog" /> class.
        /// </summary>
        /// <param name="filename">A string that specifies the log file name.</param>
        /// <param name="level">A <see cref="LogLevel" /> enumeration value that specifies log verbosity.</param>
        /// <param name="layout">The layout to apply.</param>
        /// <param name="settings">the file log settings</param>
        public FileLog(string filename, LogLevel level, ILayout layout = null, FileLogSettings settings = null)
        {
            Level     = level;
            _layout   = layout == null ? new FileLogLayout() : (FileLogLayout)layout;
            _settings = settings ?? new FileLogSettings();

            _filename = filename;
        }
        public void TestSetup()
        {
            strategyFactory = Substitute.For <IRollingStrategyFactory>();

            settings = new FileLogSettings();

            provider = new RollingStrategyProvider("log", strategyFactory, () => settings);
        }
Beispiel #10
0
        public void Should_validate_settings()
        {
            settings = new FileLogSettings
            {
                OutputTemplate = null
            };

            new Action(() => new FileLog(settings)).Should().Throw <ArgumentNullException>();
        }
Beispiel #11
0
        public void Should_be_enabled_for_configured_levels([Values] LogLevel level)
        {
            settings = new FileLogSettings
            {
                EnabledLogLevels = new[] { LogLevel.Error, LogLevel.Fatal }
            };

            new FileLog(settings).IsEnabledFor(level).Should().Be(settings.EnabledLogLevels.Contains(level));
        }
        public SynchronousSingleFileMuxer(
            [NotNull] IEventsWriterProviderFactory writerProviderFactory,
            [NotNull] FileLogSettings settings)
        {
            this.settings = settings;

            buffer             = new LogEventInfo[1];
            workerCancellation = new CancellationTokenSource();
            writerProvider     = writerProviderFactory.CreateProvider(settings.FilePath, () => this.settings);
        }
Beispiel #13
0
        public void ObtainWriterAsync_should_collect_garbage()
        {
            ObtainWriter();

            settings = new FileLogSettings {
                OutputBufferSize = 42
            };
            ObtainWriter();

            garbageCollector.Received().RemoveStaleFiles(Arg.Any <FilePath[]>());
        }
Beispiel #14
0
        public void Should_flush_by_updated_file_path()
        {
            registration.IsValid("xxx").Returns(false, false, true);
            settings = new FileLogSettings {
                FilePath = "xxx"
            };
            log.Info("Test.");
            log.Flush();

            muxer.Received().FlushAsync("xxx");
        }
        public void Should_catch_errors()
        {
            var settings    = new FileLogSettings();
            var oldSettings = settings;
            var provider    = new SafeSettingsProvider(() => settings ?? throw new Exception());

            provider.Get().Should().BeSameAs(oldSettings);

            settings = null;
            new Action(() => provider.Get()).Should().NotThrow();
        }
        public void Should_return_cached_value_on_error()
        {
            var settings    = new FileLogSettings();
            var oldSettings = settings;
            var provider    = new SafeSettingsProvider(() => settings ?? throw new Exception());

            provider.Get().Should().BeSameAs(oldSettings);

            settings = null;
            provider.Get().Should().BeSameAs(oldSettings);
        }
        public void Should_not_cache_settings_when_there_are_no_errors()
        {
            var settings  = new FileLogSettings();
            var settings2 = new FileLogSettings();
            var provider  = new SafeSettingsProvider(() => settings);

            provider.Get().Should().BeSameAs(settings);

            settings = settings2;
            provider.Get().Should().BeSameAs(settings2);
        }
Beispiel #18
0
        public void ObtainWriterAsync_should_dispose_old_writer()
        {
            ObtainWriter();

            settings = new FileLogSettings {
                OutputBufferSize = 42
            };
            ObtainWriter();

            eventsWriter.Received().Dispose();
        }
Beispiel #19
0
        public void ObtainWriterAsync_should_create_new_writer_if_Encoding_changed()
        {
            ObtainWriter();

            settings = new FileLogSettings {
                Encoding = Encoding.ASCII
            };
            ObtainWriter();

            eventsWriterFactory.Received(1).TryCreateWriter(Arg.Any <FilePath>(), Arg.Is <FileLogSettings>(s => Equals(s.Encoding, Encoding.UTF8)));
            eventsWriterFactory.Received(1).TryCreateWriter(Arg.Any <FilePath>(), Arg.Is <FileLogSettings>(s => Equals(s.Encoding, Encoding.ASCII)));
        }
Beispiel #20
0
        public void ObtainWriterAsync_should_create_new_writer_if_OutputBufferSize_changed()
        {
            ObtainWriter();

            settings = new FileLogSettings {
                OutputBufferSize = 42
            };
            ObtainWriter();

            eventsWriterFactory.Received(1).TryCreateWriter(Arg.Any <FilePath>(), Arg.Is <FileLogSettings>(s => s.OutputBufferSize == 65536));
            eventsWriterFactory.Received(1).TryCreateWriter(Arg.Any <FilePath>(), Arg.Is <FileLogSettings>(s => s.OutputBufferSize == 42));
        }
Beispiel #21
0
        public void Should_dispose_registration_on_path_change()
        {
            registration.IsValid("xxx").Returns(false, false, true);
            log.Info("Test.");

            settings = new FileLogSettings {
                FilePath = "xxx"
            };
            log.Info("Test.");

            registration.Received().Dispose();
        }
Beispiel #22
0
        public void ObtainWriterAsync_should_create_new_writer_if_FileOpenMode_changed()
        {
            ObtainWriter();

            settings = new FileLogSettings {
                FileOpenMode = FileOpenMode.Rewrite
            };
            ObtainWriter();

            eventsWriterFactory.Received(1).TryCreateWriter(Arg.Any <FilePath>(), Arg.Is <FileLogSettings>(s => s.FileOpenMode == FileOpenMode.Append));
            eventsWriterFactory.Received(1).TryCreateWriter(Arg.Any <FilePath>(), Arg.Is <FileLogSettings>(s => s.FileOpenMode == FileOpenMode.Rewrite));
        }
Beispiel #23
0
        public void ObtainWriterAsync_should_not_create_new_writer_while_cooldown_is_active()
        {
            ObtainWriter();

            cooldownController.IsCool.Returns(false);

            settings = new FileLogSettings {
                OutputBufferSize = 42
            };
            ObtainWriter();

            eventsWriterFactory.Received(1).TryCreateWriter(Arg.Any <FilePath>(), Arg.Any <FileLogSettings>());
        }
Beispiel #24
0
        public void Should_obtain_new_registration_on_path_change()
        {
            registration.IsValid("xxx").Returns(false, false, true);
            log.Info("Test.");

            settings = new FileLogSettings {
                FilePath = "xxx"
            };
            log.Info("Test.");

            muxer.Received(1).Register("logs/log", Arg.Any <FileLogSettings>(), Arg.Any <WeakReference>());
            muxer.Received(1).Register("xxx", Arg.Any <FileLogSettings>(), Arg.Any <WeakReference>());
        }
        public void Should_update_settings_only_from_owner()
        {
            var initialBufferSize = new FileLogSettings().OutputBufferSize;

            muxer.TryAdd(CreateEventInfo(new FileLogSettings {
                OutputBufferSize = 10
            }), false);
            settingsInsideMuxer().OutputBufferSize.Should().Be(initialBufferSize);

            muxer.TryAdd(CreateEventInfo(new FileLogSettings {
                OutputBufferSize = 10
            }), true);
            settingsInsideMuxer().OutputBufferSize.Should().Be(10);
        }
        public void Global_FlushAsync_should_flush_all_muxers()
        {
            var muxer1    = Substitute.For <ISingleFileMuxer>();
            var muxer2    = Substitute.For <ISingleFileMuxer>();
            var settings1 = new FileLogSettings();
            var settings2 = new FileLogSettings();

            singleMuxerFactory.Create(settings1).Returns(muxer1);
            singleMuxerFactory.Create(settings2).Returns(muxer2);
            Register("log1", settings1);
            Register("log2", settings2);

            muxer.FlushAsync();

            muxer1.Received().FlushAsync();
            muxer2.Received().FlushAsync();
        }
Beispiel #27
0
        public ILog Build(BuildContext context)
        {
            if (!enabled)
            {
                context.LogDisabled("FileLog");
                return(null);
            }

            if (settingsProvider == null)
            {
                var settings = new FileLogSettings();
                settingsCustomization.Customize(settings);
                settingsProvider = () => settings;
            }

            return(logCustomization.Customize(new FileLog(settingsProvider)));
        }
Beispiel #28
0
        public void TestSetup()
        {
            capturedEvents = new List <LogEvent>();

            registration = Substitute.For <IMuxerRegistration>();
            registration.IsValid("logs/log").Returns(true);

            muxer = Substitute.For <IMultiFileMuxer>();
            muxer.TryAdd(Arg.Any <FilePath>(), Arg.Do <LogEventInfo>(e => capturedEvents.Add(e.Event)), Arg.Any <WeakReference>()).Returns(true);
            muxer.Register(Arg.Any <FilePath>(), Arg.Any <FileLogSettings>(), Arg.Any <WeakReference>()).Returns(registration);

            settings = new FileLogSettings {
                FilePath = "logs/log", OutputTemplate = OutputTemplate.Parse("{Message}"), EnableFileLogSettingsCache = false
            };

            log = new FileLog(muxer, () => settings);
        }
        public void EventsLost_should_return_sum_of_events_lost_in_each_single_file_muxer()
        {
            var muxer1    = Substitute.For <ISingleFileMuxer>();
            var muxer2    = Substitute.For <ISingleFileMuxer>();
            var settings1 = new FileLogSettings();
            var settings2 = new FileLogSettings();

            singleMuxerFactory.Create(settings1).Returns(muxer1);
            singleMuxerFactory.Create(settings2).Returns(muxer2);
            muxer1.EventsLost.Returns(2);
            muxer2.EventsLost.Returns(3);

            Register("log1", settings1);
            Register("log2", settings2);

            muxer.EventsLost.Should().Be(5);
        }
        public void OneTimeSetup()
        {
            var configuration = new ContainerConfiguration(typeof(CassandraFileSystem).Assembly);
            var container     = new Container(configuration);
            var settings      = new FileLogSettings();
            var logger        = new CompositeLog(new ConsoleLog(), new FileLog(settings));

            container.Configurator.ForAbstraction <ILog>().UseInstances(logger);
            container.Configurator.ForAbstraction <Config>().UseInstances(Config);
            CassandraConfigurator.ConfigureCassandra(container, logger);
            directoryRepository = container.Get <DirectoryRepository>();
            fileRepository      = container.Get <FileRepository>();
            var session = container.Get <ISession>();

            directoriesTableEvent = new Table <CQLDirectory>(session);
            filesTableEvent       = new Table <CQLFile>(session);
        }