Пример #1
0
        private Task LogMetricsAsync()
        {
            if (!InternalConfig.StatisticsEnabled)
            {
                _log.LogDebug("Metrics logging is not enabled.");
                return(Task.FromResult(false));
            }
            if (_metricsRoot == null)
            {
                return(Task.FromResult(true));
            }

            var snapshot = _metricsRoot.Snapshot.Get();

            snapshot.ToMetric();

            // write to statistics log
            _metricsLogger.LogInformation("Sdk metrics:");
            var metricsFormatter = new MetricsTextOutputFormatter();

            using var stream = new MemoryStream();
            metricsFormatter.WriteAsync(stream, snapshot);
            var result = Encoding.UTF8.GetString(stream.ToArray());

            _metricsLogger.LogInformation(result);

            // call all report runners defined by user
            var tasks = _metricsRoot.ReportRunner.RunAllAsync();

            Task.WhenAll(tasks);

            return(Task.FromResult(true));
        }
Пример #2
0
        /// <summary>
        ///     Add the <see cref="MetricsTextOutputFormatter" /> allowing metrics to optionally be reported as plain text.
        /// </summary>
        /// <param name="metricFormattingBuilder">
        ///     The <see cref="IMetricsOutputFormattingBuilder" /> used to configure formatting
        ///     options.
        /// </param>
        /// <param name="metricFields">The metric fields to output as well as their names. This will override the globally configured metric fields.</param>
        /// <returns>
        ///     An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
        /// </returns>
        public static IMetricsBuilder AsPlainText(
            this IMetricsOutputFormattingBuilder metricFormattingBuilder,
            MetricFields metricFields)
        {
            if (metricFormattingBuilder == null)
            {
                throw new ArgumentNullException(nameof(metricFormattingBuilder));
            }

            var formatter = new MetricsTextOutputFormatter(metricFields);

            return(metricFormattingBuilder.Using(formatter));
        }
        public void Should_not_replace_formatter_if_types_are_different_and_asked_not_to_replace()
        {
            // Arrange
            var formatter = new MetricsTextOutputFormatter(new MetricsTextOptions {
                Encoding = Encoding.BigEndianUnicode
            });
            var builder = new MetricsBuilder()
                          .OutputMetrics.Using(formatter)
                          .OutputMetrics.Using <MetricsJsonOutputFormatter>(false);

            // Act
            var metrics = builder.Build();

            // Assert
            metrics.OutputMetricsFormatters.Count.Should().Be(2);
        }
        public void Can_keep_existing_formatter_if_registered_more_than_once()
        {
            // Arrange
            var formatter = new MetricsTextOutputFormatter(new MetricsTextOptions {
                Encoding = Encoding.BigEndianUnicode
            });
            var builder = new MetricsBuilder()
                          .OutputMetrics.Using(formatter)
                          .OutputMetrics.Using <MetricsTextOutputFormatter>(false);

            // Act
            var metrics = builder.Build();

            // Assert
            metrics.OutputMetricsFormatters.Count.Should().Be(1);
            metrics.OutputMetricsFormatters.First().Should().BeSameAs(formatter);
        }
Пример #5
0
        /// <summary>
        ///     Add the <see cref="MetricsTextOutputFormatter" /> allowing metrics to optionally be reported as plain text.
        /// </summary>
        /// <param name="metricFormattingBuilder">
        ///     The <see cref="IMetricsOutputFormattingBuilder" /> used to configure formatting
        ///     options.
        /// </param>
        /// <param name="setupAction">The plain text formatting options to use.</param>
        /// <returns>
        ///     An <see cref="IMetricsBuilder" /> that can be used to further configure App Metrics.
        /// </returns>
        public static IMetricsBuilder AsPlainText(
            this IMetricsOutputFormattingBuilder metricFormattingBuilder,
            Action <MetricsTextOptions> setupAction = null)
        {
            if (metricFormattingBuilder == null)
            {
                throw new ArgumentNullException(nameof(metricFormattingBuilder));
            }

            var options = new MetricsTextOptions();

            setupAction?.Invoke(options);

            var formatter = new MetricsTextOutputFormatter();

            return(metricFormattingBuilder.Using(formatter));
        }
Пример #6
0
 public SimpleConsoleMetricsReporter()
 {
     Formatter = new MetricsTextOutputFormatter();
 }