Ejemplo n.º 1
0
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request,
                                                                CancellationToken cancellationToken)
        {
            var tracingContext = _tracingContextProvider.GetTracingContext();

            using (LogContext.PushProperty("TracingContext", tracingContext, true))
            {
                return(base.SendAsync(request, cancellationToken));
            }
        }
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request,
                                                                CancellationToken cancellationToken)
        {
            var context = _tracingContextProvider.GetTracingContext();

            if (context != null)
            {
                var requestId = context.UnitOfWorkId + "." + GenerateRandomString(8);
                request.Headers.Add(TracingContextHeaders.RequestIdHeaderName, requestId);
                request.Headers.Add(TracingContextHeaders.CausationIdHeaderName, context.UnitOfWorkId);
                request.Headers.Add(TracingContextHeaders.FlowIdHeaderName, context.FlowId);
            }
            else
            {
                request.Headers.Add(TracingContextHeaders.RequestIdHeaderName, GenerateRandomString(8));
            }

            return(base.SendAsync(request, cancellationToken));
        }
        /// <summary>
        ///     Asks the service container for the tracingcontext for the current call
        ///     and adds it to the logcontext for this call
        /// </summary>
        /// <param name="context"></param>
        /// <param name="tracingContextProvider"></param>
        /// <returns>Task</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public async Task Invoke(HttpContext context, ITracingContextProvider tracingContextProvider)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (tracingContextProvider == null)
            {
                throw new ArgumentNullException(nameof(tracingContextProvider));
            }

            if (tracingContextProvider.GetTracingContext() is TracingContext tracingContext)
            {
                using (LogContext.PushProperty("TracingContext", tracingContext, true))
                {
                    await _next(context);
                }
            }
            else
            {
                await _next(context);
            }
        }