コード例 #1
0
        public void TestClear2()
        {
            const string fname = "TestClear2.log";

            using (FileStream stream = File.OpenWrite(fname))
                using (var writer = new StreamWriter(stream))
                {
                    stream.SetLength(0);
                    writer.WriteLine("Test");
                }

            using (var logFile = new TextLogFile(_scheduler, fname))
            {
                var listener = new Mock <ILogFileListener>();
                var sections = new List <LogFileSection>();
                listener.Setup(x => x.OnLogFileModified(It.IsAny <ILogFile>(), It.IsAny <LogFileSection>()))
                .Callback((ILogFile log, LogFileSection section) => sections.Add(section));
                logFile.AddListener(listener.Object, TimeSpan.Zero, 2);

                logFile.Property(x => x.Count).ShouldAfter(TimeSpan.FromSeconds(5)).Be(1);

                using (var stream = new FileStream(fname, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
                {
                    stream.SetLength(0);

                    logFile.Property(x => x.Count).ShouldAfter(TimeSpan.FromSeconds(5)).Be(0);
                    sections.Should().EndWith(LogFileSection.Reset);
                }
            }
        }
コード例 #2
0
        public void TestTranslator1()
        {
            var translator = new Mock <ILogLineTranslator>();

            _file = new TextLogFile(_scheduler, _fname, translator: translator.Object);
            _file.AddListener(_listener.Object, TimeSpan.Zero, 10);

            _streamWriter.Write("Foo");
            _streamWriter.Flush();
            _scheduler.RunOnce();

            translator.Verify(x => x.Translate(It.Is <ILogFile>(y => y == _file), It.IsAny <LogLine>()),
                              Times.Once);
        }
コード例 #3
0
        public void TestRead2LogEntries()
        {
            using (var file = new TextLogFile(_scheduler, File2Entries))
            {
                var listener = new Mock <ILogFileListener>();
                var changes  = new List <LogFileSection>();
                listener.Setup(x => x.OnLogFileModified(It.IsAny <ILogFile>(), It.IsAny <LogFileSection>()))
                .Callback((ILogFile logFile, LogFileSection section) => changes.Add(section));

                file.AddListener(listener.Object, TimeSpan.Zero, 1);

                changes.Property(x => x.Count).ShouldAfter(TimeSpan.FromSeconds(5)).Be(7);

                changes.Should().Equal(new[]
                {
                    LogFileSection.Reset,
                    new LogFileSection(0, 1),
                    new LogFileSection(1, 1),
                    new LogFileSection(2, 1),
                    new LogFileSection(3, 1),
                    new LogFileSection(4, 1),
                    new LogFileSection(5, 1)
                });

                file.GetValue(LogFileProperties.StartTimestamp).Should().Be(new DateTime(2015, 10, 7, 19, 50, 58, 982));

                LogLine[] lines = file.GetSection(new LogFileSection(0, 6));
                lines.Should().Equal(new[]
                {
                    new LogLine(0, 0,
                                "2015-10-07 19:50:58,982 [8092, 1] INFO  SharpRemote.Hosting.OutOfProcessSiloServer (null) - Silo Server starting, args (1): \"14056\", without custom type resolver",
                                LevelFlags.Info, new DateTime(2015, 10, 7, 19, 50, 58, 982)),
                    new LogLine(1, 1, "Foobar", LevelFlags.Other, null),
                    new LogLine(2, 2, "Some more info", LevelFlags.Other, null),
                    new LogLine(3, 3,
                                "2015-10-07 19:50:58,998 [8092, 1] DEBUG SharpRemote.Hosting.OutOfProcessSiloServer (null) - Args.Length: 1",
                                LevelFlags.Debug, new DateTime(2015, 10, 7, 19, 50, 58, 998)),
                    new LogLine(4, 4, "Hey look at me", LevelFlags.Other, null),
                    new LogLine(5, 5, "dwadawdadw", LevelFlags.Other, null)
                });
            }
        }
コード例 #4
0
        public void TestRead2Lines()
        {
            using (var file = new TextLogFile(_scheduler, File2Lines))
            {
                var listener = new Mock <ILogFileListener>();
                var changes  = new List <LogFileSection>();
                listener.Setup(x => x.OnLogFileModified(It.IsAny <ILogFile>(), It.IsAny <LogFileSection>()))
                .Callback((ILogFile logFile, LogFileSection section) => changes.Add(section));

                file.AddListener(listener.Object, TimeSpan.Zero, 1);

                changes.Property(x => x.Count).ShouldAfter(TimeSpan.FromSeconds(5)).Be(3);
                changes.Should().Equal(new[]
                {
                    LogFileSection.Reset,
                    new LogFileSection(0, 1),
                    new LogFileSection(1, 1)
                });
            }
        }
コード例 #5
0
        public void Setup()
        {
            _scheduler = new ManualTaskScheduler();
            _fname     = Path.GetTempFileName();
            if (File.Exists(_fname))
            {
                File.Delete(_fname);
            }

            _stream       = File.Open(_fname, FileMode.Create, FileAccess.Write, FileShare.Read);
            _streamWriter = new StreamWriter(_stream);
            _binaryWriter = new BinaryWriter(_stream);

            _file = new TextLogFile(_scheduler, _fname);

            _listener = new Mock <ILogFileListener>();
            _changes  = new List <LogFileSection>();
            _listener.Setup(x => x.OnLogFileModified(It.IsAny <ILogFile>(), It.IsAny <LogFileSection>()))
            .Callback((ILogFile unused, LogFileSection section) => _changes.Add(section));

            _file.AddListener(_listener.Object, TimeSpan.Zero, 10);
        }
コード例 #6
0
        public void TestReadAll2()
        {
            using (var file = new TextLogFile(_scheduler, File20Mb))
            {
                var listener = new Mock <ILogFileListener>();
                var sections = new List <LogFileSection>();
                listener.Setup(x => x.OnLogFileModified(It.IsAny <ILogFile>(), It.IsAny <LogFileSection>()))
                .Callback((ILogFile logFile, LogFileSection section) => sections.Add(section));

                file.AddListener(listener.Object, TimeSpan.Zero, 1);

                file.Property(x => x.EndOfSourceReached).ShouldAfter(TimeSpan.FromSeconds(20)).BeTrue();
                file.Count.Should().Be(165342);

                sections[0].Should().Equal(LogFileSection.Reset);
                for (int i = 1; i < sections.Count; ++i)
                {
                    LogFileSection change = sections[i];
                    change.Index.Should().Be((LogLineIndex)(i - 1));
                    change.Count.Should().Be(1);
                }
            }
        }