Exemplo n.º 1
0
        // ReSharper disable once UnusedMember.Global
        public async Task Invoke(HttpContext context)
        {
            var path = _httpContextService.Path;

            if (_segmentsToIgnore.Any(s => path.Contains(s, StringComparison.OrdinalIgnoreCase)))
            {
                await _next(context);

                return;
            }

            const string correlationHeaderKey = "X-HttPlaceholder-Correlation";
            var          correlation          = Guid.NewGuid().ToString();
            var          requestLogger        = _requestLoggerFactory.GetRequestLogger();

            requestLogger.SetCorrelationId(correlation);
            try
            {
                // Enable rewind here to be able to read the posted body multiple times.
                _httpContextService.EnableRewind();

                // Log the request here
                requestLogger.LogRequestParameters(
                    _httpContextService.Method,
                    _httpContextService.DisplayUrl,
                    _httpContextService.GetBody(),
                    _clientDataResolver.GetClientIp(),
                    _httpContextService.GetHeaders());

                _httpContextService.ClearResponse();
                _httpContextService.TryAddHeader(correlationHeaderKey, correlation);
                var response = await _stubRequestExecutor.ExecuteRequestAsync();

                _httpContextService.SetStatusCode(response.StatusCode);
                foreach (var(key, value) in response.Headers)
                {
                    _httpContextService.AddHeader(key, value);
                }

                if (response.Body != null)
                {
                    await _httpContextService.WriteAsync(response.Body);
                }
            }
            catch (RequestValidationException e)
            {
                _httpContextService.SetStatusCode((int)HttpStatusCode.InternalServerError);
                _httpContextService.TryAddHeader(correlationHeaderKey, correlation);
                _logger.LogInformation($"Request validation exception thrown: {e.Message}");
            }
            catch (Exception e)
            {
                _httpContextService.SetStatusCode((int)HttpStatusCode.InternalServerError);
                _httpContextService.TryAddHeader(correlationHeaderKey, correlation);
                _logger.LogWarning($"Unexpected exception thrown: {e}");
            }

            var loggingResult        = requestLogger.GetResult();
            var jsonLoggingResult    = JObject.FromObject(loggingResult);
            var enableRequestLogging = _settings.Storage?.EnableRequestLogging ?? false;

            if (enableRequestLogging)
            {
                _logger.LogInformation(jsonLoggingResult.ToString());
            }

            await _stubContext.AddRequestResultAsync(loggingResult);

            // We need to map the model to a DTO here, because the frontend expected that.
            await _requestNotify.NewRequestReceivedAsync(_mapper.Map <RequestResultDto>(loggingResult));
        }
 private void HandleException(string correlationId, Exception e)
 {
     _httpContextService.SetStatusCode(HttpStatusCode.InternalServerError);
     _httpContextService.TryAddHeader(CorrelationHeaderKey, correlationId);
     _logger.LogWarning($"Unexpected exception thrown: {e}");
 }