public void When_correlation_is_set_with_a_predefined_correlation_id_Then_it_should_always_return_it()
        {
            var correlationId1 = Guid.NewGuid();
            var correlationId2 = Guid.NewGuid();

            var thread1 = Task <Guid?> .Factory.StartNew(() =>
            {
                CorrelationState.InitializeCorrelation(correlationId1);

                var correlationid = CorrelationState.GetCurrentCorrelationId();

                CorrelationState.ClearCorrelation();

                return(correlationid);
            });

            var thread2 = Task <Guid?> .Factory.StartNew(() =>
            {
                CorrelationState.InitializeCorrelation(correlationId2);

                var correlationid = CorrelationState.GetCurrentCorrelationId();

                CorrelationState.ClearCorrelation();

                return(correlationid);
            });

            Task.WaitAll(thread1, thread2);

            Assert.Equal(correlationId1, thread1.Result);
            Assert.Equal(correlationId2, thread2.Result);
        }
        public void When_correlation_is_access_different_threads_It_returns_different_ids()
        {
            var thread1 = Task <Guid?> .Factory.StartNew(() =>
            {
                CorrelationState.InitializeCorrelation();

                var correlationid = CorrelationState.GetCurrentCorrelationId();

                CorrelationState.ClearCorrelation();

                return(correlationid);
            });

            var thread2 = Task <Guid?> .Factory.StartNew(() =>
            {
                CorrelationState.InitializeCorrelation();

                var correlationid = CorrelationState.GetCurrentCorrelationId();

                CorrelationState.ClearCorrelation();

                return(correlationid);
            });

            Task.WaitAll(thread1, thread2);

            Assert.NotEqual(thread1.Result, thread2.Result);
        }
Exemplo n.º 3
0
        public override Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
        {
            dynamic response = actionExecutedContext.ActionContext.Response?.Content;

            // Response can be null if we respond with streamed data (i.e. csv/pdf export)
            if (response != null)
            {
                // This method is always executed after each request but we can't guarantee
                // the thread context. If correlation Id is set in another thread in case
                // of exception - correlation id will not be set for this thread and we
                // shouldn't replace it.
                try
                {
                    if (string.IsNullOrEmpty(response.Value.CorrelationId))
                    {
                        response.Value.CorrelationId = CorrelationState.GetCurrentCorrelationId()?.ToString();
                    }

                    actionExecutedContext.ActionContext.Response.Content = response;
                }
                catch (RuntimeBinderException)
                {
                }
            }

            return(base.OnActionExecutedAsync(actionExecutedContext, cancellationToken));
        }
        public async Task When_correlation_id_is_set_outside_an_async_call_then_the_correlation_id_is_available_inside_the_async_call()
        {
            var expectedCorrelationId = Guid.NewGuid();

            CorrelationState.InitializeCorrelation(expectedCorrelationId);

            var correlationId = await Task.Run(() => CorrelationState.GetCurrentCorrelationId());

            Assert.Equal(expectedCorrelationId, correlationId);
        }
        public void When_correlation_is_uninitialized_Then_it_returns_null_when_asked_for_current_correlation_id()
        {
            var thread = Task <Guid?> .Factory.StartNew(() =>
            {
                CorrelationState.ClearCorrelation();
                return(CorrelationState.GetCurrentCorrelationId());
            });

            Task.WaitAll(thread);

            Assert.Null(thread.Result);
        }
        public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
        {
            var currentCorrelationId = CorrelationState.GetCurrentCorrelationId();

            if (currentCorrelationId.HasValue)
            {
                var correlationDictionary = CorrelationState.GetCorrelationValues().ToDictionary(c => c.Key, c => c.Value);

                correlationDictionary["CorrelationId"] = currentCorrelationId.Value;

                logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("Correlation", correlationDictionary, destructureObjects: true));
                logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("CorrelationId", currentCorrelationId.Value));
            }
        }
Exemplo n.º 7
0
 public async Task <HttpResponseMessage> Get()
 {
     return(await Task.Run(() => Request.CreateResponse(HttpStatusCode.OK, CorrelationState.GetCurrentCorrelationId())));
 }
 public async Task <string> Get()
 {
     return(await Task.Run(() => CorrelationState.GetCurrentCorrelationId()?.ToString()));
 }
 public ActionResult Index()
 {
     return(View(CorrelationState.GetCurrentCorrelationId()));
 }