Beispiel #1
0
        /// <summary>
        /// Invokes the middleware for the current <paramref name="httpContext"/>.
        /// </summary>
        /// <param name="httpContext">The current <see cref="HttpContext"/>.</param>
        /// <returns>An awaitable to wait for to complete the request.</returns>
        public Task Invoke(HttpContext httpContext)
        {
            KeyValuePair <string, string> requestHeader = httpContext.Request.Headers.GetCorrelationIdHeader(_acceptedRequestHeaders);

            if (requestHeader.Value != null)
            {
                _logger.LogTrace("Request header '{HeaderName}' found with correlation id '{CorrelationId}'.", requestHeader.Key, requestHeader.Value);
            }

            string responseHeaderName    = _options.IncludeInResponse ? requestHeader.Key : null;
            var    correlatedHttpRequest = new HttpRequestActivity(_logger, httpContext, responseHeaderName);

            return(_asyncCorrelationManager.CorrelateAsync(
                       requestHeader.Value,
                       () =>
            {
                correlatedHttpRequest.Start(_correlationContextAccessor.CorrelationContext);
                return _next(httpContext)
                .ContinueWith(t =>
                {
                    correlatedHttpRequest.Stop();
                    return t;
                })
                .Unwrap();
            }));
        }
        /// <summary>
        /// Invokes the middleware for the current <paramref name="httpContext"/>.
        /// </summary>
        /// <param name="httpContext">The current <see cref="HttpContext"/>.</param>
        /// <returns>An awaitable to wait for to complete the request.</returns>
        public Task Invoke(HttpContext httpContext)
        {
            if (httpContext is null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            // ReSharper disable once UseDeconstruction - not supported in .NET46/.NETS
            KeyValuePair <string, string?> requestHeader = httpContext.Request.Headers.GetCorrelationIdHeader(_acceptedRequestHeaders);

            if (requestHeader.Value != null)
            {
                _logger.LogTrace("Request header '{HeaderName}' found with correlation id '{CorrelationId}'.", requestHeader.Key, requestHeader.Value);
            }

            string?responseHeaderName    = _options.IncludeInResponse ? requestHeader.Key : null;
            var    correlatedHttpRequest = new HttpRequestActivity(_logger, httpContext, responseHeaderName);

            return(_asyncCorrelationManager.CorrelateAsync(
                       requestHeader.Value,
                       () =>
            {
                correlatedHttpRequest.Start(_correlationContextAccessor.CorrelationContext);
                return _next(httpContext)
                .ContinueWith(t =>
                {
                    correlatedHttpRequest.Stop();
                    return t;
                }, TaskScheduler.Current)
                .Unwrap();
            }));
        }
        public async Task When_no_header_name_is_provided_and_context_has_started_response_should_not_contain_header()
        {
            var sut = new HttpRequestActivity(_logger, _httpContext, null);

            // Act
            sut.Start(_correlationContext);
            await _responseFeature.FireOnSendingHeadersAsync();

            // Assert
            _httpContext.Response.Headers.Should().BeEmpty();
        }
        /// <summary>
        /// Invokes the middleware for the current <paramref name="httpContext"/>.
        /// </summary>
        /// <param name="httpContext">The current <see cref="HttpContext"/>.</param>
        /// <returns>An awaitable to wait for to complete the request.</returns>
        public Task Invoke(HttpContext httpContext)
        {
            KeyValuePair <string, string> requestHeader = httpContext.Request.Headers.GetCorrelationIdHeader(_acceptedRequestHeaders);

            if (requestHeader.Value != null)
            {
                _logger.LogTrace("Request header '{HeaderName}' found with correlation id '{CorrelationId}'.", requestHeader.Key, requestHeader.Value);
            }

            string responseHeaderName    = _options.IncludeInResponse ? requestHeader.Key : null;
            var    correlatedHttpRequest = new HttpRequestActivity(_logger, httpContext, responseHeaderName);

            return(_correlationManager.CorrelateInternalAsync(
                       requestHeader.Value,
                       correlatedHttpRequest,
                       () => _next(httpContext)
                       ));
        }
        public HttpRequestActivityTests()
        {
            _httpContext = new DefaultHttpContext();
            _httpContext.Features.Set <IHttpResponseFeature>(_responseFeature = new TestResponseFeature());

            Logger serilog = new LoggerConfiguration()
                             .MinimumLevel.Verbose()
                             .WriteTo.TestCorrelator()
                             .CreateLogger();

            var serilogProvider = new SerilogLoggerProvider(serilog);

            _logger = serilogProvider.CreateLogger("");

            _sut = new HttpRequestActivity(_logger, _httpContext, CorrelationHttpHeaders.CorrelationId);
            _correlationContext = new CorrelationContext
            {
                CorrelationId = Guid.NewGuid().ToString()
            };
        }