示例#1
0
文件: ConsoleLog.cs 项目: Brar/entlib
        /// <summary>
        /// Subscribes to an <see cref="IObservable{EventEntry}"/> using a <see cref="ConsoleSink"/>.
        /// </summary>
        /// <param name="eventStream">The event stream. Typically this is an instance of <see cref="ObservableEventListener"/>.</param>
        /// <param name="formatter">The formatter.</param>
        /// <param name="colorMapper">The color mapper instance.</param>
        /// <returns>A subscription to the sink that can be disposed to unsubscribe the sink, or to get access to the sink instance.</returns>
        public static SinkSubscription<ConsoleSink> LogToConsole(this IObservable<EventEntry> eventStream, IEventTextFormatter formatter = null, IConsoleColorMapper colorMapper = null)
        {
            var sink = new ConsoleSink();

            formatter = formatter ?? new EventTextFormatter();
            colorMapper = colorMapper ?? new DefaultConsoleColorMapper();

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

            return new SinkSubscription<ConsoleSink>(subscription, sink);
        }
        public static Tuple<string, ConsoleColor?> TryFormatAsStringAndColor(this EventEntry entry, IEventTextFormatter formatter, IConsoleColorMapper colorMapper)
        {
            Guard.ArgumentNotNull(entry, "entry");
            Guard.ArgumentNotNull(formatter, "formatter");
            Guard.ArgumentNotNull(colorMapper, "colorMapper");

            var message = TryFormatAsString(entry, formatter);
            if (message != null)
            {
                try
                {
                    var color = colorMapper.Map(entry.Schema.Level);
                    return Tuple.Create(message, color);
                }
                catch (Exception e)
                {
                    SemanticLoggingEventSource.Log.MapEntryLevelToColorFailed((int)entry.Schema.Level, e.ToString());
                    return Tuple.Create(message, (ConsoleColor?)null);
                }
            }

            return null;
        }
        public static Tuple <string, ConsoleColor?> TryFormatAsStringAndColor(this EventEntry entry, IEventTextFormatter formatter, IConsoleColorMapper colorMapper)
        {
            Guard.ArgumentNotNull(entry, "entry");
            Guard.ArgumentNotNull(formatter, "formatter");
            Guard.ArgumentNotNull(colorMapper, "colorMapper");

            var message = TryFormatAsString(entry, formatter);

            if (message != null)
            {
                try
                {
                    var color = colorMapper.Map(entry.Schema.Level);
                    return(Tuple.Create(message, color));
                }
                catch (Exception e)
                {
                    SemanticLoggingEventSource.Log.MapEntryLevelToColorFailed((int)entry.Schema.Level, e.ToString());
                    return(Tuple.Create(message, (ConsoleColor?)null));
                }
            }

            return(null);
        }
 public static IDisposable SubscribeWithFormatterAndColor(this IObservable <EventEntry> source, IEventTextFormatter formatter, IConsoleColorMapper colorMapper, IObserver <Tuple <string, ConsoleColor?> > sink)
 {
     return(source.CreateSubscription(sink, entry => entry.TryFormatAsStringAndColor(formatter, colorMapper)));
 }
示例#5
0
文件: ConsoleLog.cs 项目: Brar/entlib
 /// <summary>
 /// Creates an event listener that logs using a <see cref="ConsoleSink"/>.
 /// </summary>
 /// <param name="formatter">The formatter.</param>
 /// <param name="colorMapper">The color mapper instance.</param>
 /// <returns>An event listener that uses <see cref="ConsoleSink"/> to display events.</returns>
 public static EventListener CreateListener(IEventTextFormatter formatter = null, IConsoleColorMapper colorMapper = null)
 {
     var listener = new ObservableEventListener();
     listener.LogToConsole(formatter, colorMapper);
     return listener;
 }
 protected override void Given()
 {
     sut = new DefaultConsoleColorMapper();
     results = new Dictionary<EventLevel, ConsoleColor?>();
 }
 public static IDisposable SubscribeWithFormatterAndColor(this IObservable<EventEntry> source, IEventTextFormatter formatter, IConsoleColorMapper colorMapper, IObserver<Tuple<string, ConsoleColor?>> sink)
 {
     return source.CreateSubscription(sink, entry => entry.TryFormatAsStringAndColor(formatter, colorMapper));
 }
示例#8
0
        /// <summary>
        /// Creates an event listener that logs using a <see cref="ConsoleSink"/>.
        /// </summary>
        /// <param name="formatter">The formatter.</param>
        /// <param name="colorMapper">The color mapper instance.</param>
        /// <returns>An event listener that uses <see cref="ConsoleSink"/> to display events.</returns>
        public static EventListener CreateListener(IEventTextFormatter formatter = null, IConsoleColorMapper colorMapper = null)
        {
            var listener = new ObservableEventListener();

            listener.LogToConsole(formatter, colorMapper);
            return(listener);
        }
示例#9
0
        /// <summary>
        /// Subscribes to an <see cref="IObservable{EventEntry}"/> using a <see cref="ConsoleSink"/>.
        /// </summary>
        /// <param name="eventStream">The event stream. Typically this is an instance of <see cref="ObservableEventListener"/>.</param>
        /// <param name="formatter">The formatter.</param>
        /// <param name="colorMapper">The color mapper instance.</param>
        /// <returns>A subscription to the sink that can be disposed to unsubscribe the sink, or to get access to the sink instance.</returns>
        public static SinkSubscription <ConsoleSink> LogToConsole(this IObservable <EventEntry> eventStream, IEventTextFormatter formatter = null, IConsoleColorMapper colorMapper = null)
        {
            formatter   = formatter ?? new EventTextFormatter();
            colorMapper = colorMapper ?? new DefaultConsoleColorMapper();

            var sink = new ConsoleSink(formatter, colorMapper);

            var subscription = eventStream.Subscribe(sink);

            return(new SinkSubscription <ConsoleSink>(subscription, sink));
        }
示例#10
0
 protected override void Given()
 {
     Sut     = new DefaultConsoleColorMapper();
     Results = new Dictionary <EventLevel, ConsoleColor?>();
 }
示例#11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConsoleSink" /> class.
 /// </summary>
 public ConsoleSink(IEventTextFormatter formatter, IConsoleColorMapper colorMapper)
 {
     this.formatter   = formatter;
     this.colorMapper = colorMapper;
 }