public void TagCollections_IndexerGet_CaseInsensitive()
        {
            using (var scope = TagScopeManager.CreateScope())
            {
                scope.Tags.Add("TaG", "VaLuE");

                Assert.AreEqual("VaLuE", scope.Tags["tAg"]);
                Assert.AreEqual("VaLuE", scope.Tags["TaG"]);
                Assert.AreEqual("VaLuE", scope.Tags["tag"]);
                Assert.AreEqual("VaLuE", scope.Tags["TAG"]);

                TestWithNestedScope();

                scope.Tags.Clear();
                scope.AssertOnTagCollectionCounts(0, 0, 0);

                scope.Tags["TaG"] = "VaLuE";

                Assert.AreEqual("VaLuE", scope.Tags["tAg"]);
                Assert.AreEqual("VaLuE", scope.Tags["TaG"]);
                Assert.AreEqual("VaLuE", scope.Tags["tag"]);
                Assert.AreEqual("VaLuE", scope.Tags["TAG"]);

                TestWithNestedScope();
            }
        }
Exemple #2
0
        public void TagCollections_TagValueInParentScopeMaskedWithNewTagValue()
        {
            const string tagKey = "tag";

            using (var scope1 = TagScopeManager.CreateScope())
            {
                scope1.Tags.Add(tagKey, "1");
                Assert.AreEqual("1", scope1.Tags[tagKey]);
                Assert.AreEqual("1", scope1.EffectiveTags[tagKey]);

                using (var scope2 = TagScopeManager.CreateScope())
                {
                    scope2.Tags.Add(tagKey, "2");
                    Assert.AreEqual("2", scope2.Tags[tagKey]);
                    Assert.AreEqual("1", scope2.InheritedTags[tagKey]);
                    Assert.AreEqual("2", scope2.EffectiveTags[tagKey]);

                    using (var scope3 = TagScopeManager.CreateScope())
                    {
                        scope3.Tags.Add(tagKey, "3");
                        Assert.AreEqual("3", scope3.Tags[tagKey]);
                        Assert.AreEqual("2", scope3.InheritedTags[tagKey]);
                        Assert.AreEqual("3", scope3.EffectiveTags[tagKey]);
                    }
                }
            }
        }
 public void TagologExplicitOperationOnThreadCorrelationId()
 {
     using (var context = TagScopeManager.CreateScope())
     {
         ExplicitOperations(context);
     }
 }
Exemple #4
0
        /// <summary>
        /// This fragment implemented as a separate method
        /// to isolate nested scope from parent scope
        /// and prevent some really annoying problems with
        /// mess with acident "scope" and "nestedScope" calls
        /// while they are in one method.
        /// </summary>
        static void TestWithNestedScope()
        {
            // ReSharper disable AccessToDisposedClosure

            using (var nestedScope = TagScopeManager.CreateScope())
            {
                // Access to tag that does not exist.
                // Expected result: ArgumentException( "The given tag key was not present in the tag collection." )
                Assert.IsTrue(ExceptionTest.IsThrown <ArgumentException>(() =>
                {
                    Assert.IsNull(nestedScope.Tags["not_a_tag"]);
                }));

                Assert.IsTrue(ExceptionTest.IsThrown <ArgumentException>(() =>
                {
                    Assert.IsNull(nestedScope.InheritedTags["not_a_tag"]);
                }));

                Assert.IsTrue(ExceptionTest.IsThrown <ArgumentException>(() =>
                {
                    Assert.IsNull(nestedScope.EffectiveTags["not_a_tag"]);
                }));
            }

            // ReSharper restore AccessToDisposedClosure
        }
Exemple #5
0
        /// <summary>
        /// This fragment implemented as a separate method
        /// to isolate nested scope from parent scope
        /// and prevent some really annoying problems with
        /// mess with acident "scope" and "nestedScope" calls
        /// while they are in one method.
        /// </summary>
        static void TestWithNestedScope()
        {
            using ( var nestedScope = TagScopeManager.CreateScope() )
            {
                nestedScope.Tags.Add( "tag3", "value3" );
                nestedScope.Tags.Add( "tag4", "value4" );

                // Tags collection.
                var keys = nestedScope.Tags.Keys;
                Assert.AreEqual( 2, keys.Count );
                Assert.IsTrue( keys.Contains( "tag3" ) );
                Assert.IsTrue( keys.Contains( "tag4" ) );

                // Inherited collection.
                keys = nestedScope.InheritedTags.Keys;
                Assert.AreEqual( 2, keys.Count );
                Assert.IsTrue( keys.Contains( "tag1" ) );
                Assert.IsTrue( keys.Contains( "tag2" ) );

                // Effective collection.
                keys = nestedScope.EffectiveTags.Keys;
                Assert.AreEqual( 4, keys.Count );
                Assert.IsTrue( keys.Contains( "tag1" ) );
                Assert.IsTrue( keys.Contains( "tag2" ) );
                Assert.IsTrue( keys.Contains( "tag3" ) );
                Assert.IsTrue( keys.Contains( "tag4" ) );
            }
        }
Exemple #6
0
        public void TagCollections_KeysCollection()
        {
            using ( var scope = TagScopeManager.CreateScope() )
            {
                var keys = scope.Tags.Keys;
                Assert.AreEqual( 0, keys.Count );

                scope.Tags.Add( "tag1", "value1" );
                scope.Tags.Add( "tag2", "value2" );

                // Tags collection.
                keys = scope.Tags.Keys;
                Assert.AreEqual( 2, keys.Count );
                Assert.IsTrue( keys.Contains( "tag1" ) );
                Assert.IsTrue( keys.Contains( "tag2" ) );

                // Inherited collection.
                keys = scope.InheritedTags.Keys;
                Assert.AreEqual( 0, keys.Count );

                // Effective collection.
                keys = scope.EffectiveTags.Keys;
                Assert.AreEqual( 2, keys.Count );
                Assert.IsTrue( keys.Contains( "tag1" ) );
                Assert.IsTrue( keys.Contains( "tag2" ) );

                TestWithNestedScope();
            }
        }
 public void LogAdapter_EmptyScopeNoTagsInLogAdapter()
 {
     using (TagScopeManager.CreateScope())
     {
         Assert.AreEqual(0, LogAdapter.Tags.Count);
     }
 }
Exemple #8
0
        void OnTimerElapsed(object sender, ElapsedEventArgs e)
        {
            using (var scope = TagScopeManager.CreateScope())
            {
                try
                {
                    scope.Tags[AppTag.Application]    = DataCenter.ApplicationName;
                    scope.Tags[AppTag.VirtualMachine] = Name;

                    var random = RandomHelper.GenerateInt(0, 100);
                    if (random < 30)
                    {
                        _logger.Info("Low memory and CPU usage.");
                    }
                    if (random < 60)
                    {
                        _logger.Warn("Medium memory and CPU usage.");
                    }
                    else
                    {
                        throw new InvalidOperationException("High memory and CPU usage.");
                    }
                }
                catch (InvalidOperationException ex)
                {
                    _logger.Error(ex.Message, ex);
                }
            }
        }
Exemple #9
0
        public void TagCollections_IndexerGet_ThrowsOnTagKeyThatDoesNotExistTest()
        {
            // ReSharper disable AccessToDisposedClosure

            using (var scope = TagScopeManager.CreateScope())
            {
                scope.Tags.Add("tag", "value");

                // Access to existing tag. Expected result: success.
                Assert.AreEqual("value", scope.Tags["tag"]);

                // Access to tag that does not exist.
                // Expected result: ArgumentException( "The given tag key was not present in the tag collection." )
                Assert.IsTrue(ExceptionTest.IsThrown <ArgumentException>(() =>
                {
                    Assert.IsNull(scope.Tags["not_a_tag"]);
                }));

                Assert.IsTrue(ExceptionTest.IsThrown <ArgumentException>(() =>
                {
                    Assert.IsNull(scope.InheritedTags["not_a_tag"]);
                }));

                Assert.IsTrue(ExceptionTest.IsThrown <ArgumentException>(() =>
                {
                    Assert.IsNull(scope.EffectiveTags["not_a_tag"]);
                }));

                TestWithNestedScope();
            }

            // ReSharper restore AccessToDisposedClosure
        }
Exemple #10
0
        public void TagCollections_IndexerGet_ThrowsOnTagKeyThatIsNull()
        {
            using (var scope = TagScopeManager.CreateScope())
            {
                scope.Tags.Add("tag", "value");

                // Access to existing tag. Expected result: success.
                Assert.AreEqual("value", scope.Tags["tag"]);

                // Access to tag that does not exist.
                // Expected result: ArgumentNullException( "Tag key cannot be null." )
                Assert.IsTrue(ExceptionTest.IsThrown <ArgumentNullException>(() =>
                {
                    Assert.IsNull(scope.Tags[null]);
                }));

                Assert.IsTrue(ExceptionTest.IsThrown <ArgumentNullException>(() =>
                {
                    Assert.IsNull(scope.InheritedTags[null]);
                }));

                Assert.IsTrue(ExceptionTest.IsThrown <ArgumentNullException>(() =>
                {
                    Assert.IsNull(scope.EffectiveTags[null]);
                }));

                TestWithNestedScope();
            }
        }
        public void TagCollections_InheritedTagsContainsTagValueFromParentScope()
        {
            const string tagKey = "tag";

            using (var scope1 = TagScopeManager.CreateScope())
            {
                scope1.Tags.Add(tagKey, "1");
                // No tags inherited yet.
                Assert.AreEqual(0, scope1.InheritedTags.Count);

                using (var scope2 = TagScopeManager.CreateScope())
                {
                    scope2.Tags.Add(tagKey, "2");
                    Assert.AreEqual(1, scope2.InheritedTags.Count);

                    using (var scope3 = TagScopeManager.CreateScope())
                    {
                        scope3.Tags.Add(tagKey, "3");
                        Assert.AreEqual(1, scope3.InheritedTags.Count);

                        using (var scope4 = TagScopeManager.CreateScope())
                        {
                            scope4.Tags.Add(tagKey, "4");
                            Assert.AreEqual(1, scope4.InheritedTags.Count);

                            // Inherited collection contains value from parent scope.
                            Assert.AreEqual("1", scope2.InheritedTags[tagKey]);
                            Assert.AreEqual("2", scope3.InheritedTags[tagKey]);
                            Assert.AreEqual("3", scope4.InheritedTags[tagKey]);
                        }
                    }
                }
            }
        }
Exemple #12
0
        /// <summary>
        /// This fragment implemented as a separate method
        /// to isolate nested scope from parent scope
        /// and prevent some really annoying problems with
        /// mess with acident "scope" and "nestedScope" calls
        /// while they are in one method.
        /// </summary>
        static void TestWithNestedScope()
        {
            using (var nestedScope = TagScopeManager.CreateScope(new Dictionary <string, string>
            {
                { "tag5", "value5" },
                { "tag6", "value6" }
            }))
            {
                // Use IEnumerable on InheritedTags collection.
                var dictionary = nestedScope.InheritedTags.ToDictionary(_ => _.Key, _ => _.Value);
                Assert.AreEqual(4, dictionary.Count);

                // Check if all tags were converted to dictionary through IEnumerable.
                Assert.AreEqual("value1", dictionary["tag1"]);
                Assert.AreEqual("value2", dictionary["tag2"]);
                Assert.AreEqual("value3", dictionary["tag3"]);
                Assert.AreEqual("value4", dictionary["tag4"]);

                // Use IEnumerable on EffectiveTags collection.
                dictionary = nestedScope.EffectiveTags.ToDictionary(_ => _.Key, _ => _.Value);
                Assert.AreEqual(6, dictionary.Count);

                // Check if all tags were converted to dictionary through IEnumerable.
                Assert.AreEqual("value1", dictionary["tag1"]);
                Assert.AreEqual("value2", dictionary["tag2"]);
                Assert.AreEqual("value3", dictionary["tag3"]);
                Assert.AreEqual("value4", dictionary["tag4"]);
                Assert.AreEqual("value5", dictionary["tag5"]);
                Assert.AreEqual("value6", dictionary["tag6"]);
            }
        }
Exemple #13
0
 static ITagScope CreateTagScope(string operation)
 {
     return(TagScopeManager.CreateScope(new Dictionary <string, string>
     {
         { AppTag.Application, "WebInterface" },
         { AppTag.Operation, operation }
     }));
 }
Exemple #14
0
        public void TagCollections_RemoveCaseInsensitive()
        {
            using (var scope = TagScopeManager.CreateScope())
            {
                scope.Tags.Add("TaG", "value");
                scope.AssertOnTagCollectionCounts(1, 0, 1);

                scope.Tags.Remove("tAg");
                scope.AssertOnTagCollectionCounts(0, 0, 0);
            }
        }
Exemple #15
0
        static void CreateEmptyScope()
        {
            using (var scope = TagScopeManager.CreateScope())
            {
                scope.AssertOnTagCollectionCounts(0, 0, 0);

                using (var nestedScope = TagScopeManager.CreateScope())
                {
                    nestedScope.AssertOnTagCollectionCounts(0, 0, 0);
                }
            }
        }
Exemple #16
0
        public void TagCollections_IndexerAsAdd()
        {
            using (var scope = TagScopeManager.CreateScope())
            {
                Assert.AreEqual(0, scope.Tags.Count);

                scope.Tags["tag"] = "value";

                Assert.AreEqual(1, scope.Tags.Count);
                Assert.AreEqual("value", scope.Tags["tag"]);
            }
        }
        public void TagCollections_GetOriginalCaseSensitiveTagKey()
        {
            using (var scope = TagScopeManager.CreateScope())
            {
                scope.Tags.Add("TaG", "VaLuE");

                var pair = scope.Tags.First();
                Assert.AreEqual("TaG", pair.Key);

                TestWithNestedScope();
            }
        }
Exemple #18
0
        static void CreateEmptyScope()
        {
            using (var scope = TagScopeManager.CreateScope())
            {
                Assert.AreEqual(0, scope.Tags.Count);

                scope.Tags.Add("tag1", "value1");
                Assert.AreEqual(1, scope.Tags.Count);

                scope.Tags.Add("tag2", "value2");
                Assert.AreEqual(2, scope.Tags.Count);
            }
        }
Exemple #19
0
        public void TagCollections_Clear()
        {
            using (var scope = TagScopeManager.CreateScope())
            {
                scope.Tags.Add("tag1", "value1");
                scope.Tags.Add("tag2", "value2");

                scope.AssertOnTagCollectionCounts(2, 0, 2);

                scope.Tags.Clear();

                scope.AssertOnTagCollectionCounts(0, 0, 0);
            }
        }
Exemple #20
0
        /// <summary>
        /// This fragment implemented as a separate method
        /// to isolate nested scope from parent scope
        /// and prevent some really annoying problems with
        /// mess with acident "scope" and "nestedScope" calls
        /// while they are in one method.
        /// </summary>
        static void TestWithNestedScope()
        {
            using (var nestedScope = TagScopeManager.CreateScope())
            {
                var keys = nestedScope.Tags.Keys;
                keys.AssertIsCollectionReadonly();

                keys = nestedScope.InheritedTags.Keys;
                keys.AssertIsCollectionReadonly();

                keys = nestedScope.EffectiveTags.Keys;
                keys.AssertIsCollectionReadonly();
            }
        }
        /// <summary>
        /// This fragment implemented as a separate method
        /// to isolate nested scope from parent scope
        /// and prevent some really annoying problems with
        /// mess with acident "scope" and "nestedScope" calls
        /// while they are in one method.
        /// </summary>
        void TestWithNestedScope()
        {
            using (var nestedScope = TagScopeManager.CreateScope())
            {
                nestedScope.Tags["tag3"] = "value3";
                nestedScope.Tags["tag4"] = "value4";

                Assert.AreEqual(4, LogAdapter.Tags.Count);
                Assert.AreEqual("value1", LogAdapter.Tags["tag1"]);
                Assert.AreEqual("value2", LogAdapter.Tags["tag2"]);
                Assert.AreEqual("value3", LogAdapter.Tags["tag3"]);
                Assert.AreEqual("value4", LogAdapter.Tags["tag4"]);
            }
        }
        public void TagCollections_ContainsKeyCaseInsensitive()
        {
            using (var scope = TagScopeManager.CreateScope())
            {
                scope.Tags.Add("TaG", "VaLuE");

                Assert.IsTrue(scope.Tags.ContainsKey("tAg"));
                Assert.IsTrue(scope.Tags.ContainsKey("TaG"));
                Assert.IsTrue(scope.Tags.ContainsKey("tag"));
                Assert.IsTrue(scope.Tags.ContainsKey("TAG"));

                TestWithNestedScope();
            }
        }
        public void TagScopeManager_CreateScope_AsEmpty()
        {
            using (var scope = TagScopeManager.CreateScope())
            {
                scope.AssertOnTagCollectionCounts(0, 0, 0);

                using (var nestedScope = TagScopeManager.CreateScope())
                {
                    nestedScope.AssertOnTagCollectionCounts(0, 0, 0);
                }

                scope.AssertOnTagCollectionCounts(0, 0, 0);
            }
        }
        /// <summary>
        /// This fragment implemented as a separate method
        /// to isolate nested scope from parent scope
        /// and prevent some really annoying problems with
        /// mess with acident "scope" and "nestedScope" calls
        /// while they are in one method.
        /// </summary>
        static void TestWithNestedScope()
        {
            using (var nestedScope = TagScopeManager.CreateScope())
            {
                // Inherited collection.
                var keys = nestedScope.InheritedTags.Keys;
                Assert.IsTrue(keys.Contains("TaG"));
                Assert.IsFalse(keys.Contains("tAg"));

                // Effective collection.
                keys = nestedScope.EffectiveTags.Keys;
                Assert.IsTrue(keys.Contains("TaG"));
                Assert.IsFalse(keys.Contains("tAg"));
            }
        }
        /// <summary>
        /// This fragment implemented as a separate method
        /// to isolate nested scope from parent scope
        /// and prevent some really annoying problems with
        /// mess with acident "scope" and "nestedScope" calls
        /// while they are in one method.
        /// </summary>
        static void TestWithNestedScope()
        {
            using (var nestedScope = TagScopeManager.CreateScope())
            {
                // Test on Inherited and Effective collections also.

                // Inherited collection.
                var pair = nestedScope.InheritedTags.First();
                Assert.AreEqual("TaG", pair.Key);

                // Effective collection.
                pair = nestedScope.EffectiveTags.First();
                Assert.AreEqual("TaG", pair.Key);
            }
        }
 /// <summary>
 /// This fragment implemented as a separate method
 /// to isolate nested scope from parent scope
 /// and prevent some really annoying problems with
 /// mess with acident "scope" and "nestedScope" calls
 /// while they are in one method.
 /// </summary>
 void CreateScopeInvalidatesLogAdapter_TestWithNestedScope()
 {
     using (TagScopeManager.CreateScope(new Dictionary <string, string>
     {
         { "tag3", "value3" },
         { "tag4", "value4" }
     }))
     {
         Assert.AreEqual(4, LogAdapter.Tags.Count);
         Assert.AreEqual("value1", LogAdapter.Tags["tag1"]);
         Assert.AreEqual("value2", LogAdapter.Tags["tag2"]);
         Assert.AreEqual("value3", LogAdapter.Tags["tag3"]);
         Assert.AreEqual("value4", LogAdapter.Tags["tag4"]);
     }
 }
        protected static void Run(string loggerName, Action <string> runAction, bool checkTagValues = true)
        {
            var logger = LogManager.GetLogger(loggerName);

            Assert.That(LogManager.Configuration.AllTargets.Count, Is.GreaterThan(0),
                        "NLog targets were NOT found in the configuration.");

            var target = LogManager.Configuration.FindTargetByName(loggerName);

            Assert.True(null != target, "NLog MemoryTarget was not found by name '{0}'.", loggerName);
            Assert.True(target is MemoryTarget, "NLog target is not of type MemoryTarget.");

            var memoryTarget = ( MemoryTarget )target;

            using (var tagScope = TagScopeManager.CreateScope())
            {
                tagScope.Tags["Tag1"] = "Value1";
                tagScope.Tags["Tag2"] = "Value2";

                logger.Debug("Log message.");

                Assert.That(memoryTarget.Logs.Count, Is.EqualTo(1),
                            "The only row should be logged in the MemoryTarget.");

                var jsonString = memoryTarget.Logs[0];

                try
                {
                    runAction.Invoke(jsonString);

                    dynamic json = JToken.Parse(jsonString);

                    if (checkTagValues)
                    {
                        var value1 = ( string )json.Tag1;
                        var value2 = ( string )json.Tag2;

                        Assert.That(value1, Is.EqualTo("Value1"), "Tag1 value was not found.");
                        Assert.That(value2, Is.EqualTo("Value2"), "Tag2 value was not found.");
                    }
                }
                catch (JsonReaderException ex)
                {
                    var message = string.Format("Wrong Json string: {0}", jsonString);
                    throw new ArgumentException(message, ex);
                }
            }
        }
Exemple #28
0
        static void NestedScopesWithTags()
        {
            using (var scope1 = TagScopeManager.CreateScope(new Dictionary <string, string>
            {
                { "scope1_tag1", "scope1_tag1_value" },
                { "scope1_tag2", "scope1_tag2_value" }
            }))
            {
                // Expected counts: tags, inherited, effective.
                scope1.AssertOnTagCollectionCounts(2, 0, 2);

                using (var scope2 = TagScopeManager.CreateScope(new Dictionary <string, string>
                {
                    { "scope1_scope2_tag1", "scope1_scope2_tag1_value" },
                    { "scope1_scope2_tag2", "scope1_scope2_tag2_value" }
                }))
                {
                    // Expected counts: tags, inherited, effective.
                    scope2.AssertOnTagCollectionCounts(2, 2, 4);

                    using (var scope3 = TagScopeManager.CreateScope(new Dictionary <string, string>
                    {
                        { "scope1_scope2_scope3_tag1", "scope1_scope2_scope3_tag1_value" },
                        { "scope1_scope2_scope3_tag2", "scope1_scope2_scope3_tag1_value" }
                    }))
                    {
                        // Expected counts: tags, inherited, effective.
                        scope3.AssertOnTagCollectionCounts(2, 4, 6);

                        scope3.Tags.Add("scope1_scope2_scope3_tag3", "scope1_scope2_scope3_tag3_value");
                        scope3.AssertOnTagCollectionCounts(3, 4, 7);

                        scope3.Tags.Add("scope1_scope2_scope3_tag4", "scope1_scope2_scope3_tag4_value");
                        scope3.AssertOnTagCollectionCounts(4, 4, 8);

                        // Modify tags in upper scopes.
                        scope2.Tags.Add("scope1_scope2_tag3", "scope1_scope2_tag3_value");
                        scope3.AssertOnTagCollectionCounts(4, 5, 9);

                        scope1.Tags.Add("scope1_tag3", "scope1_tag3_value");
                        scope3.AssertOnTagCollectionCounts(4, 6, 10);
                    }

                    // Expected counts: tags, inherited, effective.
                    scope2.AssertOnTagCollectionCounts(3, 3, 6);
                }
            }
        }
Exemple #29
0
        public void TagCollections_Remove()
        {
            using (var scope = TagScopeManager.CreateScope())
            {
                scope.Tags.Add("tag1", "value1");
                scope.Tags.Add("tag2", "value2");
                scope.AssertOnTagCollectionCounts(2, 0, 2);

                scope.Tags.Remove("tag2");

                scope.AssertOnTagCollectionCounts(1, 0, 1);
                Assert.IsTrue(scope.Tags.ContainsKey("tag1"));

                scope.Tags.Remove("tag1");
                scope.AssertOnTagCollectionCounts(0, 0, 0);
            }
        }
Exemple #30
0
        static void CreateScopeWithTags()
        {
            using (var scope = TagScopeManager.CreateScope(new Dictionary <string, string>
            {
                { "tag1", "value1" },
                { "tag2", "value2" }
            }))
            {
                Assert.AreEqual(2, scope.Tags.Count);

                scope.Tags.Add("tag3", "value3");
                Assert.AreEqual(3, scope.Tags.Count);

                scope.Tags.Add("tag4", "value4");
                Assert.AreEqual(4, scope.Tags.Count);
            }
        }