예제 #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();
            }));
        }
예제 #2
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)
        {
            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();
            }));
        }
예제 #3
0
        /// <summary>
        /// Executes the <paramref name="correlatedTask"/> with its own <see cref="CorrelationContext"/>.
        /// </summary>
        /// <typeparam name="T">The return type of the awaitable task.</typeparam>
        /// <param name="asyncCorrelationManager">The async correlation manager.</param>
        /// <param name="correlatedTask">The task to execute.</param>
        /// <param name="onException">A delegate to handle the exception inside the correlation scope, before it is disposed. Returns <see langword="true" /> to consider the exception handled, or <see langword="false" /> to throw.</param>
        /// <returns>An awaitable that completes with a result <typeparamref name="T"/>  once the <paramref name="correlatedTask"/> has executed and the correlation context has disposed.</returns>
        /// <remarks>
        /// When logging and tracing are both disabled, no correlation context is created and the task simply executed as it normally would.
        /// </remarks>
        public static Task <T> CorrelateAsync <T>(this IAsyncCorrelationManager asyncCorrelationManager, Func <Task <T> > correlatedTask, OnException <T>?onException)
        {
            if (asyncCorrelationManager is null)
            {
                throw new ArgumentNullException(nameof(asyncCorrelationManager));
            }

            return(asyncCorrelationManager.CorrelateAsync(null, correlatedTask, onException));
        }
예제 #4
0
        /// <summary>
        /// Executes the <paramref name="correlatedTask"/> with its own <see cref="CorrelationContext"/>.
        /// </summary>
        /// <param name="asyncCorrelationManager">The async correlation manager.</param>
        /// <param name="correlationId">The correlation id to use, or null to generate a new one.</param>
        /// <param name="correlatedTask">The task to execute.</param>
        /// <returns>An awaitable that completes once the <paramref name="correlatedTask"/> has executed and the correlation context has disposed.</returns>
        /// <remarks>
        /// When logging and tracing are both disabled, no correlation context is created and the task simply executed as it normally would.
        /// </remarks>
        public static Task CorrelateAsync(this IAsyncCorrelationManager asyncCorrelationManager, string?correlationId, Func <Task> correlatedTask)
        {
            if (asyncCorrelationManager is null)
            {
                throw new ArgumentNullException(nameof(asyncCorrelationManager));
            }

            return(asyncCorrelationManager.CorrelateAsync(correlationId, correlatedTask, null));
        }
예제 #5
0
        public Task Process(IncomingStepContext context, Func <Task> next)
        {
            Message message = context.Load <Message>();

            message.Headers.TryGetValue(Headers.CorrelationId, out string?correlationId);
            if (correlationId != null)
            {
                _logger.Debug("Correlation ID: {CorrelationId}", correlationId);
            }
            // If id is null, we just let manager assign new one.
            return(_asyncCorrelationManager.CorrelateAsync(correlationId, next));
        }
예제 #6
0
 /// <summary>
 /// Executes the <paramref name="correlatedTask"/> with its own <see cref="CorrelationContext"/>.
 /// </summary>
 /// <typeparam name="T">The return type of the awaitable task.</typeparam>
 /// <param name="asyncCorrelationManager">The async correlation manager.</param>
 /// <param name="correlationId">The correlation id to use, or null to generate a new one.</param>
 /// <param name="correlatedTask">The task to execute.</param>
 /// <returns>An awaitable that completes with a result <typeparamref name="T"/> once the <paramref name="correlatedTask"/> has executed and the correlation context has disposed.</returns>
 /// <remarks>
 /// When logging and tracing are both disabled, no correlation context is created and the task simply executed as it normally would.
 /// </remarks>
 public static Task <T> CorrelateAsync <T>(this IAsyncCorrelationManager asyncCorrelationManager, string correlationId, Func <Task <T> > correlatedTask)
 {
     return(asyncCorrelationManager.CorrelateAsync(correlationId, correlatedTask, null));
 }
예제 #7
0
 /// <summary>
 /// Executes the <paramref name="correlatedTask"/> with its own <see cref="CorrelationContext"/>.
 /// </summary>
 /// <typeparam name="T">The return type of the awaitable task.</typeparam>
 /// <param name="asyncCorrelationManager">The async correlation manager.</param>
 /// <param name="correlatedTask">The task to execute.</param>
 /// <param name="onException">A delegate to handle the exception inside the correlation scope, before it is disposed. Returns <see langword="true" /> to consider the exception handled, or <see langword="false" /> to throw.</param>
 /// <returns>An awaitable that completes with a result <typeparamref name="T"/>  once the <paramref name="correlatedTask"/> has executed and the correlation context has disposed.</returns>
 /// <remarks>
 /// When logging and tracing are both disabled, no correlation context is created and the task simply executed as it normally would.
 /// </remarks>
 public static Task <T> CorrelateAsync <T>(this IAsyncCorrelationManager asyncCorrelationManager, Func <Task <T> > correlatedTask, OnException <T> onException)
 {
     return(asyncCorrelationManager.CorrelateAsync(null, correlatedTask, onException));
 }
예제 #8
0
 /// <summary>
 /// Executes the <paramref name="correlatedTask"/> with its own <see cref="CorrelationContext"/>.
 /// </summary>
 /// <typeparam name="T">The return type of the awaitable task.</typeparam>
 /// <param name="asyncCorrelationManager">The async correlation manager.</param>
 /// <param name="correlatedTask">The task to execute.</param>
 /// <returns>An awaitable that completes with a result <typeparamref name="T"/> once the <paramref name="correlatedTask"/> has executed and the correlation context has disposed.</returns>
 /// <remarks>
 /// When logging and tracing are both disabled, no correlation context is created and the task simply executed as it normally would.
 /// </remarks>
 public static Task <T> CorrelateAsync <T>(this IAsyncCorrelationManager asyncCorrelationManager, Func <Task <T> > correlatedTask)
 {
     return(asyncCorrelationManager.CorrelateAsync(null, correlatedTask));
 }