Exemplo n.º 1
0
        [Ignore]    // TODO fix race condition
        public void ConcurrentAppendsEntriesToFlatFileWhenUsingAsync()
        {
            AppDomain.CurrentDomain.SetData("APPBASE", Environment.CurrentDirectory);
            const int TimesRepeated   = 50;
            const int NumberOfEntries = 100;

            for (int repeat = 0; repeat < TimesRepeated; repeat++)
            {
                this.fileName = Path.ChangeExtension(Guid.NewGuid().ToString("N"), ".log");

                try
                {
                    using (var sink = new FlatFileSink(this.fileName, new SimpleMessageFormatter(), isAsync: true))
                    {
                        Parallel.For(0, NumberOfEntries, i => sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "|" + i)));
                        sink.FlushAsync().Wait();
                    }

                    var entriesStr = ReadFileWithoutLock(this.fileName);
                    var entries    = entriesStr.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    Assert.AreEqual <int>(NumberOfEntries, entries.Length, this.fileName + "|" + entries.Length + "    " + entriesStr);
                }
                finally
                {
                    if (File.Exists(this.fileName))
                    {
                        File.Delete(this.fileName);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void CreatesFlatFile()
        {
            sink = new FlatFileSink(this.fileName, false);
            sink.OnNext("|1");

            Assert.IsTrue(File.Exists(this.fileName));

            var entries = ReadFileWithoutLock(this.fileName).Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            Assert.AreEqual <int>(1, entries.Count());
        }
Exemplo n.º 3
0
        public void CreatesFlatFile()
        {
            sink = new FlatFileSink(this.fileName, new SimpleMessageFormatter(), false);
            sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "|1"));

            Assert.IsTrue(File.Exists(this.fileName));

            var entries = ReadFileWithoutLock(this.fileName).Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            Assert.AreEqual <int>(1, entries.Count());
        }
Exemplo n.º 4
0
        public void CreatesDirectoryForLogRecursively()
        {
            string file = @"dir1\dir2\test\patterns\practices\log.xt";

            using (var flatFileSink = new FlatFileSink(file, new SimpleMessageFormatter(), false))
            {
                Assert.IsTrue(new DirectoryInfo(Path.GetDirectoryName(file)).Exists);
            }

            Directory.Delete(Path.GetDirectoryName(file), true);
        }
Exemplo n.º 5
0
        public void Setup()
        {
            FlatFileSinkData sinkParams = new FlatFileSinkData();

            sinkParams.FileName = CommonUtil.FileName;
            sinkParams.Header   = "";
            sinkParams.Footer   = "";
            this.sink           = new FlatFileSink();
            sink.Initialize(new TestLogSinkConfigurationView(sinkParams));
            CommonUtil.ResetEventLogCounter();
        }
Exemplo n.º 6
0
        /// <summary>
        /// Subscribes to an <see cref="IObservable{EventEntry}"/> using a <see cref="FlatFileSink"/>.
        /// </summary>
        /// <param name="eventStream">The event stream. Typically this is an instance of <see cref="ObservableEventListener"/>.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="formatter">The formatter.</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 <FlatFileSink> LogToFlatFile(this IObservable <EventEntry> eventStream, string fileName = null, IEventTextFormatter formatter = null, bool isAsync = false)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                fileName = FileUtil.CreateRandomFileName();
            }

            var sink = new FlatFileSink(fileName, formatter ?? new EventTextFormatter(), isAsync);

            var subscription = eventStream.Subscribe(sink);

            return(new SinkSubscription <FlatFileSink>(subscription, sink));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Subscribes to an <see cref="IObservable{EventEntry}"/> using a <see cref="FlatFileSink"/>.
        /// </summary>
        /// <param name="eventStream">The event stream. Typically this is an instance of <see cref="ObservableEventListener"/>.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="formatter">The formatter.</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<FlatFileSink> LogToFlatFile(this IObservable<EventEntry> eventStream, string fileName = null, IEventTextFormatter formatter = null, bool isAsync = false)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                fileName = FileUtil.CreateRandomFileName();
            }

            var sink = new FlatFileSink(fileName, isAsync);

            var subscription = eventStream.SubscribeWithFormatter(formatter ?? new EventTextFormatter(), sink);

            return new SinkSubscription<FlatFileSink>(subscription, sink);
        }
Exemplo n.º 8
0
        public void AppendsEntriesToFlatFile()
        {
            sink = new FlatFileSink(this.fileName, new SimpleMessageFormatter(), false);
            sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "|1"));
            sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "|2"));
            sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "|3"));

            var entries = ReadFileWithoutLock(this.fileName).Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            Assert.AreEqual <int>(3, entries.Length);
            Assert.AreEqual("1", entries[0]);
            Assert.AreEqual("2", entries[1]);
            Assert.AreEqual("3", entries[2]);
        }
Exemplo n.º 9
0
        public void AppendsEntriesToFlatFile()
        {
            sink = new FlatFileSink(this.fileName, false);
            sink.OnNext("|1");
            sink.OnNext("|2");
            sink.OnNext("|3");

            var entries = ReadFileWithoutLock(this.fileName).Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            Assert.AreEqual <int>(3, entries.Length);
            Assert.AreEqual("1", entries[0]);
            Assert.AreEqual("2", entries[1]);
            Assert.AreEqual("3", entries[2]);
        }
Exemplo n.º 10
0
        public void ConcurrentAppendsEntriesToFlatFile()
        {
            sink = new FlatFileSink(this.fileName, false);
            const int NumberOfEntries = 100;

            Parallel.For(0, NumberOfEntries, i => sink.OnNext("|" + i));

            var entries = ReadFileWithoutLock(this.fileName).Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            Assert.AreEqual <int>(NumberOfEntries, entries.Length);
            for (int i = 0; i < NumberOfEntries; i++)
            {
                CollectionAssert.Contains(entries, i.ToString());
            }
        }
Exemplo n.º 11
0
        [Ignore]    // TODO fix race condition
        public void ConcurrentAppendsEntriesToFlatFileWhenUsingAsync()
        {
            sink = new FlatFileSink(this.fileName, new SimpleMessageFormatter(), isAsync: true);
            const int NumberOfEntries = 100;

            Parallel.For(0, NumberOfEntries, i => sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "|" + i)));
            sink.FlushAsync().Wait();
            var entries = ReadFileWithoutLock(this.fileName).Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            Assert.AreEqual <int>(NumberOfEntries, entries.Length);
            for (int i = 0; i < NumberOfEntries; i++)
            {
                CollectionAssert.Contains(entries, i.ToString());
            }
        }
Exemplo n.º 12
0
        public void LogMessageToFlatFileWithNullFileName()
        {
            FlatFileSinkData sinkParams = new FlatFileSinkData();
            sinkParams.FileName = null;

            this.sink = new FlatFileSink();
            sink.Initialize(new TestLogSinkConfigurationView(sinkParams));

            CommonUtil.SendTestMessage(this.sink);

            Assert.AreEqual(1, CommonUtil.EventLogEntryCount(), "confirm warning written to event log");
            string entry = CommonUtil.GetLastEventLogEntry();
            string expected = SR.DefaultLogDestinationMessage + Environment.NewLine + Environment.NewLine +
                SR.FileSinkMissingConfiguration;
            Assert.IsTrue(entry.IndexOf(expected) > -1, "confirm error message");
        }
Exemplo n.º 13
0
        private static FlatFileSink CreateFlatFileSink(string consumerGroupName)
        {
            var formatter = new CsvEventTextFormatter();
            var filename = string.Format(
                CultureInfo.InvariantCulture,
                "Data\\{0}-{1}.csv",
                consumerGroupName,
                DateTime.Now.ToString("yyyy-MM-dd-hh-mm"));

            var sink = new FlatFileSink(filename, formatter, true);

            var outputDir = Path.Combine(Environment.CurrentDirectory, "Data");
            if (!Directory.Exists(outputDir))
            {
                Directory.CreateDirectory(outputDir);
            }
            return sink;
        }
Exemplo n.º 14
0
        public void LogMessageToFlatFileWithNullFileName()
        {
            FlatFileSinkData sinkParams = new FlatFileSinkData();

            sinkParams.FileName = null;

            this.sink = new FlatFileSink();
            sink.Initialize(new TestLogSinkConfigurationView(sinkParams));

            CommonUtil.SendTestMessage(this.sink);

            Assert.AreEqual(1, CommonUtil.EventLogEntryCount(), "confirm warning written to event log");
            string entry    = CommonUtil.GetLastEventLogEntry();
            string expected = SR.DefaultLogDestinationMessage + Environment.NewLine + Environment.NewLine +
                              SR.FileSinkMissingConfiguration;

            Assert.IsTrue(entry.IndexOf(expected) > -1, "confirm error message");
        }
Exemplo n.º 15
0
        public void MissingRequiredParameters()
        {
            // create a flatfile sink without the required parameters
            FlatFileSink badSink = new FlatFileSink();

            badSink.Initialize(new TestLogSinkConfigurationView(new FlatFileSinkData()));
            CommonUtil.SendTestMessage(badSink);
            Assert.AreEqual(1, CommonUtil.EventLogEntryCount(), "confirm an error message was written to event log");

            string entry    = CommonUtil.GetLastEventLogEntry();
            string expected = SR.DefaultLogDestinationMessage + Environment.NewLine + Environment.NewLine +
                              SR.FileSinkMissingConfiguration;

            Assert.IsTrue(entry.IndexOf(expected) > -1, "confirm error message");

            expected = CommonUtil.FormattedMessage;
            Assert.IsTrue(entry.IndexOf(expected) > -1, "confirm message is inside");
        }
Exemplo n.º 16
0
        public void IgnoresMissingEnvironmentVariables()
        {
            var tempFileName = this.fileName;

            this.fileName = Path.Combine(Environment.CurrentDirectory, tempFileName);

            this.sink = new FlatFileSink(Path.Combine(Environment.CurrentDirectory, @"%MISSINGTESTVAR%\") + tempFileName, new SimpleMessageFormatter(), false);
            this.sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "|1"));
            this.sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "|2"));
            this.sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "|3"));

            var entries = ReadFileWithoutLock(this.fileName).Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            Assert.AreEqual <int>(3, entries.Length);
            Assert.AreEqual("1", entries[0]);
            Assert.AreEqual("2", entries[1]);
            Assert.AreEqual("3", entries[2]);
        }
Exemplo n.º 17
0
        public void ExpandsExistingEnvironmentVariables()
        {
            var tempFileName = this.fileName;

            this.fileName = Path.Combine(Path.Combine(Environment.CurrentDirectory, "fromtestvariable"), tempFileName);

            this.sink = new FlatFileSink(Path.Combine(Environment.CurrentDirectory, @"%TESTVAR%\") + tempFileName, false);
            this.sink.OnNext("|1");
            this.sink.OnNext("|2");
            this.sink.OnNext("|3");

            var entries = ReadFileWithoutLock(this.fileName).Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            Assert.AreEqual <int>(3, entries.Length);
            Assert.AreEqual("1", entries[0]);
            Assert.AreEqual("2", entries[1]);
            Assert.AreEqual("3", entries[2]);
        }
Exemplo n.º 18
0
        private static FlatFileSink CreateFlatFileSink(string consumerGroupName)
        {
            var formatter = new CsvEventTextFormatter();
            var filename  = string.Format(
                CultureInfo.InvariantCulture,
                "Data\\{0}-{1}.csv",
                consumerGroupName,
                DateTime.Now.ToString("yyyy-MM-dd-hh-mm"));

            var sink = new FlatFileSink(filename, formatter, true);

            var outputDir = Path.Combine(Environment.CurrentDirectory, "Data");

            if (!Directory.Exists(outputDir))
            {
                Directory.CreateDirectory(outputDir);
            }
            return(sink);
        }
Exemplo n.º 19
0
        public void LogMessageToFlatFileWithNoPath()
        {
            string fileName = "trace.log";
            FlatFileSinkData sinkParams = new FlatFileSinkData();
            sinkParams.FileName = fileName;
            sinkParams.Header = "";
            sinkParams.Footer = "";

            this.sink = new FlatFileSink();
            sink.Initialize(new TestLogSinkConfigurationView(sinkParams));

            CommonUtil.SendTestMessage(this.sink);

            FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(stream);
            string contents = reader.ReadToEnd();
            stream.Close();

            Assert.AreEqual(0, CommonUtil.EventLogEntryCount(), "confirm no messages written to event log");
            Assert.IsTrue(contents.IndexOf(CommonUtil.FormattedMessage) > -1, "confirm message is part of contents");
        }
Exemplo n.º 20
0
        public void LogMessageToFlatFileWithNullHeaderFooter()
        {
            string           fileName   = "trace.log";
            FlatFileSinkData sinkParams = new FlatFileSinkData();

            sinkParams.FileName = fileName;
            sinkParams.Header   = null;
            sinkParams.Footer   = null;

            this.sink = new FlatFileSink();
            sink.Initialize(new TestLogSinkConfigurationView(sinkParams));

            CommonUtil.SendTestMessage(this.sink);

            FileStream   stream   = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            StreamReader reader   = new StreamReader(stream);
            string       contents = reader.ReadToEnd();

            stream.Close();

            Assert.AreEqual(0, CommonUtil.EventLogEntryCount(), "confirm no messages written to event log");
            Assert.IsTrue(contents.IndexOf(CommonUtil.FormattedMessage) > -1, "confirm message is part of contents");
        }
        public SingleFileSink()
        {
            var eventTextFormatter = new EventTextFormatter(verbosityThreshold: EventLevel.Informational, dateTimeFormat: "o");

            _sink = new FlatFileSink("someFileName.txt", eventTextFormatter, true);
        }
Exemplo n.º 22
0
        public void AppendsEntriesToFlatFile()
        {
            sink = new FlatFileSink(this.fileName, new SimpleMessageFormatter(), false);
            sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "|1"));
            sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "|2"));
            sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "|3"));

            var entries = ReadFileWithoutLock(this.fileName).Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            Assert.AreEqual<int>(3, entries.Length);
            Assert.AreEqual("1", entries[0]);
            Assert.AreEqual("2", entries[1]);
            Assert.AreEqual("3", entries[2]);
        }
Exemplo n.º 23
0
 public void Setup()
 {
     FlatFileSinkData sinkParams = new FlatFileSinkData();
     sinkParams.FileName = CommonUtil.FileName;
     sinkParams.Header = "";
     sinkParams.Footer = "";
     this.sink = new FlatFileSink();
     sink.Initialize(new TestLogSinkConfigurationView(sinkParams));
     CommonUtil.ResetEventLogCounter();
 }
Exemplo n.º 24
0
        public void MissingRequiredParameters()
        {
            // create a flatfile sink without the required parameters
            FlatFileSink badSink = new FlatFileSink();
            badSink.Initialize(new TestLogSinkConfigurationView(new FlatFileSinkData()));
            CommonUtil.SendTestMessage(badSink);
            Assert.AreEqual(1, CommonUtil.EventLogEntryCount(), "confirm an error message was written to event log");

            string entry = CommonUtil.GetLastEventLogEntry();
            string expected = SR.DefaultLogDestinationMessage + Environment.NewLine + Environment.NewLine +
                SR.FileSinkMissingConfiguration;
            Assert.IsTrue(entry.IndexOf(expected) > -1, "confirm error message");

            expected = CommonUtil.FormattedMessage;
            Assert.IsTrue(entry.IndexOf(expected) > -1, "confirm message is inside");
        }
Exemplo n.º 25
0
        public void ExpandsExistingEnvironmentVariables()
        {
            var tempFileName = this.fileName;
            this.fileName = Path.Combine(Path.Combine(Environment.CurrentDirectory, "fromtestvariable"), tempFileName);

            this.sink = new FlatFileSink(Path.Combine(Environment.CurrentDirectory, @"%TESTVAR%\") + tempFileName, new SimpleMessageFormatter(), false);
            this.sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "|1"));
            this.sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "|2"));
            this.sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "|3"));

            var entries = ReadFileWithoutLock(this.fileName).Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            Assert.AreEqual<int>(3, entries.Length);
            Assert.AreEqual("1", entries[0]);
            Assert.AreEqual("2", entries[1]);
            Assert.AreEqual("3", entries[2]);
        }
Exemplo n.º 26
0
        public void ConcurrentAppendsEntriesToFlatFile()
        {
            sink = new FlatFileSink(this.fileName, new SimpleMessageFormatter(), false);
            const int NumberOfEntries = 100;

            Parallel.For(0, NumberOfEntries, i => sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "|" + i)));

            var entries = ReadFileWithoutLock(this.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 AppendsEntriesToFlatFile()
        {
            sink = new FlatFileSink(this.fileName, false);
            sink.OnNext("|1");
            sink.OnNext("|2");
            sink.OnNext("|3");

            var entries = ReadFileWithoutLock(this.fileName).Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            Assert.AreEqual<int>(3, entries.Length);
            Assert.AreEqual("1", entries[0]);
            Assert.AreEqual("2", entries[1]);
            Assert.AreEqual("3", entries[2]);
        }
Exemplo n.º 28
0
        public void CreatesFlatFile()
        {
            sink = new FlatFileSink(this.fileName, new SimpleMessageFormatter(), false);
            sink.OnNext(EventEntryTestHelper.Create(formattedMessage: "|1"));

            Assert.IsTrue(File.Exists(this.fileName));

            var entries = ReadFileWithoutLock(this.fileName).Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            Assert.AreEqual<int>(1, entries.Count());
        }
        public void ConcurrentAppendsEntriesToFlatFileWhenUsingAsync()
        {
            sink = new FlatFileSink(this.fileName, isAsync: true);
            const int NumberOfEntries = 100;

            Parallel.For(0, NumberOfEntries, i => sink.OnNext("|" + i));
            sink.FlushAsync().Wait();
            var entries = ReadFileWithoutLock(this.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 CreatesDirectoryForLogRecursively()
        {
            string file = @"dir1\dir2\test\patterns\practices\log.xt";
            using (var flatFileSink = new FlatFileSink(file, false))
            {
                Assert.IsTrue(new DirectoryInfo(Path.GetDirectoryName(file)).Exists);
            }

            Directory.Delete(Path.GetDirectoryName(file), true);
        }
        public void CreatesFlatFile()
        {
            sink = new FlatFileSink(this.fileName, false);
            sink.OnNext("|1");

            Assert.IsTrue(File.Exists(this.fileName));

            var entries = ReadFileWithoutLock(this.fileName).Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            Assert.AreEqual<int>(1, entries.Count());
        }
        public void ConcurrentAppendsEntriesToFlatFileWhenUsingAsync()
        {
            AppDomain.CurrentDomain.SetData("APPBASE", Environment.CurrentDirectory);
            const int TimesRepeated = 50;
            const int NumberOfEntries = 100;
            for (int repeat = 0; repeat < TimesRepeated; repeat++)
            {
                this.fileName = Path.ChangeExtension(Guid.NewGuid().ToString("N"), ".log");

                try
                {
                    using (var sink = new FlatFileSink(this.fileName, isAsync: true))
                    {
                        Parallel.For(0, NumberOfEntries, i => sink.OnNext("|" + i));
                        sink.FlushAsync().Wait();
                    }

                    var entriesStr = ReadFileWithoutLock(this.fileName);
                    var entries = entriesStr.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    Assert.AreEqual<int>(NumberOfEntries, entries.Length, this.fileName + "|" + entries.Length + "    " + entriesStr);
                }
                finally
                {
                    if (File.Exists(this.fileName))
                        File.Delete(this.fileName);
                }
            }
        }
        public void IgnoresMissingEnvironmentVariables()
        {
            var tempFileName = this.fileName;
            this.fileName = Path.Combine(Environment.CurrentDirectory, tempFileName);

            this.sink = new FlatFileSink(Path.Combine(Environment.CurrentDirectory, @"%MISSINGTESTVAR%\") + tempFileName, false);
            this.sink.OnNext("|1");
            this.sink.OnNext("|2");
            this.sink.OnNext("|3");

            var entries = ReadFileWithoutLock(this.fileName).Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            Assert.AreEqual<int>(3, entries.Length);
            Assert.AreEqual("1", entries[0]);
            Assert.AreEqual("2", entries[1]);
            Assert.AreEqual("3", entries[2]);
        }