/// <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);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Adds <paramref name="entryFormatter" /> or the default formatter for <typeparamref name="TEntry" /> to each of
        /// the <see cref="ILogWriterConfig" /> objects in <paramref name="logWriterConfigs" /> that are of type
        /// <see cref="TextLogWriterConfig" />.
        /// <para>
        /// This is a convenience function to make it easy to apply the same formatting for the same entry types to multiple
        /// logwriters.
        /// </para>
        /// </summary>
        /// <typeparam name="TEntry"></typeparam>
        /// <param name="logWriterConfigs">A collection of <see cref="ILogWriterConfig" /> objects.</param>
        /// <param name="entryFormatter">
        /// The <see cref="EntryFormatter{TEntry}" /> to use for all entries of type <typeparamref name="TEntry" />;
        /// or <c>null</c> to use the default entryformatter for <typeparamref name="TEntry" />.
        /// </param>
        /// <param name="overwriteExistingFormatters">
        /// If <c>true</c>, existing formatters for <typeparamref name="TEntry" /> are
        /// overwritten with default formatters.
        /// </param>
        /// <returns>The <paramref name="logWriterConfigs" /> parameter, for fluent call chaining.</returns>
        public static IEnumerable <ILogWriterConfig> FormatAll <TEntry>(this IEnumerable <ILogWriterConfig> logWriterConfigs,
                                                                        EntryFormatter <TEntry> entryFormatter = null,
                                                                        bool overwriteExistingFormatters       = false)
            where TEntry : ILogEntry
        {
            Contract.Requires <ArgumentNullException>(logWriterConfigs != null);

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

            foreach (var logWriterConfig in logWriterConfigs)
            {
                var textLogWriterConfig = logWriterConfig as TextLogWriterConfig;
                if (textLogWriterConfig != null)
                {
                    if (overwriteExistingFormatters || !textLogWriterConfig.HasFormatterFor <TEntry>())
                    {
                        textLogWriterConfig.Format(entryFormatter);
                    }
                }
            }
            return(logWriterConfigs);
        }
        /// <summary>
        /// Sets formatting for entry types <typeparamref name="TEntry" /> using <paramref name="entryFormatter" />.
        /// </summary>
        /// <typeparam name="TEntry">The log entry type for the specified <paramref name="entryFormatter" />.</typeparam>
        /// <param name="entryFormatter">
        /// The <see cref="EntryFormatter{TEntry}" /> to use to format <typeparamref name="TEntry" /> objects.
        /// If <c>null</c>, the <see cref="DefaultFormatterAttribute" /> on <typeparamref name="TEntry" /> is used to resolve a
        /// default formatter.
        /// </param>
        /// <returns><c>this</c>, to support chaining configuration calls in a fluent manner.</returns>
        /// <remarks>
        /// If <see cref="Format{TEntry}(LogJam.Writer.Text.EntryFormatter{TEntry})" /> is called more than once for the same
        /// <typeparamref name="TEntry" /> value, the last formatter is the only one used. In other words, repeating the call
        /// replaces
        /// earlier formatters.
        /// </remarks>
        public TextLogWriterConfig Format <TEntry>(EntryFormatter <TEntry> entryFormatter = null)
            where TEntry : ILogEntry
        {
            if (entryFormatter == null)
            { // Try creating the default entry formatter
                entryFormatter = DefaultFormatterAttribute.GetDefaultFormatterFor <TEntry>();
                if (entryFormatter == null)
                {
                    throw new ArgumentNullException(nameof(entryFormatter),
                                                    $"No [DefaultFormatter] attribute could be found for entry type {typeof(TEntry).FullName}, so {nameof(entryFormatter)} argument must be set.");
                }
            }

            Type entryType = typeof(TEntry);
            Action <TextLogWriter> configureAction = (mw) => mw.AddFormat(entryFormatter);

            // Remove any existing formatters for the same TEntry.
            _formatters.RemoveAll(tuple => tuple.Item1 == entryType);

            _formatters.Add(new Tuple <Type, object, Action <TextLogWriter> >(entryType, entryFormatter, configureAction));
            return(this);
        }