예제 #1
0
        public void Can_use_reporter_of_type_and_use_specific_metrics_formatter_and_flushinterval()
        {
            // Arrange
            var formatter = new MetricsJsonOutputFormatter();
            var builder   = new MetricsBuilder().Report.Using <TestReporter>(formatter: formatter, flushInterval: TimeSpan.FromDays(1));

            // Act
            var metrics  = builder.Build();
            var reporter = (metrics.Reporters as MetricsReporterCollection)?.GetType <TestReporter>();

            // Assert
            reporter?.Formatter.Should().BeOfType(typeof(MetricsJsonOutputFormatter));
            reporter?.FlushInterval.Should().Be(TimeSpan.FromDays(1));
        }
        /// <summary>
        ///     Add the <see cref="MetricsJsonOutputFormatter" /> allowing metrics to optionally be reported as JSON.
        /// </summary>
        /// <param name="metricFormattingBuilder">
        ///     The <see cref="IMetricsOutputFormattingBuilder" /> used to configure JSON formatting
        ///     options.
        /// </param>
        /// <param name="setupAction">The JSON formatting options to use.</param>
        /// <returns>
        ///     An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
        /// </returns>
        public static IMetricsBuilder AsJson(
            this IMetricsOutputFormattingBuilder metricFormattingBuilder,
            Action <MetricsJsonOptions> setupAction = null)
        {
            if (metricFormattingBuilder == null)
            {
                throw new ArgumentNullException(nameof(metricFormattingBuilder));
            }

            var options = new MetricsJsonOptions();

            setupAction?.Invoke(options);

            var formatter = new MetricsJsonOutputFormatter(options.SerializerSettings);

            return(metricFormattingBuilder.Using(formatter));
        }
        public void Should_remove_formatter_of_existing_type_when_adding()
        {
            // Arrange
            var formatters        = new MetricsFormatterCollection();
            var existingFormatter = new MetricsJsonOutputFormatter();

            formatters.TryAdd(existingFormatter);
            formatters.TryAdd(new MetricsTextOutputFormatter());

            // Act
            var newFormatter = new MetricsJsonOutputFormatter();

            formatters.TryAdd(newFormatter);

            // Assert
            formatters.Single(f => f.GetType() == typeof(MetricsJsonOutputFormatter)).Should().NotBeSameAs(existingFormatter);
        }