public void WriteDictionary()
        {
            if (!EventLog.SourceExists("Unit Test"))
            {
                return;
            }

            LogEntry entry = CommonUtil.GetDefaultLogEntry();

            entry.Categories         = new string[] { "DictionaryCategory" };
            entry.Severity           = TraceEventType.Error;
            entry.ExtendedProperties = CommonUtil.GetPropertiesDictionary();

            Logger.Write(entry);

            string expected = CommonUtil.FormattedMessageWithDictionary;
            string actual   = CommonUtil.GetLastEventLogEntryCustom();

            Assert.IsTrue(actual.IndexOf("key1") > -1);
            Assert.IsTrue(actual.IndexOf("value1") > -1);
            Assert.IsTrue(actual.IndexOf("key2") > -1);
            Assert.IsTrue(actual.IndexOf("value2") > -1);
            Assert.IsTrue(actual.IndexOf("key3") > -1);
            Assert.IsTrue(actual.IndexOf("value3") > -1);
        }
Exemple #2
0
        public void WriteDictionary()
        {
            try
            {
                if (!EventLog.SourceExists("Unit Test"))
                {
                    Assert.Inconclusive("Source named 'Unit Test' does not exist.");
                }
            }
            catch (SecurityException ex)
            {
                Assert.Inconclusive("In order to run the tests, please run Visual Studio as Administrator.\r\n{0}", ex.ToString());
            }

            LogEntry entry = CommonUtil.GetDefaultLogEntry();

            entry.Categories         = new string[] { "DictionaryCategory" };
            entry.Severity           = TraceEventType.Error;
            entry.ExtendedProperties = CommonUtil.GetPropertiesDictionary();

            Logger.Write(entry);

            string expected = CommonUtil.FormattedMessageWithDictionary;
            string actual   = CommonUtil.GetLastEventLogEntryCustom();

            Assert.IsTrue(actual.IndexOf("key1") > -1);
            Assert.IsTrue(actual.IndexOf("value1") > -1);
            Assert.IsTrue(actual.IndexOf("key2") > -1);
            Assert.IsTrue(actual.IndexOf("value2") > -1);
            Assert.IsTrue(actual.IndexOf("key3") > -1);
            Assert.IsTrue(actual.IndexOf("value3") > -1);
        }
        public void WriteMessageCategoriesAndDictionaryOverload()
        {
            Logger.Write(message, categories, CommonUtil.GetPropertiesDictionary());

            Assert.AreEqual(message, MockTraceListener.LastEntry.Message, "message");
            Assert.AreEqual(1, MockTraceListener.LastEntry.Categories.Count);
            Assert.IsTrue(MockTraceListener.LastEntry.Categories.Contains(category));
            Assert.AreEqual(CommonUtil.GetPropertiesDictionary().Count,
                            MockTraceListener.LastEntry.ExtendedProperties.Count, "hash count");
        }
        public void WriteMessageAndDictionaryOverload()
        {
            Logger.Write(message, CommonUtil.GetPropertiesDictionary());

            Assert.AreEqual(message, MockTraceListener.LastEntry.Message, "message");
            Assert.AreEqual(CommonUtil.GetPropertiesDictionary().Count,
                            MockTraceListener.LastEntry.ExtendedProperties.Count, "hash count");

            Assert.AreEqual(CommonUtil.GetPropertiesDictionary()["key1"],
                            MockTraceListener.LastEntry.ExtendedProperties["key1"], "hash count");
        }
        public void AddItemsAndDictionaryThenLog()
        {
            Logger.SetContextItem("AppVersion", "1234");
            Logger.SetContextItem("BuildNumber", "5678");
            LogEntry log = new LogEntry();

            log.Categories         = new string[] { "MockCategoryOne" };
            log.ExtendedProperties = CommonUtil.GetPropertiesDictionary();
            Logger.Write(log);
            Assert.AreEqual(5, ((LogEntry)MockTraceListener.LastEntry).ExtendedProperties.Count);
            Assert.AreEqual("1234", ((LogEntry)MockTraceListener.LastEntry).ExtendedProperties["AppVersion"]);
            Assert.AreEqual("5678", ((LogEntry)MockTraceListener.LastEntry).ExtendedProperties["BuildNumber"]);
        }
        public void FlushItems()
        {
            Logger.SetContextItem("AppVersion", "1234");
            Logger.SetContextItem("BuildNumber", "5678");
            LogEntry log = new LogEntry();

            log.Categories         = new string[] { "MockCategoryOne" };
            log.ExtendedProperties = CommonUtil.GetPropertiesDictionary();
            Logger.FlushContextItems();
            Logger.Write(log);
            Assert.AreEqual(3, ((LogEntry)MockTraceListener.LastEntry).ExtendedProperties.Count);
            Assert.IsFalse(((LogEntry)MockTraceListener.LastEntry).ExtendedProperties.ContainsKey("AppVersion"));
            Assert.IsFalse(((LogEntry)MockTraceListener.LastEntry).ExtendedProperties.ContainsKey("BuildNumber"));
        }
        public void WriteDictionaryWithHeaderAndFooter()
        {
            LogEntry entry = CommonUtil.GetDefaultLogEntry();

            entry.Categories         = new string[] { "DictionaryCategoryForFlatFile" };
            entry.Severity           = TraceEventType.Error;
            entry.ExtendedProperties = CommonUtil.GetPropertiesDictionary();

            Logger.Write(entry);
            Logger.Reset();

            string expected = CommonUtil.FormattedMessageWithDictionary;
            string actual   = GetFileContents(CommonUtil.FileName);

            Assert.IsTrue(actual.IndexOf("key1") > -1);
            Assert.IsTrue(actual.IndexOf("value1") > -1);
            Assert.IsTrue(actual.IndexOf("key2") > -1);
            Assert.IsTrue(actual.IndexOf("value2") > -1);
            Assert.IsTrue(actual.IndexOf("key3") > -1);
            Assert.IsTrue(actual.IndexOf("value3") > -1);
        }