public void FiltersEventsWithLoggerExactMatchWithNonAsciiCharactersInUtf8() { var expected = new[] { new EventExpectation { Level = Level.Warn, Logger = "Root.ЛоггерК.ЛоггерК1", Thread = "Thread-6", Timestamp = 1411231353796L, Message = "#6. Test event G.", Throwable = null, Id = 846 } }; var subject = Filter.Logger("Root.ЛоггерК.ЛоггерК1"); using (var sourceStream = new MemoryStream(sampleBytesUtf8)) using (var source = Log4JFile.Create(sourceStream)) { source.Encoding = Encoding.UTF8; var actual = source.GetEvents().Where(subject); Assert.That(actual, Is.EqualTo(expected)); } }
public void HandlesEscapedCDataNodes() { var expected = new[] { new EventExpectation { Level = Level.Info, Logger = "Root.ChildA.LoggerA2", Thread = "Thread-1", Timestamp = 1411231353782L, Message = "<nestedXml><![CDATA[Nested CDATA]]></nestedXml>", Throwable = "System.FormatException: An error is present.", Properties = { { "log4jmachinename", "EXAMPLE_PC" }, { "log4japp", "LogGenerator.exe" }, { "log4net:Identity", "" }, { "log4net:UserName", "EXAMPLE_PC\\Dev" }, { "log4net:HostName", "EXAMPLE_PC" } }, Id = 0 } }; using (var source = new MemoryStream(sampleEscapedCDataBytes)) using (var subject = Log4JFile.Create(source)) { subject.Encoding = Encoding.GetEncoding(1251); var actual = subject.GetEvents(); Assert.That(actual, Is.EqualTo(expected)); } }
public void MatchesAllInnerFilters() { var expected = new[] { new EventExpectation { Level = Level.Debug, Logger = "Root.ChildB.LoggerB2", Thread = "Thread-2", Timestamp = 1411231353792L, Message = "#2. Test event B.", Throwable = null, Id = 164 } }; var subject = Filter.All ( Filter.Level(Level.MinValue, Level.Info), Filter.Timestamp(1411231353792L, Int64.MaxValue) ); using (var sourceStream = new MemoryStream(sampleBytes)) using (var source = Log4JFile.Create(sourceStream)) { source.Encoding = Encoding.GetEncoding(1251); var actual = source.GetEvents().Where(subject); Assert.That(actual, Is.EqualTo(expected)); } }
public void FiltersEventsWithTimestampExactValue() { var expected = new[] { new EventExpectation { Level = Level.Warn, Logger = "Root.ChildA.LoggerA1", Thread = "Thread-4", Timestamp = 1411231353793L, Message = "#4. Test event E.", Throwable = null, Id = 507 } }; var subject = Filter.Timestamp(1411231353793L, 1411231353793L); using (var sourceStream = new MemoryStream(sampleBytes)) using (var source = Log4JFile.Create(sourceStream)) { source.Encoding = Encoding.GetEncoding(1251); var actual = source.GetEvents().Where(subject); Assert.That(actual, Is.EqualTo(expected)); } }
public void FiltersEventsWithMessageContainingStringWithNonAsciiCharactersInUtf8() { var expected = new[] { new EventExpectation { Level = Level.Fatal, Logger = "Root.ChildA.LoggerA2", Thread = "Thread-3", Timestamp = 1411231353792L, Message = "#3. Test event C. С кирилицей.", Throwable = null, Id = 329 } }; var subject = Filter.Message("кирилицей"); using (var sourceStream = new MemoryStream(sampleBytesUtf8)) using (var source = Log4JFile.Create(sourceStream)) { source.Encoding = Encoding.UTF8; var actual = source.GetEvents().Where(subject); Assert.That(actual, Is.EqualTo(expected)); } }
private Log4JFile ReadFile() { using (var contentStream = new MemoryStream(content_)) { var result = Log4JFile.Create(contentStream); result.Encoding = Encoding; return(result); } }
public void FiltersEventsWithMinTimestamp() { var expected = new[] { new EventExpectation { Level = Level.Debug, Logger = "Root.ChildB.LoggerB2", Thread = "Thread-2", Timestamp = 1411231353792L, Message = "#2. Test event B.", Throwable = null, Id = 164 }, new EventExpectation { Level = Level.Fatal, Logger = "Root.ChildA.LoggerA2", Thread = "Thread-3", Timestamp = 1411231353792L, Message = "#3. Test event C. С кирилицей.", Throwable = null, Id = 329 }, new EventExpectation { Level = Level.Warn, Logger = "Root.ChildA.LoggerA1", Thread = "Thread-4", Timestamp = 1411231353793L, Message = "#4. Test event E.", Throwable = null, Id = 507 }, new EventExpectation { Level = Level.Error, Logger = "Root.ChildA.LoggerA1", Thread = "Thread-5", Timestamp = 1411231353795L, Message = "#5. Test event F.", Throwable = null, Id = 671 } }; var subject = Filter.Timestamp(1411231353792L, Int64.MaxValue); using (var sourceStream = new MemoryStream(sampleBytes)) using (var source = Log4JFile.Create(sourceStream)) { source.Encoding = Encoding.GetEncoding(1251); var actual = source.GetEvents().Where(subject); Assert.That(actual, Is.EqualTo(expected)); } }
public void FiltersEventsWithLoggerStartingWithPrefix() { var expected = new[] { new EventExpectation { Level = Level.Info, Logger = "Root.ChildA.LoggerA2", Thread = "Thread-1", Timestamp = 1411231353782L, Message = "#1. Test event A.", Throwable = null, Id = 0 }, new EventExpectation { Level = Level.Fatal, Logger = "Root.ChildA.LoggerA2", Thread = "Thread-3", Timestamp = 1411231353792L, Message = "#3. Test event C. С кирилицей.", Throwable = null, Id = 329 }, new EventExpectation { Level = Level.Warn, Logger = "Root.ChildA.LoggerA1", Thread = "Thread-4", Timestamp = 1411231353793L, Message = "#4. Test event E.", Throwable = null, Id = 507 }, new EventExpectation { Level = Level.Error, Logger = "Root.ChildA.LoggerA1", Thread = "Thread-5", Timestamp = 1411231353795L, Message = "#5. Test event F.", Throwable = null, Id = 671 } }; var subject = Filter.Logger("Root.ChildA"); using (var sourceStream = new MemoryStream(sampleBytesWin1251)) using (var source = Log4JFile.Create(sourceStream)) { source.Encoding = Encoding.GetEncoding(1251); var actual = source.GetEvents().Where(subject); Assert.That(actual, Is.EqualTo(expected)); } }
private Log4JFile OpenLog4JFile() { Trace.WriteLine($"Reading log4j file. File name: '{fileStream_.Name}'.", "Log4JDash.Web.Domain.LazyLogFile"); EnsureSizeIsRead(); var effectiveMaxSize = maxSize_ ?? Size; var result = Log4JFile.Create(fileStream_, effectiveMaxSize); result.Encoding = encoding_; fileStream_.Dispose(); return(result); }
public void MatchesAnyInnerFilters() { var expected = new[] { new EventExpectation { Level = Level.Info, Logger = "Root.ChildA.LoggerA2", Thread = "Thread-1", Timestamp = 1411231353782L, Message = "#1. Test event A.", Throwable = null, Id = 0 }, new EventExpectation { Level = Level.Debug, Logger = "Root.ChildB.LoggerB2", Thread = "Thread-2", Timestamp = 1411231353792L, Message = "#2. Test event B.", Throwable = null, Id = 201 }, new EventExpectation { Level = Level.Fatal, Logger = "Root.ChildA.LoggerA2", Thread = "Thread-3", Timestamp = 1411231353792L, Message = "#3. Test event C. С кирилицей.", Throwable = null, Id = 403 } }; var subject = Filter.Any ( Filter.Level(Level.MinValue, Level.Info), Filter.Timestamp(1411231353792L, 1411231353792L) ); using (var sourceStream = new MemoryStream(sampleBytes)) using (var source = Log4JFile.Create(sourceStream)) { source.Encoding = Encoding.GetEncoding(1251); var actual = source.GetEvents().Where(subject); Assert.That(actual, Is.EqualTo(expected)); } }
public void CanReadFileWithErrorInMiddleInReverseOrder() { var expected = new[] { new EventExpectation { Level = Level.Debug, Logger = "Root.ChildB.LoggerB2", Thread = "Thread-2", Timestamp = 1411231353792L, Message = "#2. Test event B.", Throwable = null, Properties = { { "log4jmachinename", "EXAMPLE_PC" }, { "log4japp", "LogGenerator.exe" }, { "log4net:Identity", "" }, { "log4net:UserName", "EXAMPLE_PC\\Dev" }, { "log4net:HostName", "EXAMPLE_PC" } }, Id = 530 }, new EventExpectation { Level = Level.Info, Logger = "Root.ChildA.LoggerA2", Thread = "Thread-1", Timestamp = 1411231353782L, Message = "#1. Test event A.", Throwable = null, Properties = { { "log4jmachinename", "EXAMPLE_PC" }, { "log4japp", "LogGenerator.exe" }, { "log4net:Identity", "" }, { "log4net:UserName", "EXAMPLE_PC\\Dev" }, { "log4net:HostName", "EXAMPLE_PC" } }, Id = 0 } }; using (var source = new MemoryStream(sampleErrorInMiddleBytes)) using (var subject = Log4JFile.Create(source)) { subject.Encoding = Encoding.GetEncoding(1251); var actual = subject.GetEventsReverse(); Assert.That(actual, Is.EqualTo(expected)); } }
public void CanReadEventWithPropertiesCountGreaterThanPropertyBufferSize() { var expected = new[] { new EventExpectation { Level = Level.Info, Logger = "L1", Thread = "1", Timestamp = 0L, Message = "Msg", Throwable = null, Properties = { { "log4jmachinename", "EXAMPLE_PC" }, { "log4japp", "LogGenerator.exe" }, { "log4net:Identity", "" }, { "log4net:UserName", "EXAMPLE_PC\\Dev" }, { "log4net:HostName", "EXAMPLE_PC" }, { "prop-06", "#06" }, { "prop-07", "#07" }, { "prop-08", "#08" }, { "prop-09", "#09" }, { "prop-10", "#10" }, { "prop-11", "#11" }, { "prop-12", "#12" }, { "prop-13", "#13" }, { "prop-14", "#14" }, { "prop-15", "#15" }, { "prop-16", "#16" }, { "prop-17", "#17" } }, Id = 0 } }; Assume.That(expected[0].Properties.Count, Is.EqualTo(17)); using (var source = new MemoryStream(SampleProperties17Bytes)) using (var subject = Log4JFile.Create(source)) { subject.Encoding = Encoding.GetEncoding(1251); var actual = subject.GetEventsReverse(); Assert.That(actual, Is.EqualTo(expected)); } }
public void FiltersEventsWithMaxLevel() { var expected = new[] { new EventExpectation { Level = Level.Info, Logger = "Root.ChildA.LoggerA2", Thread = "Thread-1", Timestamp = 1411231353782L, Message = "#1. Test event A.", Throwable = null, Id = 0 }, new EventExpectation { Level = Level.Debug, Logger = "Root.ChildB.LoggerB2", Thread = "Thread-2", Timestamp = 1411231353792L, Message = "#2. Test event B.", Throwable = null, Id = 164 }, new EventExpectation { Level = Level.Warn, Logger = "Root.ChildA.LoggerA1", Thread = "Thread-4", Timestamp = 1411231353793L, Message = "#4. Test event E.", Throwable = null, Id = 507 } }; var subject = Filter.Level(Level.MinValue, Level.Warn); using (var sourceStream = new MemoryStream(sampleBytes)) using (var source = Log4JFile.Create(sourceStream)) { source.Encoding = Encoding.GetEncoding(1251); var actual = source.GetEvents().Where(subject); Assert.That(actual, Is.EqualTo(expected)); } }
private static void ParseXml(string filename) { using (new TimeTrace("total")) { using (new TimeTrace("process with E/S init")) { using (var eventSource = Log4JFile.Create(filename)) using (new TimeTrace("process inner")) { var filterAll = Filter.All ( Filter.Timestamp(1411231371536L, 1411231371556L), Filter.Not(Filter.Level("INFO", "ERROR")), Filter.Any ( Filter.Message("#2"), Filter.Message("#3") ), Filter.Logger("Root.ChildB") ); var matchingEvents = eventSource .GetEvents() .Where(filterAll) .Take(20) .ToList(); foreach (var @event in matchingEvents) { PrintEvent(@event); } Console.WriteLine("Found events: {0}", matchingEvents.Count); } } using (new TimeTrace("count all events")) using (var eventSource = Log4JFile.Create(filename)) { var count = eventSource.GetEvents().Count(); Console.WriteLine("Found events: {0}", count); } } }
public void CanGetFileSizeLogWriter() { const string sample = Sample1; var encoding = Encoding.UTF8; var expected = encoding.GetByteCount(sample); Exception primaryException = null; string logFile = null; try { logFile = Path.GetTempFileName(); File.WriteAllBytes(logFile, encoding.GetBytes(sample)); using (var fileHolder = new FileStream(logFile, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) { var actual = Log4JFile.GetSize(logFile); Assert.That(actual, Is.EqualTo(expected)); } } catch (Exception ex) { primaryException = ex; throw; } finally { try { File.Delete(logFile); } catch (Exception cleanupEx) { if (primaryException != null) { throw new AggregateException(primaryException, cleanupEx); } throw; } } }
public void HandlesExcessiveMaxLogSize() { var maxSignedValue = checked ((1UL << 8 * IntPtr.Size - 1) - 1); Warn.If(IntPtr.Size, Is.Not.EqualTo(4).And.Not.EqualTo(8)); Assume.That(maxSignedValue == Int64.MaxValue || IntPtr.Size != 8); Assume.That(maxSignedValue == Int32.MaxValue || IntPtr.Size != 4); var maxCStringSize = (long)maxSignedValue - 1; using (var source = new MemoryStream(sample1Bytes)) { Assert.That(() => { using (Log4JFile.Create(source, maxCStringSize)) { } }, Throws.Nothing); } }
public void NegatesInnerFilter() { var expected = new[] { new EventExpectation { Level = Level.Debug, Logger = "Root.ChildB.LoggerB2", Thread = "Thread-2", Timestamp = 1411231353792L, Message = "#2. Test event B.", Throwable = null, Id = 164 }, new EventExpectation { Level = Level.Fatal, Logger = "Root.ChildA.LoggerA2", Thread = "Thread-3", Timestamp = 1411231353792L, Message = "#3. Test event C. С кирилицей.", Throwable = null, Id = 329 } }; var childFilter = Filter.Level(Level.Info, Level.Error); var subject = Filter.Not(childFilter); using (var sourceStream = new MemoryStream(sampleBytes)) using (var source = Log4JFile.Create(sourceStream)) { source.Encoding = Encoding.GetEncoding(1251); var actual = source.GetEvents().Where(subject); Assert.That(actual, Is.EqualTo(expected)); } }
public void FiltersEventsWithLoggerStartingWithNonAsciiPrefixInWindows1251() { var expected = new[] { new EventExpectation { Level = Level.Warn, Logger = "Root.ЛоггерК.ЛоггерК1", Thread = "Thread-6", Timestamp = 1411231353796L, Message = "#6. Test event G.", Throwable = null, Id = 836 }, new EventExpectation { Level = Level.Error, Logger = "Root.ЛоггерК.ЛоггерК2", Thread = "Thread-7", Timestamp = 1411231353797L, Message = "#7. Test event H.", Throwable = null, Id = 1001 } }; var subject = Filter.Logger("Root.ЛоггерК"); using (var sourceStream = new MemoryStream(sampleBytesWin1251)) using (var source = Log4JFile.Create(sourceStream)) { source.Encoding = Encoding.GetEncoding(1251); var actual = source.GetEvents().Where(subject); Assert.That(actual, Is.EqualTo(expected)); } }
public void CanOpenFilesConcurrentlyWithLogWriter() { const string sample = Sample1; var encoding = Encoding.UTF8; var expected = new[] { new EventExpectation { Level = Level.Info, Logger = "Root.ChildA.LoggerA2", Thread = "Thread-1", Timestamp = 1411231353782L, Message = "#1. Test event A.", Throwable = null, Properties = { { "log4jmachinename", "EXAMPLE_PC" }, { "log4japp", "LogGenerator.exe" }, { "log4net:Identity", "" }, { "log4net:UserName", "EXAMPLE_PC\\Dev" }, { "log4net:HostName", "EXAMPLE_PC" } }, Id = 0 }, new EventExpectation { Level = Level.Debug, Logger = "Root.ChildB.LoggerB2", Thread = "Thread-2", Timestamp = 1411231353792L, Message = "#2. Test event B.", Throwable = null, Properties = { { "log4jmachinename", "EXAMPLE_PC" }, { "log4japp", "LogGenerator.exe" }, { "log4net:Identity", "" }, { "log4net:UserName", "EXAMPLE_PC\\Dev" }, { "log4net:HostName", "EXAMPLE_PC" } }, Id = 478 }, new EventExpectation { Level = Level.Fatal, Logger = "Root.ChildA.LoggerA2", Thread = "Thread-3", Timestamp = 1411231353792L, Message = "#3. Test event C. С кирилицей.", Throwable = null, Properties = { { "log4jmachinename", "EXAMPLE_PC" }, { "log4japp", "LogGenerator.exe" }, { "log4net:Identity", "" }, { "log4net:UserName", "EXAMPLE_PC\\Dev" }, { "log4net:HostName", "EXAMPLE_PC" } }, Id = 957 }, new EventExpectation { Level = Level.Warn, Logger = "Root.ChildA.LoggerA1", Thread = "Thread-4", Timestamp = 1411231353793L, Message = "#4. Test event E.", Throwable = null, Properties = { { "log4jmachinename", "EXAMPLE_PC" }, { "log4japp", "LogGenerator.exe" }, { "log4net:Identity", "" }, { "log4net:UserName", "EXAMPLE_PC\\Dev" }, { "log4net:HostName", "EXAMPLE_PC" } }, Id = 1459 }, new EventExpectation { Level = Level.Error, Logger = "Root.ChildA.LoggerA1", Thread = "Thread-5", Timestamp = 1411231353795L, Message = "#5. Test event F.", Throwable = null, Properties = { { "log4jmachinename", "EXAMPLE_PC" }, { "log4japp", "LogGenerator.exe" }, { "log4net:Identity", "" }, { "log4net:UserName", "EXAMPLE_PC\\Dev" }, { "log4net:HostName", "EXAMPLE_PC" } }, Id = 1937 } }; Exception primaryException = null; string logFile = null; try { logFile = Path.GetTempFileName(); File.WriteAllBytes(logFile, encoding.GetBytes(sample)); using (var fileHolder = new FileStream(logFile, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) using (var subject = Log4JFile.Create(logFile)) { subject.Encoding = encoding; var actual = subject.GetEvents(); Assert.That(actual, Is.EqualTo(expected)); } } catch (Exception ex) { primaryException = ex; throw; } finally { try { File.Delete(logFile); } catch (Exception cleanupEx) { if (primaryException != null) { throw new AggregateException(primaryException, cleanupEx); } throw; } } }
public void EnumeratesEventsDirectly() { var expected = new[] { new EventExpectation { Level = Level.Info, Logger = "Root.ChildA.LoggerA2", Thread = "Thread-1", Timestamp = 1411231353782L, Message = "#1. Test event A.", Throwable = null, Properties = { { "log4jmachinename", "EXAMPLE_PC" }, { "log4japp", "LogGenerator.exe" }, { "log4net:Identity", "" }, { "log4net:UserName", "EXAMPLE_PC\\Dev" }, { "log4net:HostName", "EXAMPLE_PC" } }, Id = 0 }, new EventExpectation { Level = Level.Debug, Logger = "Root.ChildB.LoggerB2", Thread = "Thread-2", Timestamp = 1411231353792L, Message = "#2. Test event B.", Throwable = null, Properties = { { "log4jmachinename", "EXAMPLE_PC" }, { "log4japp", "LogGenerator.exe" }, { "log4net:Identity", "" }, { "log4net:UserName", "EXAMPLE_PC\\Dev" }, { "log4net:HostName", "EXAMPLE_PC" } }, Id = 478 }, new EventExpectation { Level = Level.Fatal, Logger = "Root.ChildA.LoggerA2", Thread = "Thread-3", Timestamp = 1411231353792L, Message = "#3. Test event C. С кирилицей.", Throwable = null, Properties = { { "log4jmachinename", "EXAMPLE_PC" }, { "log4japp", "LogGenerator.exe" }, { "log4net:Identity", "" }, { "log4net:UserName", "EXAMPLE_PC\\Dev" }, { "log4net:HostName", "EXAMPLE_PC" } }, Id = 957 }, new EventExpectation { Level = Level.Warn, Logger = "Root.ChildA.LoggerA1", Thread = "Thread-4", Timestamp = 1411231353793L, Message = "#4. Test event E.", Throwable = null, Properties = { { "log4jmachinename", "EXAMPLE_PC" }, { "log4japp", "LogGenerator.exe" }, { "log4net:Identity", "" }, { "log4net:UserName", "EXAMPLE_PC\\Dev" }, { "log4net:HostName", "EXAMPLE_PC" } }, Id = 1449 }, new EventExpectation { Level = Level.Error, Logger = "Root.ChildA.LoggerA1", Thread = "Thread-5", Timestamp = 1411231353795L, Message = "#5. Test event F.", Throwable = null, Properties = { { "log4jmachinename", "EXAMPLE_PC" }, { "log4japp", "LogGenerator.exe" }, { "log4net:Identity", "" }, { "log4net:UserName", "EXAMPLE_PC\\Dev" }, { "log4net:HostName", "EXAMPLE_PC" } }, Id = 1927 } }; using (var source = new MemoryStream(sample1Bytes)) using (var subject = Log4JFile.Create(source)) { subject.Encoding = Encoding.GetEncoding(1251); var actual = subject.GetEvents(); Assert.That(actual, Is.EqualTo(expected)); } }