public void When_correlation_is_not_initialized_then_it_forgets_the_correlation_keyvalues()
        {
            CorrelationState.TryAddOrUpdateCorrelationValue(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
            var values = CorrelationState.GetCorrelationValues();

            Assert.False(values.Any());
        }
        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));
            }
        }
        public async Task When_correlation_values_is_set_outside_an_async_call_then_the_correlation_values_are_available_inside_the_async_call()
        {
            var key   = Guid.NewGuid().ToString();
            var value = Guid.NewGuid().ToString();

            CorrelationState.InitializeCorrelation();
            CorrelationState.TryAddOrUpdateCorrelationValue(key, value);

            var correlationValues = (await Task.Run(() => CorrelationState.GetCorrelationValues())).ToList();

            Assert.Equal(1, correlationValues.Count);

            var firstCorrelationValue = correlationValues.First();

            Assert.Equal(key, firstCorrelationValue.Key);
            Assert.Equal(value, firstCorrelationValue.Value);
        }
        public void When_correlation_is_initialized_and_then_cleared_then_it_forgets_the_correlation_keyvalues()
        {
            var thread = Task <IEnumerable <KeyValuePair <string, object> > > .Factory.StartNew(() =>
            {
                CorrelationState.InitializeCorrelation();

                CorrelationState.TryAddOrUpdateCorrelationValue(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

                CorrelationState.ClearCorrelation();

                return(CorrelationState.GetCorrelationValues());
            });

            Task.WaitAll(thread);

            Assert.False(thread.Result.Any());
        }
        public void When_correlation_is_initialized_then_correlation_dictionary_accepts_new_correlation_keyvalues()
        {
            var key           = Guid.NewGuid().ToString();
            var expectedValue = Guid.NewGuid().ToString();

            var thread = Task <IEnumerable <KeyValuePair <string, object> > > .Factory.StartNew(() =>
            {
                CorrelationState.InitializeCorrelation();

                CorrelationState.TryAddOrUpdateCorrelationValue(key, expectedValue);

                return(CorrelationState.GetCorrelationValues());
            });

            Task.WaitAll(thread);

            var actualValue = thread.Result.SingleOrDefault(c => c.Key == key);

            Assert.Equal(expectedValue, actualValue.Value);
        }