Exemplo n.º 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();
            }));
        }
Exemplo n.º 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();
            }));
        }
Exemplo n.º 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));
        }
Exemplo n.º 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));
        }
Exemplo n.º 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));
        }
Exemplo n.º 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));
 }
Exemplo n.º 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));
 }
Exemplo n.º 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));
 }