/// <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);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Formats and writes <paramref name="entries" /> to <paramref name="testOutputHelper" />.
        /// </summary>
        /// <param name="testOutputHelper"></param>
        /// <param name="entries"></param>
        /// <param name="entryFormatter"></param>
        /// <typeparam name="TEntry">The log entry type; must implement <see cref="ILogEntry" /></typeparam>
        public static void WriteEntries <TEntry>(this ITestOutputHelper testOutputHelper, IEnumerable <TEntry> entries, EntryFormatter <TEntry> entryFormatter = null)
            where TEntry : ILogEntry
        {
            Contract.Requires <ArgumentNullException>(entries != null);
            Contract.Requires <ArgumentNullException>(testOutputHelper != null);

            var setupLog     = new SetupLog();
            var formatWriter = new TestOutputFormatWriter(testOutputHelper, setupLog);
            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);
                }
            }
        }