/// <summary>
    /// Handles the middleware.
    /// </summary>
    public async Task Invoke(HttpContext context)
    {
        var path = _httpContextService.Path;

        if (_settings?.Stub?.HealthcheckOnRootUrl == true && path == "/")
        {
            _httpContextService.SetStatusCode(HttpStatusCode.OK);
            await _httpContextService.WriteAsync("OK");

            return;
        }

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

            return;
        }

        var correlationId = Guid.NewGuid().ToString();
        var requestLogger = _requestLoggerFactory.GetRequestLogger();

        requestLogger.SetCorrelationId(correlationId);
        ResponseModel response = null;

        try
        {
            response = await HandleRequest(correlationId);
        }
        catch (RequestValidationException e)
        {
            await HandleRequestValidationException(correlationId, e);
        }
        catch (Exception e)
        {
            HandleException(correlationId, e);
        }

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

        if (enableRequestLogging)
        {
            _logger.LogInformation($"Request: {jsonLoggingResult}");
        }

        await _stubContext.AddRequestResultAsync(loggingResult, response);

        // We need to map the model to a DTO here, because the frontend expects that.
        await _requestNotify.NewRequestReceivedAsync(_mapper.Map <RequestOverviewDto>(loggingResult));
    }
Пример #2
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));
        }