Example #1
0
        /// <summary>
        /// Determine if there is a formatter for
        /// </summary>
        /// <param name="logEntry"></param>
        /// <returns></returns>
        public bool HasFormatter(RxLogEntry logEntry)
        {
            IRxLogFormatter formatterInstance;

            Guard.NotNull(logEntry);
            Guard.NotNull(logEntry.Specifics);

            if (_formatterMap.TryGetValue(logEntry.Specifics.GetType(), out formatterInstance))
            {
                return(formatterInstance != null);
            }

            if (_useAttributes)
            {
                TryResolveByAttribute(logEntry);

                formatterInstance = _formatterMap[logEntry.Specifics.GetType()];

                if (formatterInstance != null)
                {
                    return(true);
                }
            }

            return(_defaultFormatter != null);
        }
        public void CrashingSubscriberHandledCorrectlyTest()
        {
            var hostConfiguration = new HostConfiguration(publishScheduler: Scheduler.Immediate);
            var configuration     = RxLoggerConfiguration.Create(hostConfiguration);

            RxLogEntry lastValueSub1 = null;
            RxLogEntry lastValueSub2 = null;

            configuration.AddSubscriber((c, en) => TestSubscriber(c, en, (r) => lastValueSub1 = r, raiseException: true));
            configuration.AddSubscriber((c, en) => TestSubscriber(c, en, (r) => lastValueSub2 = r));

            // one subscription we will let it raise exceptions, other won't.
            var entry1 = RxLogEntry.WithNoMeta(new object());
            var entry2 = RxLogEntry.WithNoMeta(new object());

            var host = new RxObservableLogHost(configuration);

            host.Publish(entry1);

            Assert.Null(lastValueSub1);
            Assert.Same(entry1, lastValueSub2);

            host.Publish(entry2);

            Assert.Null(lastValueSub1);
            Assert.Same(entry2, lastValueSub2);
        }
Example #3
0
        /// <summary>
        /// Get the formatter for a specified instance.
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public IRxLogFormatter GetFor(RxLogEntry entry)
        {
            IRxLogFormatter formatterInstance;

            Guard.NotNull(entry);
            Guard.NotNull(entry.Specifics);

            if (_formatterMap.TryGetValue(entry.Specifics.GetType(), out formatterInstance))
            {
                return(formatterInstance ?? _defaultFormatter);
            }

            if (_useAttributes)
            {
                TryResolveByAttribute(entry);

                formatterInstance = _formatterMap[entry.Specifics.GetType()];

                if (formatterInstance != null)
                {
                    return(formatterInstance);
                }
            }

            return(_defaultFormatter);
        }
Example #4
0
        public void PublishWhenEnabledTest()
        {
            var entry = new RxLogEntry(new RxLogEntryMeta(null, "", 0), new object());

            _log.Publish(entry);

            _logHost.Verify(p => p.Publish(entry), Times.Once);
        }
Example #5
0
        public void DoesntPublishWhenDisabledTest()
        {
            var entry = new RxLogEntry(new RxLogEntryMeta(null, "", 0), new object());

            _log.Enabled = false;
            _log.Publish(entry);

            _logHost.Verify(p => p.Publish(entry), Times.Never);
        }
Example #6
0
        public void StaticPublishChannelsThroughDefaultTest()
        {
            RxLog.SetDefault(_log);

            var entry = new RxLogEntry(new RxLogEntryMeta(null, "", 0), new object());

            RxLog.Log(entry);

            _logHost.Verify(p => p.Publish(entry), Times.Once);
        }
        public void CreateGenericSuccessfullyTest()
        {
            var meta = new RxLogEntryMeta(this.GetType(), "member6", 1);

            const string specifics = "Test";
            var          entry     = new RxLogEntry <string>(meta, specifics);

            Assert.Same(meta, entry.Meta);
            Assert.Same(specifics, entry.Specifics);
            Assert.True(entry.InstanceOfType <string>());
            Assert.Equal(specifics, entry.Instance);
        }
        public void CreateSuccessfullyTest()
        {
            var meta = new RxLogEntryMeta(this.GetType(), "member4", 1);

            var specifics = new object();

            var entry = new RxLogEntry(meta, specifics);

            Assert.Same(meta, entry.Meta);
            Assert.Same(specifics, entry.Specifics);
            Assert.True(entry.InstanceOfType <object>());
        }
        public void PublishedItemsChanneledThroughConfigTest()
        {
            var hostConfiguration = new HostConfiguration(prePumpObservable: (e) => PumpTest(e), publishScheduler: Scheduler.Immediate);

            var configuration = RxLoggerConfiguration.Create(hostConfiguration: hostConfiguration);

            var host = new RxObservableLogHost(configuration);

            var entry = RxLogEntry.WithNoMeta(new object());

            host.Publish(entry);

            Assert.Same(entry, _lastValue);
        }
        public string Formatted(RxLoggerConfiguration configuration, RxLogEntry entry, FormatFlags flags)
        {
            Guard.NotNull(configuration);
            Guard.NotNull(entry);

            var sb = new StringBuilder();

            // right then, we need to format the core and then the rest of the properties for the actual log item...
            // this is a straight wander of the properties of the item...

            if (flags == FormatFlags.IncludeMeta && entry.Meta != null)
            {
                sb.Append(_metaFormatter.Formatted(entry.Meta));
            }

            if (_onePropertyPerLine)
            {
                sb.AppendLine();
                sb.AppendLine(entry.Specifics.GetType().Name);
            }
            else
            {
                sb.Append(" ");
                sb.Append(entry.Specifics.GetType().Name);
                sb.Append(" ");
            }


            var attributes = entry.Specifics.Properties();

            foreach (var attribute in attributes)
            {
                if (_onePropertyPerLine)
                {
                    sb.Append(attribute.Key);
                    sb.Append("=");
                    sb.Append(attribute.Value);
                    sb.AppendLine();
                }
                else
                {
                    sb.Append(" ");
                    sb.Append(attribute.Key);
                    sb.Append("=");
                    sb.Append(attribute.Value);
                }
            }

            return(sb.ToString());
        }
Example #11
0
        private void TryResolveByAttribute(RxLogEntry instance)
        {
            Guard.NotNull(instance);

            var rootType  = instance.Specifics.GetType();
            var attribute = rootType.GetTypeInfo().GetCustomAttributes(typeof(FormatterAttribute), true).Cast <FormatterAttribute>().FirstOrDefault();

            if (attribute == null)
            {
                _formatterMap[rootType] = null;
            }
            else
            {
                _formatterMap[rootType] = (IRxLogFormatter)Activator.CreateInstance(attribute.FormatterType);
            };
        }
        public void SubscribersCorrectlySubscribedTest()
        {
            var hostConfiguration = new HostConfiguration(prePumpObservable: (e) => PumpTest(e, false), publishScheduler: Scheduler.Immediate);

            var configuration = RxLoggerConfiguration.Create(hostConfiguration: hostConfiguration);

            RxLogEntry lastValue = default(RxLogEntry);

            configuration.AddSubscriber((c, en) => TestSubscriber(c, en, (v) => lastValue = v));

            var host = new RxObservableLogHost(configuration);

            var entry = RxLogEntry.WithNoMeta(new object());

            host.Publish(entry);

            Assert.Same(entry, lastValue);
        }
        public void ErrorsDuringProcessingPassedToErrorHandlerTest()
        {
            Exception lastException = null;

            var hostConfiguration = new HostConfiguration(prePumpObservable: (e) => PumpTest(e, true), publishScheduler: Scheduler.Immediate, errored:
                                                          (e) =>
            {
                lastException = e;
                return(Observable.Return(Unit.Default));
            });

            var configuration = RxLoggerConfiguration.Create(hostConfiguration: hostConfiguration);

            var host = new RxObservableLogHost(configuration);

            var entry = RxLogEntry.WithNoMeta(new object());

            host.Publish(entry);

            Assert.NotNull(lastException);
        }
        public void PublishedItemsCanBeFilteredTest()
        {
            // indicate that pump shouldn't pass through messages
            var hostConfiguration = new HostConfiguration(prePumpObservable: (e) => PumpTest(e, ignoreAll: true), publishScheduler: Scheduler.Immediate);

            var configuration = RxLoggerConfiguration.Create(hostConfiguration: hostConfiguration);


            RxLogEntry lastSubValue = default(RxLogEntry);

            configuration.AddSubscriber((c, en) => TestSubscriber(c, en, (r) => lastSubValue = r, raiseException: true));

            var host = new RxObservableLogHost(configuration);

            var entry = RxLogEntry.WithNoMeta(new object());

            host.Publish(entry);

            // verify not received anything.
            Assert.Null(lastSubValue);
        }
        public IObservable <RxLogEntry> PumpTest(IObservable <RxLogEntry> source, bool throwException = false, bool ignoreAll = false)
        {
            return(Observable.Create <RxLogEntry>((o) =>
            {
                var sub = source.Subscribe(v =>
                {
                    if (throwException)
                    {
                        o.OnError(new ArgumentException());
                    }

                    _lastValue = v;

                    if (!ignoreAll)
                    {
                        o.OnNext(v);
                    }
                });

                return sub;
            }));
        }
 /// <summary>
 /// Simple extension to force a throw once logged.
 /// </summary>
 /// <param name="entry"></param>
 /// <param name="exception"></param>
 public static void Throw(this RxLogEntry <Classified> entry,
                          Func <RxLogEntry <Classified>, Exception> exception)
 {
     throw exception(entry);
 }
 /// <summary>
 /// Determine if there is a formatter for
 /// </summary>
 /// <param name="logEntry"></param>
 /// <returns></returns>
 public bool HasFormatter(RxLogEntry logEntry)
 {
     return(Resolver.HasFormatter(logEntry));
 }
 public IRxLogFormatter FormatterFor(RxLogEntry logEntry)
 {
     return(Resolver.GetFor(logEntry));
 }
Example #19
0
 protected override string Format(RxLoggerConfiguration configuration, RxLogEntry entry, FormatFlags format)
 {
     return(this.Format(configuration, entry.Meta, (TLog)entry.Specifics, format));
 }