public void RolledFileWithIncrementWillCreateArchiveFileWithMaxSequenceIfDateTemplateDoesMatch() { using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, 0, new SimpleMessageFormatter(), false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "1234567890")); Assert.IsTrue(sink.RollingHelper.UpdateRollingInformationIfNecessary()); sink.RollingHelper.PerformRoll(new DateTime(2007, 01, 01)); sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "12345")); Assert.IsTrue(sink.RollingHelper.UpdateRollingInformationIfNecessary()); sink.RollingHelper.PerformRoll(new DateTime(2007, 01, 02)); sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "abcde")); } Assert.IsTrue(File.Exists(fileName)); Assert.AreEqual("abcde", File.ReadAllText(fileName)); Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".2007.2" + Extension)); Assert.AreEqual("12345", File.ReadAllText(fileNameWithoutExtension + ".2007.2" + Extension)); Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".2007.1" + Extension)); Assert.AreEqual("1234567890", File.ReadAllText(fileNameWithoutExtension + ".2007.1" + Extension)); string[] archiveFiles = Directory.GetFiles(".", fileNameWithoutExtension + ".2007*" + Extension + "*"); Assert.AreEqual(2, archiveFiles.Length); }
public void RolledFileWithOverwriteWillFallBackToUniqueNameIfDateTemplateMatchesButArchiveFileIsInUse() { string targetArchiveFile = fileNameWithoutExtension + ".2007" + Extension; using (FileStream stream = File.Open(targetArchiveFile, FileMode.CreateNew, FileAccess.Write, FileShare.Read)) { using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day, 0, new SimpleMessageFormatter(), false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "1234567890")); Assert.IsTrue(sink.RollingHelper.UpdateRollingInformationIfNecessary()); sink.RollingHelper.PerformRoll(new DateTime(2007, 01, 01)); sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "12345")); } } Assert.IsTrue(File.Exists(fileName)); Assert.AreEqual("12345", File.ReadAllText(fileName)); Assert.IsTrue(File.Exists(targetArchiveFile)); Assert.AreEqual(string.Empty, File.ReadAllText(targetArchiveFile)); // couldn't archive string[] archiveFiles = Directory.GetFiles(".", targetArchiveFile + "*"); Assert.AreEqual(2, archiveFiles.Length); foreach (string archiveFile in archiveFiles) { if (!Path.GetFileName(archiveFile).Equals(targetArchiveFile)) { Assert.AreEqual("1234567890", File.ReadAllText(archiveFile)); } } }
public void WillRollForDateIfEnabled() { using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, 0, new SimpleMessageFormatter(), false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.CurrentDateTimeField = DateTime.Now; sink.RollingHelper.UpdateRollingInformationIfNecessary(); dateTimeProvider.CurrentDateTimeField = DateTime.Now.AddDays(2); Assert.IsNotNull(sink.RollingHelper.CheckIsRollNecessary()); } using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.None, 0, new SimpleMessageFormatter(), false)) { sink.RollingHelper.UpdateRollingInformationIfNecessary(); sink.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.CurrentDateTimeField = DateTime.Now; sink.RollingHelper.UpdateRollingInformationIfNecessary(); dateTimeProvider.CurrentDateTimeField = DateTime.Now.AddDays(2); Assert.IsNull(sink.RollingHelper.CheckIsRollNecessary()); } }
public void RolledFileWithOverwriteWillCreateArchiveFileIfDateTemplateDoesNotMatch() { using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day, 0, false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; sink.OnNext("1234567890"); Assert.IsTrue(sink.RollingHelper.UpdateRollingInformationIfNecessary()); sink.RollingHelper.PerformRoll(new DateTime(2007, 01, 01)); sink.OnNext("12345"); Assert.IsTrue(sink.RollingHelper.UpdateRollingInformationIfNecessary()); sink.RollingHelper.PerformRoll(new DateTime(2008, 01, 01)); sink.OnNext("abcde"); } Assert.IsTrue(File.Exists(fileName)); Assert.AreEqual("abcde", File.ReadAllText(fileName)); Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".2008" + extension)); Assert.AreEqual("12345", File.ReadAllText(fileNameWithoutExtension + ".2008" + extension)); Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".2007" + extension)); Assert.AreEqual("1234567890", File.ReadAllText(fileNameWithoutExtension + ".2007" + extension)); string[] archiveFiles = Directory.GetFiles(".", fileNameWithoutExtension + ".2007" + extension + "*"); Assert.AreEqual(1, archiveFiles.Length); archiveFiles = Directory.GetFiles(".", fileNameWithoutExtension + ".2008" + extension + "*"); Assert.AreEqual(1, archiveFiles.Length); }
/// <summary> /// Initializes a new instance of the <see cref="StreamWriterRollingHelper"/>. /// </summary> /// <param name="owner">The <see cref="RollingFlatFileSink"/> to use.</param> public StreamWriterRollingHelper(RollingFlatFileSink owner) { this.owner = owner; this.dateTimeProvider = new DateTimeProvider(); this.performsRolling = this.owner.rollInterval != RollInterval.None || this.owner.rollSizeInBytes > 0; }
public void WillNotRollWhenTracingIfNotOverThresholds() { using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, 0, new SimpleMessageFormatter(), false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; } }
public void ConcurrentAppendsEntriesToRollingFlatFile() { const int NumberOfEntries = 300; using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, 0, new SimpleMessageFormatter(), false)) { Parallel.For(0, NumberOfEntries, i => sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "Info-" + i + ":"))); } Assert.AreEqual <int>(NumberOfEntries, File.ReadAllText(fileName).Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries).Length); }
public void RollingFlatFileCreatesSubDirectories() { string file = @"dir1\dir2\rolling\patterns\practices\log.xt"; using (var sink = new RollingFlatFileSink(file, 1024, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, -1, new SimpleMessageFormatter(), false)) { Assert.IsTrue(new DirectoryInfo(Path.GetDirectoryName(file)).Exists); } Directory.Delete(Path.GetDirectoryName(file), true); }
/// <summary> /// Subscribes to an <see cref="IObservable{EventEntry}"/> using a <see cref="RollingFlatFileSink"/>. /// </summary> /// <param name="eventStream">The event stream. Typically this is an instance of <see cref="ObservableEventListener"/>.</param> /// <param name="fileName">The filename where the entries will be logged.</param> /// <param name="rollSizeKB">The maximum file size (KB) before rolling.</param> /// <param name="timestampPattern">The date format that will be appended to the new roll file.</param> /// <param name="rollFileExistsBehavior">Expected behavior that will be used when the roll file has to be created.</param> /// <param name="rollInterval">The time interval that makes the file to be rolled.</param> /// <param name="formatter">The formatter.</param> /// <param name="maxArchivedFiles">The maximum number of archived files to keep.</param> /// <param name="isAsync">Specifies if the writing should be done asynchronously, or synchronously with a blocking call.</param> /// <returns>A subscription to the sink that can be disposed to unsubscribe the sink and dispose it, or to get access to the sink instance.</returns> public static SinkSubscription <RollingFlatFileSink> LogToRollingFlatFile(this IObservable <EventEntry> eventStream, string fileName, int rollSizeKB, string timestampPattern, RollFileExistsBehavior rollFileExistsBehavior, RollInterval rollInterval, IEventTextFormatter formatter = null, int maxArchivedFiles = 0, bool isAsync = false) { if (string.IsNullOrWhiteSpace(fileName)) { fileName = FileUtil.CreateRandomFileName(); } var sink = new RollingFlatFileSink(fileName, rollSizeKB, timestampPattern, rollFileExistsBehavior, rollInterval, maxArchivedFiles, isAsync); var subscription = eventStream.SubscribeWithFormatter(formatter ?? new EventTextFormatter(), sink); return(new SinkSubscription <RollingFlatFileSink>(subscription, sink)); }
public void SinkForNewFileWillUseCreationDateToCalculateRollDate() { using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day, 0, new SimpleMessageFormatter(), false)) { sink.RollingHelper.DateTimeProvider = this.dateTimeProvider; Assert.IsTrue(sink.RollingHelper.UpdateRollingInformationIfNecessary()); Assert.AreEqual(sink.RollingHelper.CalculateNextRollDate(File.GetCreationTime(fileName)), sink.RollingHelper.NextRollDateTime); Assert.IsNull(sink.RollingHelper.CheckIsRollNecessary()); } }
/// <summary> /// Subscribes to an <see cref="IObservable{EventEntry}"/> using a <see cref="RollingFlatFileSink"/>. /// </summary> /// <param name="eventStream">The event stream. Typically this is an instance of <see cref="ObservableEventListener"/>.</param> /// <param name="fileName">The filename where the entries will be logged.</param> /// <param name="rollSizeKB">The maximum file size (KB) before rolling.</param> /// <param name="timestampPattern">The date format that will be appended to the new roll file.</param> /// <param name="rollFileExistsBehavior">Expected behavior that will be used when the roll file has to be created.</param> /// <param name="rollInterval">The time interval that makes the file to be rolled.</param> /// <param name="formatter">The formatter.</param> /// <param name="maxArchivedFiles">The maximum number of archived files to keep.</param> /// <param name="isAsync">Specifies if the writing should be done asynchronously, or synchronously with a blocking call.</param> /// <returns>A subscription to the sink that can be disposed to unsubscribe the sink and dispose it, or to get access to the sink instance.</returns> public static SinkSubscription<RollingFlatFileSink> LogToRollingFlatFile(this IObservable<EventEntry> eventStream, string fileName, int rollSizeKB, string timestampPattern, RollFileExistsBehavior rollFileExistsBehavior, RollInterval rollInterval, IEventTextFormatter formatter = null, int maxArchivedFiles = 0, bool isAsync = false) { if (string.IsNullOrWhiteSpace(fileName)) { fileName = FileUtil.CreateRandomFileName(); } var sink = new RollingFlatFileSink(fileName, rollSizeKB, timestampPattern, rollFileExistsBehavior, rollInterval, maxArchivedFiles, isAsync); var subscription = eventStream.SubscribeWithFormatter(formatter ?? new EventTextFormatter(), sink); return new SinkSubscription<RollingFlatFileSink>(subscription, sink); }
public void WriterKeepsTally() { using (var sink = new RollingFlatFileSink(fileName, 10, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day, 0, new SimpleMessageFormatter(), false)) { sink.RollingHelper.DateTimeProvider = this.dateTimeProvider; Assert.IsTrue(sink.RollingHelper.UpdateRollingInformationIfNecessary()); sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "12345")); Assert.AreEqual(5L, sink.Tally); } }
public void SinkForExistingFileWillUseCreationDateToCalculateRollDate() { File.WriteAllText(fileName, "existing text"); File.SetCreationTime(fileName, new DateTime(2000, 01, 01)); using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day, 0, new SimpleMessageFormatter(), false)) { this.dateTimeProvider.CurrentDateTimeField = new DateTime(2008, 01, 01); sink.RollingHelper.DateTimeProvider = this.dateTimeProvider; Assert.IsTrue(sink.RollingHelper.UpdateRollingInformationIfNecessary()); Assert.AreEqual(sink.RollingHelper.CalculateNextRollDate(File.GetCreationTime(fileName)), sink.RollingHelper.NextRollDateTime); Assert.AreEqual(this.dateTimeProvider.CurrentDateTime, sink.RollingHelper.CheckIsRollNecessary()); } }
public void RolledAtMidnight() { DateTime rollDate = DateTime.Now.AddDays(1).Date; using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Midnight, 0, new SimpleMessageFormatter(), false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.CurrentDateTimeField = rollDate; sink.RollingHelper.UpdateRollingInformationIfNecessary(); Assert.IsNotNull(sink.RollingHelper.NextRollDateTime); Assert.IsNotNull(sink.RollingHelper.CheckIsRollNecessary()); Assert.AreEqual(rollDate, sink.RollingHelper.NextRollDateTime); } }
[Ignore] // TODO fix race condition public void ConcurrentAppendsEntriesToFlatFileWhenUsingAsync() { const int NumberOfEntries = 300; using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, 0, new SimpleMessageFormatter(), isAsync: true)) { Parallel.For(0, NumberOfEntries, i => sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "|" + i))); sink.FlushAsync().Wait(); } var entries = File.ReadAllText(fileName).Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); Assert.AreEqual <int>(NumberOfEntries, entries.Length); for (int i = 0; i < NumberOfEntries; i++) { CollectionAssert.Contains(entries, i.ToString()); } }
public void RolledFileWithOverwriteAndCapWillCreateArchiveFileIfDateTemplateDoesNotMatchAndKeepTheNewest() { using (var sink = new RollingFlatFileSink( fileName, 0, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day, 1, new SimpleMessageFormatter(), false)) { var testTime = new DateTime(2007, 01, 01); sink.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.CurrentDateTimeField = testTime; sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "1234567890")); sink.RollingHelper.PerformRoll(testTime); sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "12345")); dateTimeProvider.CurrentDateTimeField = testTime.AddYears(1); sink.RollingHelper.PerformRoll(testTime.AddYears(1)); sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "abcde")); dateTimeProvider.CurrentDateTimeField = testTime.AddYears(2); sink.RollingHelper.PerformRoll(testTime.AddYears(2)); sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "edcbe")); } Assert.IsTrue(File.Exists(fileName)); Assert.IsTrue(File.ReadAllText(fileName).Contains("edcbe")); Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".2009" + Extension)); Assert.IsTrue(File.ReadAllText(fileNameWithoutExtension + ".2009" + Extension).Contains("abcde")); Assert.IsFalse(File.Exists(fileNameWithoutExtension + ".2008" + Extension)); Assert.IsFalse(File.Exists(fileNameWithoutExtension + ".2007" + Extension)); }
public void WillRollExistingFileIfOverDateThreshold() { string existingPayload = new string('c', 10); DateTime currentDateTime = new DateTime(2007, 1, 1); File.WriteAllText(fileName, existingPayload); File.SetCreationTime(fileName, currentDateTime); using (var sink = new RollingFlatFileSink(fileName, 1, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day, 0, new SimpleMessageFormatter(), false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.CurrentDateTimeField = currentDateTime.AddDays(2); sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "logged message")); } Assert.AreEqual(existingPayload, File.ReadAllText(fileNameWithoutExtension + ".2007" + Extension)); Assert.IsTrue(File.ReadAllText(fileName).Contains("logged message")); }
public void WillTruncateExistingFileIfOverSizeThresholdAndNoPatternIsSpecifiedForOverwriteBehavior() { string existingPayload = new string('c', 5000); DateTime currentDateTime = new DateTime(2007, 1, 1); File.WriteAllText(fileName, existingPayload); File.SetCreationTime(fileName, currentDateTime); using (var sink = new RollingFlatFileSink(fileName, 1, string.Empty, RollFileExistsBehavior.Overwrite, RollInterval.None, 0, new SimpleMessageFormatter(), false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.CurrentDateTimeField = currentDateTime; sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "logged message")); } Assert.IsFalse(File.ReadAllText(fileName).Contains(existingPayload)); Assert.IsTrue(File.ReadAllText(fileName).Contains("logged message")); }
public void WillRollExistingFileIfOverSizeThresholdAndNoPatternIsSpecifiedForIncrementBehavior() { string existingPayload = new string('c', 5000); DateTime currentDateTime = new DateTime(2007, 1, 1); File.WriteAllText(fileName, existingPayload); File.SetCreationTime(fileName, currentDateTime); using (var sink = new RollingFlatFileSink(fileName, 1, "", RollFileExistsBehavior.Increment, RollInterval.None, 0, false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.currentDateTime = currentDateTime; sink.OnNext("logged message"); } Assert.AreEqual(existingPayload, File.ReadAllText(fileNameWithoutExtension + ".1" + extension)); Assert.IsTrue(File.ReadAllText(fileName).Contains("logged message")); }
public void WillRollExistingFileIfOverSizeThresholdAndNoPatternIsSpecifiedForIncrementBehaviorWhenUsingAsync() { string existingPayload = new string('c', 5000); DateTime currentDateTime = new DateTime(2007, 1, 1); File.WriteAllText(fileName, existingPayload); File.SetCreationTime(fileName, currentDateTime); using (var sink = new RollingFlatFileSink(fileName, 1, string.Empty, RollFileExistsBehavior.Increment, RollInterval.None, 0, new SimpleMessageFormatter(), isAsync: true)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.CurrentDateTimeField = currentDateTime; sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "logged message")); sink.FlushAsync().Wait(); } Assert.AreEqual(existingPayload, File.ReadAllText(fileNameWithoutExtension + ".1" + Extension)); Assert.IsTrue(File.ReadAllText(fileName).Contains("logged message")); }
public void RolledFileWithIncrementAndCapWillCreateArchiveFileWithMaxSequenceIfDateTemplateDoesMatchAndKeepTheNewest() { using (var sink = new RollingFlatFileSink( fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, 1, new SimpleMessageFormatter(), false)) { var testTime = new DateTime(2007, 01, 01); sink.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.CurrentDateTimeField = testTime; sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "1234567890")); dateTimeProvider.CurrentDateTimeField = testTime.AddHours(12); sink.RollingHelper.PerformRoll(testTime.AddHours(12)); sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "12345")); dateTimeProvider.CurrentDateTimeField = testTime.AddHours(24); sink.RollingHelper.PerformRoll(testTime.AddHours(24)); sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "abcde")); } Assert.IsTrue(File.Exists(fileName)); Assert.AreEqual("abcde", File.ReadAllText(fileName)); Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".2007.2" + Extension)); Assert.AreEqual("12345", File.ReadAllText(fileNameWithoutExtension + ".2007.2" + Extension)); Assert.IsFalse(File.Exists(fileNameWithoutExtension + ".2007.1" + Extension)); string[] archiveFiles = Directory.GetFiles(".", fileNameWithoutExtension + ".2007*" + Extension + "*"); Assert.AreEqual(1, archiveFiles.Length); }
public void WillRollForSize() { using (var sink = new RollingFlatFileSink(fileName, 1, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Year, 0, new SimpleMessageFormatter(), false)) { sink.RollingHelper.UpdateRollingInformationIfNecessary(); sink.OnNext(EventEntryTestHelper.Create(formattedMessage: new string('c', 1200))); Assert.IsNotNull(sink.RollingHelper.CheckIsRollNecessary()); } using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Year, 0, new SimpleMessageFormatter(), false)) { sink.RollingHelper.UpdateRollingInformationIfNecessary(); sink.OnNext(EventEntryTestHelper.Create(formattedMessage: new string('c', 1200))); Assert.IsNull(sink.RollingHelper.CheckIsRollNecessary()); } }
public void RolledFileWillHaveCurrentDateForTimestamp() { using (var sink = new RollingFlatFileSink(fileName, 10, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day, 0, new SimpleMessageFormatter(), false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "1234567890")); Assert.IsTrue(sink.RollingHelper.UpdateRollingInformationIfNecessary()); sink.RollingHelper.PerformRoll(new DateTime(2007, 01, 01)); sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "12345")); Assert.AreEqual(5L, sink.Tally); } Assert.IsTrue(File.Exists(fileName)); Assert.AreEqual("12345", File.ReadAllText(fileName)); Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".2007" + Extension)); Assert.AreEqual("1234567890", File.ReadAllText(fileNameWithoutExtension + ".2007" + Extension)); }
public void RolledFileWithIncrementAndCapWillCreateArchiveFileWithMaxSequenceIfDateTemplateDoesMatchAndKeepTheNewest() { using (var sink = new RollingFlatFileSink( fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, 1, false)) { var testTime = new DateTime(2007, 01, 01); sink.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.currentDateTime = testTime; sink.OnNext("1234567890"); dateTimeProvider.currentDateTime = testTime.AddHours(12); sink.RollingHelper.PerformRoll(testTime.AddHours(12)); sink.OnNext("12345"); dateTimeProvider.currentDateTime = testTime.AddHours(24); sink.RollingHelper.PerformRoll(testTime.AddHours(24)); sink.OnNext("abcde"); } Assert.IsTrue(File.Exists(fileName)); Assert.AreEqual("abcde", File.ReadAllText(fileName)); Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".2007.2" + extension)); Assert.AreEqual("12345", File.ReadAllText(fileNameWithoutExtension + ".2007.2" + extension)); Assert.IsFalse(File.Exists(fileNameWithoutExtension + ".2007.1" + extension)); string[] archiveFiles = Directory.GetFiles(".", fileNameWithoutExtension + ".2007*" + extension + "*"); Assert.AreEqual(1, archiveFiles.Length); }
public void WillTruncateExistingFileIfOverSizeThresholdAndNoPatternIsSpecifiedForOverwriteBehavior() { string existingPayload = new string('c', 5000); DateTime currentDateTime = new DateTime(2007, 1, 1); File.WriteAllText(fileName, existingPayload); File.SetCreationTime(fileName, currentDateTime); using (var sink = new RollingFlatFileSink(fileName, 1, "", RollFileExistsBehavior.Overwrite, RollInterval.None, 0, false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.currentDateTime = currentDateTime; sink.OnNext("logged message"); } Assert.IsFalse(File.ReadAllText(fileName).Contains(existingPayload)); Assert.IsTrue(File.ReadAllText(fileName).Contains("logged message")); }
public void ConcurrentAppendsEntriesToRollingFlatFile() { const int NumberOfEntries = 300; using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, 0, false)) { Parallel.For(0, NumberOfEntries, i => sink.OnNext("Info-" + i + ":")); } Assert.AreEqual<int>(NumberOfEntries, File.ReadAllText(fileName).Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries).Length); }
public void WillRollExistingFileIfOverSizeThresholdAndNoPatternIsSpecifiedForIncrementBehavior() { string existingPayload = new string('c', 5000); DateTime currentDateTime = new DateTime(2007, 1, 1); File.WriteAllText(fileName, existingPayload); File.SetCreationTime(fileName, currentDateTime); using (var sink = new RollingFlatFileSink(fileName, 1, string.Empty, RollFileExistsBehavior.Increment, RollInterval.None, 0, new SimpleMessageFormatter(), false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.CurrentDateTimeField = currentDateTime; sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "logged message")); } Assert.AreEqual(existingPayload, File.ReadAllText(fileNameWithoutExtension + ".1" + Extension)); Assert.IsTrue(File.ReadAllText(fileName).Contains("logged message")); }
public void SinkForNewFileWillUseCreationDateToCalculateRollDate() { using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day, 0, false)) { sink.RollingHelper.DateTimeProvider = this.dateTimeProvider; Assert.IsTrue(sink.RollingHelper.UpdateRollingInformationIfNecessary()); Assert.AreEqual(sink.RollingHelper.CalculateNextRollDate(File.GetCreationTime(fileName)), sink.RollingHelper.NextRollDateTime); Assert.IsNull(sink.RollingHelper.CheckIsRollNecessary()); } }
public void WriterKeepsTally() { using (var sink = new RollingFlatFileSink(fileName, 10, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day, 0, false)) { sink.RollingHelper.DateTimeProvider = this.dateTimeProvider; Assert.IsTrue(sink.RollingHelper.UpdateRollingInformationIfNecessary()); sink.OnNext("12345"); Assert.AreEqual(5L, sink.Tally); } }
public void SinkForExistingFileWillUseCreationDateToCalculateRollDate() { File.WriteAllText(fileName, "existing text"); File.SetCreationTime(fileName, new DateTime(2000, 01, 01)); using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day, 0, false)) { this.dateTimeProvider.currentDateTime = new DateTime(2008, 01, 01); sink.RollingHelper.DateTimeProvider = this.dateTimeProvider; Assert.IsTrue(sink.RollingHelper.UpdateRollingInformationIfNecessary()); Assert.AreEqual(sink.RollingHelper.CalculateNextRollDate(File.GetCreationTime(fileName)), sink.RollingHelper.NextRollDateTime); Assert.AreEqual(this.dateTimeProvider.CurrentDateTime, sink.RollingHelper.CheckIsRollNecessary()); } }
public void RolledAtMidnight() { DateTime rollDate = DateTime.Now.AddDays(1).Date; using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Midnight, 0, false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.currentDateTime = rollDate; sink.RollingHelper.UpdateRollingInformationIfNecessary(); Assert.IsNotNull(sink.RollingHelper.NextRollDateTime); Assert.IsNotNull(sink.RollingHelper.CheckIsRollNecessary()); Assert.AreEqual(rollDate, sink.RollingHelper.NextRollDateTime); } }
public void RolledFileWithIncrementWillCreateArchiveFileWithMaxSequenceIfDateTemplateDoesMatch() { using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, 0, false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; sink.OnNext("1234567890"); Assert.IsTrue(sink.RollingHelper.UpdateRollingInformationIfNecessary()); sink.RollingHelper.PerformRoll(new DateTime(2007, 01, 01)); sink.OnNext("12345"); Assert.IsTrue(sink.RollingHelper.UpdateRollingInformationIfNecessary()); sink.RollingHelper.PerformRoll(new DateTime(2007, 01, 02)); sink.OnNext("abcde"); } Assert.IsTrue(File.Exists(fileName)); Assert.AreEqual("abcde", File.ReadAllText(fileName)); Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".2007.2" + extension)); Assert.AreEqual("12345", File.ReadAllText(fileNameWithoutExtension + ".2007.2" + extension)); Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".2007.1" + extension)); Assert.AreEqual("1234567890", File.ReadAllText(fileNameWithoutExtension + ".2007.1" + extension)); string[] archiveFiles = Directory.GetFiles(".", fileNameWithoutExtension + ".2007*" + extension + "*"); Assert.AreEqual(2, archiveFiles.Length); }
public void WillRollExistingFileIfOverSizeThresholdAndNoPatternIsSpecifiedForIncrementBehaviorWhenUsingAsync() { string existingPayload = new string('c', 5000); DateTime currentDateTime = new DateTime(2007, 1, 1); File.WriteAllText(fileName, existingPayload); File.SetCreationTime(fileName, currentDateTime); using (var sink = new RollingFlatFileSink(fileName, 1, "", RollFileExistsBehavior.Increment, RollInterval.None, 0, isAsync: true)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.currentDateTime = currentDateTime; sink.OnNext("logged message"); sink.FlushAsync().Wait(); } Assert.AreEqual(existingPayload, File.ReadAllText(fileNameWithoutExtension + ".1" + extension)); Assert.IsTrue(File.ReadAllText(fileName).Contains("logged message")); }
public void WillRollForDateIfEnabled() { using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, 0, false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.currentDateTime = DateTime.Now; sink.RollingHelper.UpdateRollingInformationIfNecessary(); dateTimeProvider.currentDateTime = DateTime.Now.AddDays(2); Assert.IsNotNull(sink.RollingHelper.CheckIsRollNecessary()); } using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.None, 0, false)) { sink.RollingHelper.UpdateRollingInformationIfNecessary(); sink.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.currentDateTime = DateTime.Now; sink.RollingHelper.UpdateRollingInformationIfNecessary(); dateTimeProvider.currentDateTime = DateTime.Now.AddDays(2); Assert.IsNull(sink.RollingHelper.CheckIsRollNecessary()); } }
public void WillRollForSize() { using (var sink = new RollingFlatFileSink(fileName, 1, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Year, 0, false)) { sink.RollingHelper.UpdateRollingInformationIfNecessary(); sink.OnNext(new string('c', 1200)); Assert.IsNotNull(sink.RollingHelper.CheckIsRollNecessary()); } using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Year, 0, false)) { sink.RollingHelper.UpdateRollingInformationIfNecessary(); sink.OnNext(new string('c', 1200)); Assert.IsNull(sink.RollingHelper.CheckIsRollNecessary()); } }
public void RolledFileWithOverwriteWillFallBackToUniqueNameIfDateTemplateMatchesButArchiveFileIsInUse() { string targetArchiveFile = fileNameWithoutExtension + ".2007" + extension; using (FileStream stream = File.Open(targetArchiveFile, FileMode.CreateNew, FileAccess.Write, FileShare.Read)) { using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day, 0, false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; sink.OnNext("1234567890"); Assert.IsTrue(sink.RollingHelper.UpdateRollingInformationIfNecessary()); sink.RollingHelper.PerformRoll(new DateTime(2007, 01, 01)); sink.OnNext("12345"); } } Assert.IsTrue(File.Exists(fileName)); Assert.AreEqual("12345", File.ReadAllText(fileName)); Assert.IsTrue(File.Exists(targetArchiveFile)); Assert.AreEqual("", File.ReadAllText(targetArchiveFile)); // couldn't archive string[] archiveFiles = Directory.GetFiles(".", targetArchiveFile + "*"); Assert.AreEqual(2, archiveFiles.Length); foreach (string archiveFile in archiveFiles) { if (!Path.GetFileName(archiveFile).Equals(targetArchiveFile)) { Assert.AreEqual("1234567890", File.ReadAllText(archiveFile)); } } }
public void WillNotRollWhenTracingIfNotOverThresholds() { using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, 0, false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; } }
public void RolledFileWillHaveCurrentDateForTimestamp() { using (var sink = new RollingFlatFileSink(fileName, 10, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day, 0, false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; sink.OnNext("1234567890"); Assert.IsTrue(sink.RollingHelper.UpdateRollingInformationIfNecessary()); sink.RollingHelper.PerformRoll(new DateTime(2007, 01, 01)); sink.OnNext("12345"); Assert.AreEqual(5L, sink.Tally); } Assert.IsTrue(File.Exists(fileName)); Assert.AreEqual("12345", File.ReadAllText(fileName)); Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".2007" + extension)); Assert.AreEqual("1234567890", File.ReadAllText(fileNameWithoutExtension + ".2007" + extension)); }
public void WillRollExistingFileIfOverDateThreshold() { string existingPayload = new string('c', 10); DateTime currentDateTime = new DateTime(2007, 1, 1); File.WriteAllText(fileName, existingPayload); File.SetCreationTime(fileName, currentDateTime); using (var sink = new RollingFlatFileSink(fileName, 1, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day, 0, false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.currentDateTime = currentDateTime.AddDays(2); sink.OnNext("logged message"); } Assert.AreEqual(existingPayload, File.ReadAllText(fileNameWithoutExtension + ".2007" + extension)); Assert.IsTrue(File.ReadAllText(fileName).Contains("logged message")); }
public void ConcurrentAppendsEntriesToFlatFileWhenUsingAsync() { const int NumberOfEntries = 300; using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, 0, isAsync: true)) { Parallel.For(0, NumberOfEntries, i => sink.OnNext("|" + i)); sink.FlushAsync().Wait(); } var entries = File.ReadAllText(fileName).Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); Assert.AreEqual<int>(NumberOfEntries, entries.Length); for (int i = 0; i < NumberOfEntries; i++) { CollectionAssert.Contains(entries, i.ToString()); } }
public void RolledFileWithOverwriteAndCapWillCreateArchiveFileIfDateTemplateDoesNotMatchAndKeepTheNewest() { using (var sink = new RollingFlatFileSink( fileName, 0, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day, 1, false)) { var testTime = new DateTime(2007, 01, 01); sink.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.currentDateTime = testTime; sink.OnNext("1234567890"); sink.RollingHelper.PerformRoll(testTime); sink.OnNext("12345"); dateTimeProvider.currentDateTime = testTime.AddYears(1); sink.RollingHelper.PerformRoll(testTime.AddYears(1)); sink.OnNext("abcde"); dateTimeProvider.currentDateTime = testTime.AddYears(2); sink.RollingHelper.PerformRoll(testTime.AddYears(2)); sink.OnNext("edcbe"); } Assert.IsTrue(File.Exists(fileName)); Assert.IsTrue(File.ReadAllText(fileName).Contains("edcbe")); Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".2009" + extension)); Assert.IsTrue(File.ReadAllText(fileNameWithoutExtension + ".2009" + extension).Contains("abcde")); Assert.IsFalse(File.Exists(fileNameWithoutExtension + ".2008" + extension)); Assert.IsFalse(File.Exists(fileNameWithoutExtension + ".2007" + extension)); }
public void RollingFlatFileCreatesSubDirectories() { string file = @"dir1\dir2\rolling\patterns\practices\log.xt"; using (var sink = new RollingFlatFileSink(file, 1024, "yyyy", RollFileExistsBehavior.Increment, RollInterval.Day, -1, false)) { Assert.IsTrue(new DirectoryInfo(Path.GetDirectoryName(file)).Exists); } Directory.Delete(Path.GetDirectoryName(file), true); }
public void RolledFileWithOverwriteWillCreateArchiveFileIfDateTemplateDoesNotMatch() { using (var sink = new RollingFlatFileSink(fileName, 0, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day, 0, new SimpleMessageFormatter(), false)) { sink.RollingHelper.DateTimeProvider = dateTimeProvider; sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "1234567890")); Assert.IsTrue(sink.RollingHelper.UpdateRollingInformationIfNecessary()); sink.RollingHelper.PerformRoll(new DateTime(2007, 01, 01)); sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "12345")); Assert.IsTrue(sink.RollingHelper.UpdateRollingInformationIfNecessary()); sink.RollingHelper.PerformRoll(new DateTime(2008, 01, 01)); sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "abcde")); } Assert.IsTrue(File.Exists(fileName)); Assert.AreEqual("abcde", File.ReadAllText(fileName)); Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".2008" + Extension)); Assert.AreEqual("12345", File.ReadAllText(fileNameWithoutExtension + ".2008" + Extension)); Assert.IsTrue(File.Exists(fileNameWithoutExtension + ".2007" + Extension)); Assert.AreEqual("1234567890", File.ReadAllText(fileNameWithoutExtension + ".2007" + Extension)); string[] archiveFiles = Directory.GetFiles(".", fileNameWithoutExtension + ".2007" + Extension + "*"); Assert.AreEqual(1, archiveFiles.Length); archiveFiles = Directory.GetFiles(".", fileNameWithoutExtension + ".2008" + Extension + "*"); Assert.AreEqual(1, archiveFiles.Length); }