/// <inheritdoc />
        public async Task <FluentHttpResponse> Invoke(FluentHttpMiddlewareContext context)
        {
            var request = context.Request;

            if (!_logger.IsEnabled(LogLevel.Information))
            {
                return(await _next(context));
            }

            var options = request.GetLoggingOptions(_options);

            if (request.Message.Content == null || !(options.ShouldLogDetailedRequest ?? false))
            {
                _logger.LogInformation("Pre-request... {request}", request);
            }
            else
            {
                var requestContent = await request.Message.Content.ReadAsStringAsync();

                _logger.LogInformation("Pre-request... {request}\nContent: {requestContent}", request, requestContent);
            }

            var response = await _next(context);

            if (response.Content == null || !(options.ShouldLogDetailedResponse ?? false))
            {
                _logger.LogInformation("Post-request... {response}", response);
                return(response);
            }

            var responseContent = await response.Content.ReadAsStringAsync();

            _logger.LogInformation("Post-request... {response}\nContent: {responseContent}", response, responseContent);
            return(response);
        }
        /// <inheritdoc />
        public async Task <FluentHttpResponse> Invoke(FluentHttpMiddlewareContext context)
        {
            var request = context.Request;
            var watch   = Stopwatch.StartNew();

            FluentHttpResponse response;

            try
            {
                response = await _next(context);
            }
            finally
            {
                var threshold = request.GetTimerWarnThreshold() ?? _options.WarnThreshold;

                if (_logger.IsEnabled(LogLevel.Warning) && watch.Elapsed > threshold)
                {
                    _logger.LogWarning(TimeTakenMessage, request, watch.ElapsedMilliseconds);
                }
                else if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.LogInformation(TimeTakenMessage, request, watch.ElapsedMilliseconds);
                }
            }

            return(response.SetTimeTaken(watch.Elapsed));
        }
        /// <inheritdoc />
        public async Task <FluentHttpResponse> Invoke(FluentHttpMiddlewareContext context)
        {
            var request = context.Request;

            var options = request.GetResponseCachingOptions(_options);

            if (options.ShouldIgnore || options.Matcher != null && !options.Matcher(request))
            {
                return(await _next(context));
            }
            var hash = request.GetHash();
            FluentHttpResponse response = null;

            if (!options.IsWriteOnly)
            {
                response = await _service.Get(hash);
            }
            if (response != null)
            {
                foreach (var item in request.Items)
                {
                    response.Items[item.Key] = item.Value;
                }

                _logger.LogInformation("Pre-request - Returning a cached response {hash}", hash);
                return(response);
            }

            response = await _next(context);

            _logger.LogInformation("Post-Response - Caching request... {hash}", hash);
            await _service.Set(hash, response);

            return(response);
        }
Beispiel #4
0
        /// <inheritdoc />
        public async Task <FluentHttpResponse> Invoke(FluentHttpMiddlewareContext context)
        {
            var request          = context.Request;
            var stopwatchElapsed = TimeSpan.Zero;

            FluentHttpResponse response;

            try
            {
                var watch = ValueStopwatch.StartNew();
                response = await _next(context);

                stopwatchElapsed = watch.GetElapsedTime();
            }
            finally
            {
                var threshold = request.GetTimerWarnThreshold() ?? _options.WarnThreshold;
                if (_logger.IsEnabled(LogLevel.Warning) && stopwatchElapsed > threshold)
                {
                    _logger.LogWarning(TimeTakenMessage, request, stopwatchElapsed.TotalMilliseconds);
                }
                else if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug(TimeTakenMessage, request, stopwatchElapsed.TotalMilliseconds);
                }
            }

            return(response.SetTimeTaken(stopwatchElapsed));
        }
Beispiel #5
0
        /// <inheritdoc />
        public async Task <FluentHttpResponse> Invoke(FluentHttpMiddlewareContext context)
        {
            var request = context.Request;

            if (!_logger.IsEnabled(LogLevel.Information))
            {
                return(await _next(context));
            }

            var options = request.GetLoggingOptions(_options);
            var watch   = ValueStopwatch.StartNew();
            FluentHttpResponse response;

            if (options.IsCondensed.GetValueOrDefault(false) &&
                !options.ShouldLogDetailedRequest.GetValueOrDefault(false) &&
                !options.ShouldLogDetailedResponse.GetValueOrDefault(false))
            {
                response = await _next(context);

                _logger.LogInformation("HTTP request [{method}] {requestUrl} responded {statusCode:D} in {elapsed:n0}ms",
                                       request.Method,
                                       request.Uri,
                                       response.StatusCode,
                                       watch.GetElapsedTime().TotalMilliseconds
                                       );
                return(response);
            }

            if (!(options.ShouldLogDetailedRequest ?? false))
            {
                _logger.LogInformation("Pre-request... {request}", request);
            }
            else
            {
                string?requestContent = null;
                if (request.Message.Content != null)
                {
                    requestContent = await request.Message.Content.ReadAsStringAsync();
                }
                _logger.LogInformation(
                    "Pre-request... {request}\nHeaders: {headers}\nContent: {requestContent}",
                    request,
                    request.Headers.ToFormattedString(),
                    requestContent
                    );
            }

            response = await _next(context);

            var stopwatchElapsed = watch.GetElapsedTime();

            if (response.Content == null || !(options.ShouldLogDetailedResponse ?? false))
            {
                _logger.LogInformation("Post-request... {response} in {elapsed:n0}ms", response, stopwatchElapsed.TotalMilliseconds);
                return(response);
            }

            var responseContent = await response.Content.ReadAsStringAsync();

            _logger.LogInformation("Post-request... {response}\nHeaders: {headers}\nContent: {responseContent} in {elapsed:n0}ms",
                                   response,
                                   response.Headers.ToFormattedString(),
                                   responseContent,
                                   stopwatchElapsed.TotalMilliseconds
                                   );
            return(response);
        }
Beispiel #6
0
 public async Task <FluentHttpResponse> Invoke(FluentHttpMiddlewareContext context) => await context.Func();