public void WhenDefaultColorMappingForCritical()
        {
            var consoleOutputInterceptor = new MockConsoleOutputInterceptor();
            var formatter = new EventTextFormatter(EventTextFormatter.DashSeparator);
            var logger = MockConsoleListenerEventSource.Logger;

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    listener.LogToConsole();
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    logger.Critical("This is to log critical in Console");
                });

            var entry = Regex.Split(consoleOutputInterceptor.Ouput, formatter.Header).Where(c => !string.IsNullOrWhiteSpace(c)).SingleOrDefault();
            Assert.IsNotNull(entry);
            StringAssert.Contains(entry, "200");
            Assert.AreEqual(DefaultConsoleColorMapper.Critical, consoleOutputInterceptor.OutputForegroundColor);
        }
        public void WhenThreadId()
        {
            var consoleOutputInterceptor = new MockConsoleOutputInterceptor();
            var formatter = new EventTextFormatter(EventTextFormatter.DashSeparator);
            var logger = MockConsoleListenerEventSource.Logger;

            int threadId = ThreadHelper.GetCurrentUnManagedThreadId();

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    listener.LogToConsole();
                    listener.EnableEvents(logger, EventLevel.Critical);
                    logger.Critical("This is to log critical in Console");
                });

            var entry = Regex.Split(consoleOutputInterceptor.Ouput, formatter.Header).Where(c => !string.IsNullOrWhiteSpace(c)).SingleOrDefault();
            Assert.IsNotNull(entry);
            StringAssert.Contains(entry, "ThreadId : " + threadId);
        }
        public void WhenEventWithMessageInAttributeUsingXml()
        {
            var consoleOutputInterceptor = new MockConsoleOutputInterceptor();
            var formatter = new EventTextFormatter(EventTextFormatter.DashSeparator);
            var xmlFormatter = new XmlEventTextFormatter();
            xmlFormatter.DateTimeFormat = "dd/MM/yyyy";
            var logger = TestEventSourceNoAttributes.Logger;

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    listener.LogToConsole(xmlFormatter);
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    logger.ObjectArrayEvent4(1000, "stringstringarg10", 2000, "stringstringarg20", 3000);
                });

            var entry = Regex.Split(consoleOutputInterceptor.Ouput, formatter.Header).Where(c => !string.IsNullOrWhiteSpace(c)).SingleOrDefault();
            Assert.IsNotNull(entry);
            StringAssert.Contains(entry, "<Data Name=\"arg0\">1000</Data><Data Name=\"arg1\">stringstringarg10</Data><Data Name=\"arg2\">2000</Data><Data Name=\"arg3\">stringstringarg20</Data><Data Name=\"arg4\">3000</Data>");
            StringAssert.Contains(entry, "<Message>Check if it is logged</Message>");
        }
        public void WhenEventWithTaskNone()
        {
            var consoleOutputInterceptor = new MockConsoleOutputInterceptor();
            var formatter = new EventTextFormatter(EventTextFormatter.DashSeparator);
            var logger = MockConsoleListenerEventSource.Logger;

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    listener.LogToConsole();
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    logger.Critical("Critical with taskname or eventname as Critical");
                });

            var entry = Regex.Split(consoleOutputInterceptor.Ouput, formatter.Header).Where(c => !string.IsNullOrWhiteSpace(c)).SingleOrDefault();
            Assert.IsNotNull(entry);
            Assert.IsTrue(entry.Contains("ProviderId : "));
            Assert.IsTrue(entry.Contains("\r\nEventId : 200\r\nKeywords : None\r\nLevel : Critical\r\nMessage : Functional Test\r\nOpcode : Info\r\nTask : 65334\r\nVersion : 0\r\nPayload : [message : Critical with taskname or eventname as Critical] \r\nEventName : CriticalInfo\r\nTimestamp : "));
        }
        public void WhenEventWithMessageInAttribute()
        {
            var consoleOutputInterceptor = new MockConsoleOutputInterceptor();
            var formatter = new EventTextFormatter(EventTextFormatter.DashSeparator);
            var eventformatter = new EventTextFormatter("----", "-----", EventLevel.Informational);
            eventformatter.DateTimeFormat = "dd/MM/yyyy";
            var logger = TestEventSourceNoAttributes.Logger;

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    listener.LogToConsole(eventformatter);
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    logger.ObjectArrayEvent4(1000, "stringstringarg10", 2000, "stringstringarg20", 3000);
                });

            var entry = Regex.Split(consoleOutputInterceptor.Ouput, formatter.Header).Where(c => !string.IsNullOrWhiteSpace(c)).SingleOrDefault();
            Assert.IsNotNull(entry);
            StringAssert.Contains(entry, "[arg0 : 1000] [arg1 : stringstringarg10] [arg2 : 2000] [arg3 : stringstringarg20] [arg4 : 3000]");
            StringAssert.Contains(entry, "Message : Check if it is logged");
        }
        public void WhenSourceEnabledAndNotSpecifyingKeyword()
        {
            var consoleOutputInterceptor = new MockConsoleOutputInterceptor();
            var formatter = new EventTextFormatter(EventTextFormatter.DashSeparator);
            var logger = MockConsoleListenerEventSource.Logger;

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    listener.LogToConsole();
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    logger.InfoWithKeywordDiagnostic("Info with keyword Diagnostic");
                });

            var entry = Regex.Split(consoleOutputInterceptor.Ouput, formatter.Header).Where(c => !string.IsNullOrWhiteSpace(c)).SingleOrDefault();
#if EVENT_SOURCE_PACKAGE
            Assert.IsNotNull(entry);
#else
            Assert.IsNull(entry);
#endif
            }
        public void WhenActivityIdAndRelatedActivityId()
        {
            var consoleOutputInterceptor = new MockConsoleOutputInterceptor();
            var formatter = new EventTextFormatter(EventTextFormatter.DashSeparator);
            var logger = MockConsoleListenerEventSource.Logger;

            var activityId = Guid.NewGuid();
            var relatedActivityId = Guid.NewGuid();
            var previousActivityId = Guid.Empty;
            EventSource.SetCurrentThreadActivityId(activityId, out previousActivityId);
            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    listener.LogToConsole();
                    listener.EnableEvents(logger, EventLevel.Critical);
                    logger.CriticalWithRelatedActivityId("This is to log critical in Console", relatedActivityId);
                });
            
            EventSource.SetCurrentThreadActivityId(previousActivityId);

            var entry = Regex.Split(consoleOutputInterceptor.Ouput, formatter.Header).Where(c => !string.IsNullOrWhiteSpace(c)).SingleOrDefault();
            Assert.IsNotNull(entry);
            StringAssert.Contains(entry, "800");
            StringAssert.Contains(entry, "Keywords : None");
            Assert.AreEqual(DefaultConsoleColorMapper.Critical, consoleOutputInterceptor.OutputForegroundColor);
            StringAssert.Contains(entry, "ActivityId : " + activityId.ToString());
            StringAssert.Contains(entry, "RelatedActivityId : " + relatedActivityId.ToString());
        }
        public void WhenLoggingErrorOCcurs()
        {
            var mockConsole = new MockConsoleOutputInterceptor();
            var logger = TestEventSource.Logger;

            TestScenario.With1Listener(
                logger,
                (listener, errorsListener) =>
                {
                    listener.LogToConsole(new MockFormatter(true));
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    logger.EventWithPayload("payload1", 100);

                    StringAssert.Contains(errorsListener.ToString(), "System.InvalidOperationException: Operation is not valid due to the current state of the object.");
                    Assert.AreEqual(string.Empty, mockConsole.Ouput);
                });
        }
        public void WhenEnablingAllKeywords()
        {
            var consoleOutputInterceptor = new MockConsoleOutputInterceptor();
            var formatter = new EventTextFormatter("----", "====", EventLevel.LogAlways);
            var logger = MockConsoleListenerEventSource.Logger;

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    listener.EnableEvents(logger, EventLevel.LogAlways, Keywords.All);
                    listener.LogToConsole(formatter);
                    logger.InfoWithKeywordDiagnostic("Info with keyword Diagnostic");
                });

            var entry = Regex.Split(consoleOutputInterceptor.Ouput, formatter.Header).Where(c => !string.IsNullOrWhiteSpace(c)).SingleOrDefault();
            Assert.IsNotNull(entry);
            Assert.IsTrue(entry.Contains("ProviderId : "));
            Assert.IsTrue(entry.Contains("\r\nEventId : 1020\r\nKeywords : 4\r\nLevel : Informational\r\nMessage : \r\nOpcode : Info\r\nTask : 2\r\nVersion : 0\r\nPayload : [message : Info with keyword Diagnostic] \r\nEventName : DbQueryInfo\r\nTimestamp : "));
        }
        public void WhenHighEventIds()
        {
            var consoleOutputInterceptor = new MockConsoleOutputInterceptor();
            var formatter = new EventTextFormatter(EventTextFormatter.DashSeparator);
            var logger = MockHighEventIdEventSource.HigheventIdLogger;

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    listener.LogToConsole();
                    listener.EnableEvents(logger, EventLevel.Warning);
                    logger.Warning();
                });

            var entry = Regex.Split(consoleOutputInterceptor.Ouput, formatter.Header).Where(c => !string.IsNullOrWhiteSpace(c)).SingleOrDefault();
            Assert.IsNotNull(entry);
        }
        public void WhenLowEventIds()
        {
            var consoleOutputInterceptor = new MockConsoleOutputInterceptor();
            var formatter = new EventTextFormatter(EventTextFormatter.DashSeparator);
            var logger = MockNegativeEventIdEventSource.LoweventIdLogger;

            try
            {
                TestScenario.With1Listener(
                logger,
                listener =>
                {
                    listener.EnableEvents(logger, EventLevel.Warning);
                    Assert.IsFalse(true, "Should throw when calling EnableEvents.");
                });
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("Event IDs must be positive integers.", ex.Message);
            }
        }
        public void WhenEventHasRawMessageAndFormattedMessage()
        {
            var consoleOutputInterceptor = new MockConsoleOutputInterceptor();
            var formatter = new EventTextFormatter(EventTextFormatter.DashSeparator);
            var logger = MockEventSourceNoTask.Logger;

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    listener.LogToConsole();
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    logger.Informational("testing");
                });

            var entry = Regex.Split(consoleOutputInterceptor.Ouput, formatter.Header).Where(c => !string.IsNullOrWhiteSpace(c)).SingleOrDefault();
            Assert.IsNotNull(entry);
            Assert.IsTrue(entry.Count() > 0);
            StringAssert.Contains(entry, "message param");
        }
        public void WhenPayloadHasDifferentTypesAndNull()
        {
            var consoleOutputInterceptor = new MockConsoleOutputInterceptor();
            var formatter = new EventTextFormatter(EventTextFormatter.DashSeparator);
            var logger = MockEventSourceNoTask.Logger;

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    listener.LogToConsole();
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    logger.DifferentTypes(null, 500000);
                });

            var entry = Regex.Split(consoleOutputInterceptor.Ouput, formatter.Header).Where(c => !string.IsNullOrWhiteSpace(c)).SingleOrDefault();
            Assert.IsNotNull(entry);
            Assert.IsTrue(entry.Count() > 0);
            StringAssert.Contains(entry, @"strArg : ");
            StringAssert.Contains(entry, @"longArg : 500000");
        }
        public void WhenOneSourceTwoListenersConcurrently()
        {
            var consoleOutputInterceptor = new MockConsoleOutputInterceptor();
            var formatter = new EventTextFormatter(EventTextFormatter.DashSeparator);
            var logger = MockConsoleListenerEventSource.Logger;

            TestScenario.With2Listeners(
                logger,
                (listener1, listener2) =>
                {
                    listener1.LogToConsole();
                    listener2.LogToConsole();
                    int maxLoggedEntries = 9;
                    string criticalMessage = string.Concat("CriticalMessage");
                    string infoMessage = string.Concat("InfoMessage");
                    listener1.EnableEvents(logger, EventLevel.Critical);
                    listener2.EnableEvents(logger, EventLevel.Critical);
                    Parallel.Invoke(Enumerable.Range(0, maxLoggedEntries).Select(i =>
                    new Action(() =>
                    {
                        logger.Critical(i + criticalMessage);
                    })).ToArray());
                });

            var entry = Regex.Split(consoleOutputInterceptor.Ouput, formatter.Header).Where(c => !string.IsNullOrWhiteSpace(c)).SingleOrDefault();
            Assert.IsNotNull(entry);
        }
        public void WhenMultipleEvents()
        {
            var consoleOutputInterceptor = new MockConsoleOutputInterceptor();
            var formatter = new EventTextFormatter(EventTextFormatter.DashSeparator);
            var logger = MockConsoleListenerEventSource.Logger;

            TestScenario.With1Listener(
                logger,
                listener =>
                {
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    listener.LogToConsole();
                    for (int n = 0; n < 300; n++)
                    {
                        logger.Informational("Some message to console " + n);
                    }
                });

            var output = Regex.Split(consoleOutputInterceptor.Ouput, formatter.Header).Where(c => !string.IsNullOrWhiteSpace(c));
            Assert.IsNotNull(output);
            StringAssert.Contains(output.First(), "Some message to console 0");
            StringAssert.Contains(output.First(), "Some message to console 299");
        }
        public void WhenOneListenerTwoSources()
        {
            var consoleOutputInterceptor = new MockConsoleOutputInterceptor();
            var formatter = new EventTextFormatter(EventTextFormatter.DashSeparator);
            var logger = MockConsoleListenerEventSource.Logger;
            var logger2 = MockConsoleListenerEventSource2.Logger;

            string message = string.Concat("Message ", Guid.NewGuid());
            string errorMessage = string.Concat("Error ", Guid.NewGuid());
            TestScenario.With1Listener(
                new EventSource[] { logger, logger2 },
                listener =>
                {
                    listener.LogToConsole();
                    listener.EnableEvents(logger, EventLevel.LogAlways);
                    listener.EnableEvents(logger2, EventLevel.LogAlways);
                    logger.Informational(message);
                    logger2.Informational(message);
                });

            var entry = Regex.Split(consoleOutputInterceptor.Ouput, formatter.Header).Where(c => !string.IsNullOrWhiteSpace(c)).SingleOrDefault();
            Assert.IsNotNull(entry);
            StringAssert.Contains(entry, "100");
        }