Exemplo n.º 1
0
        public override Task Report(CancellationToken cancellationToken = default)
        {
            if (_logger == null || cancellationToken.IsCancellationRequested)
            {
                return(Task.CompletedTask);
            }

            try
            {
                using var stream = new MemoryStream();

                var encoding = Encoding.UTF8;
                using (var writer = new StreamWriter(stream, encoding))
                {
                    if (!ConsoleReporter.TryWrite(_registry, writer, cancellationToken) &&
                        _options.Value.StopOnError)
                    {
                        Stop();
                        return(Task.CompletedTask);
                    }
                }

                _logger.Log(_options.Value.LogLevel, encoding.GetString(stream.ToArray()));
            }
            catch (Exception e)
            {
                _logger?.LogError(e, "Error reporting metrics to logger");
                if (_options.Value.StopOnError)
                {
                    Stop();
                }
            }

            return(Task.CompletedTask);
        }