/// <summary>
        /// Formats and writes <paramref name="entries" /> to <paramref name="textWriter" />.
        /// </summary>
        /// <param name="entries"></param>
        /// <param name="textWriter"></param>
        /// <param name="entryFormatter"></param>
        /// <typeparam name="TEntry"></typeparam>
        public static void WriteEntriesTo <TEntry>(this IEnumerable <TEntry> entries, TextWriter textWriter, EntryFormatter <TEntry> entryFormatter = null)
            where TEntry : ILogEntry
        {
            Contract.Requires <ArgumentNullException>(entries != null);
            Contract.Requires <ArgumentNullException>(textWriter != null);

            if (entryFormatter == null)
            { // Try creating the default log formatter
                entryFormatter = DefaultFormatterAttribute.GetDefaultFormatterFor <TEntry>();
                if (entryFormatter == null)
                {
                    throw new ArgumentNullException(nameof(entryFormatter),
                                                    $"No [DefaultFormatter] could be found for entry type {typeof(TEntry).FullName}, so logFormatter argument must be set.");
                }
            }

            var setupLog     = new SetupLog();
            var formatWriter = new TextWriterFormatWriter(setupLog, textWriter, disposeWriter: false);
            var logWriter    = new TextLogWriter(setupLog, formatWriter);

            logWriter.AddFormat(entryFormatter);
            IEntryWriter <TEntry> entryWriter;

            logWriter.TryGetEntryWriter(out entryWriter);
            using (logWriter)
            {
                logWriter.Start();
                for (var enumerator = entries.GetEnumerator(); enumerator.MoveNext();)
                {
                    TEntry logEntry = enumerator.Current;
                    entryWriter.Write(ref logEntry);
                }
            }
        }
        public ReflectionFormatterTests(ITestOutputHelper testOutput)
        {
            _testOutput = testOutput;

            _setupLog            = new SetupLog();
            _formatterOutput     = new StringWriter();
            _formatWriter        = new TextWriterFormatWriter(_setupLog, _formatterOutput);
            _reflectionFormatter = new ReflectionFormatter
            {
                // Excluded for most tests, b/c type names make the output messy
                IncludeTypeNames = false
            };
        }
예제 #3
0
        public void WriteLinesStringBuilderWorks(string lines, int indentLevel, string expectedOutput)
        {
            // Prepare
            var setupLog   = new SetupLog();
            var sbOut      = new StringBuilder();
            var textWriter = new StringWriter(sbOut);

            using (var formatWriter = new TextWriterFormatWriter(setupLog, textWriter))
            {
                // Test
                formatWriter.WriteLines(new StringBuilder(lines), relativeIndentLevel: indentLevel);
            }

            // Verify
            Assert.Equal(expectedOutput, sbOut.ToString());
        }
예제 #4
0
        public void WriteTimeOffsetWorks(double milliseconds, string expectedOutput)
        {
            // Prepare
            var setupLog   = new SetupLog();
            var sbOut      = new StringBuilder();
            var textWriter = new StringWriter(sbOut);

            using (var formatWriter = new TextWriterFormatWriter(setupLog, textWriter))
            {
                // Test
                var timeOffset = TimeSpan.FromMilliseconds(milliseconds);
                formatWriter.WriteTimeOffset(timeOffset, ColorCategory.None);
            }

            // Verify
            Assert.Equal(expectedOutput, sbOut.ToString());
        }