Example #1
0
        public void TextWriterEventListener()
        {
            string text;

            using (var writer = new StringWriter(CultureInfo.InvariantCulture))
            {
                using (var listener = new TextWriterEventListener(Events.Log, writer, DateTime.UtcNow, warningNumber => WarningState.AsWarning, EventLevel.Warning))
                {
                    listener.RegisterEventSource(TestEvents.Log);

                    TestEvents log = TestEvents.Log;

                    // should be captured
                    log.AlwaysEvent("Cookie 1 ");
                    log.CriticalEvent("Cookie 2 ");
                    log.ErrorEvent("Cookie 3 ");
                    log.WarningEvent("Cookie 4 ");

                    // shouldn't be captured
                    log.InfoEvent("Cookie 5 ");
                    log.VerboseEvent("Cookie 6 ");
                }

                text = writer.ToString();
            }

            XAssert.IsTrue(Regex.IsMatch(text, ".*Cookie 1 .*"));
            XAssert.IsTrue(Regex.IsMatch(text, ".*Cookie 2 .*"));
            XAssert.IsTrue(Regex.IsMatch(text, ".*Cookie 3 .*"));
            XAssert.IsTrue(Regex.IsMatch(text, ".*Cookie 4 .*"));
            XAssert.IsFalse(Regex.IsMatch(text, ".*Cookie 5 .*"));
            XAssert.IsFalse(Regex.IsMatch(text, ".*Cookie 6 .*"));

            using (var writer = new StringWriter(CultureInfo.InvariantCulture))
            {
                using (var listener = new TextWriterEventListener(Events.Log, writer, DateTime.UtcNow, warningNumber => WarningState.AsWarning))
                {
                    listener.RegisterEventSource(TestEvents.Log);

                    TestEvents log = TestEvents.Log;

                    // should be captured
                    log.AlwaysEvent("Cookie 11 ");
                    log.CriticalEvent("Cookie 12 ");
                    log.ErrorEvent("Cookie 13 ");
                    log.WarningEvent("Cookie 14 ");
                    log.InfoEvent("Cookie 15 ");
                    log.VerboseEvent("Cookie 16 ");
                }

                text = writer.ToString();
            }

            XAssert.IsTrue(Regex.IsMatch(text, ".*Cookie 11 .*"));
            XAssert.IsTrue(Regex.IsMatch(text, ".*Cookie 12 .*"));
            XAssert.IsTrue(Regex.IsMatch(text, ".*Cookie 13 .*"));
            XAssert.IsTrue(Regex.IsMatch(text, ".*Cookie 14 .*"));
            XAssert.IsTrue(Regex.IsMatch(text, ".*Cookie 15 .*"));
            XAssert.IsTrue(Regex.IsMatch(text, ".*Cookie 16 .*"));
        }
Example #2
0
        public void TestMessageLabels()
        {
            string text;
            string alwaysEventLabel            = "message";
            string criticalEventLabel          = "critical";
            string errorEventLabel             = "error";
            string warningEventLabel           = "warning";
            string suppressedWarningEventLabel = "NoWarn";
            string infoEventLabel    = "info";
            string verboseEventLabel = "verbose";

            using (var writer = new StringWriter(CultureInfo.InvariantCulture))
            {
                using (var listener = new TextWriterEventListener(Events.Log, writer, DateTime.UtcNow, warningNumber => WarningState.AsWarning))
                {
                    listener.RegisterEventSource(TestEvents.Log);

                    TestEvents log = TestEvents.Log;

                    log.AlwaysEvent("Cookie 1");
                    log.CriticalEvent("Cookie 2");
                    log.ErrorEvent("Cookie 3");
                    log.WarningEvent("Cookie 4");
                    log.InfoEvent("Cookie 5");
                    log.VerboseEvent("Cookie 6");
                }

                text = writer.ToString();
            }

            XAssert.IsTrue(Regex.IsMatch(text, $"^{alwaysEventLabel} DX{(int)EventId.AlwaysEvent:D4}: Cookie 1\\r?$", RegexOptions.Multiline));
            XAssert.IsTrue(Regex.IsMatch(text, $"^{criticalEventLabel} DX{(int)EventId.CriticalEvent:D4}: Cookie 2\\r?$", RegexOptions.Multiline));
            XAssert.IsTrue(Regex.IsMatch(text, $"^{errorEventLabel} DX{(int)EventId.ErrorEvent:D4}: Cookie 3\\r?$", RegexOptions.Multiline));
            XAssert.IsTrue(Regex.IsMatch(text, $"^{warningEventLabel} DX{(int)EventId.WarningEvent:D4}: Cookie 4\\r?$", RegexOptions.Multiline));
            XAssert.IsTrue(Regex.IsMatch(text, $"^{infoEventLabel} DX{(int)EventId.InfoEvent:D4}: Cookie 5\\r?$", RegexOptions.Multiline));
            XAssert.IsTrue(Regex.IsMatch(text, $"^{verboseEventLabel} DX{(int)EventId.VerboseEvent:D4}: Cookie 6\\r?$", RegexOptions.Multiline));

            // suppress the warning message (similar to passing /noWarn:10002 (EventId.WarningEvent))
            using (var writer = new StringWriter(CultureInfo.InvariantCulture))
            {
                using (var listener = new TextWriterEventListener(Events.Log, writer, DateTime.UtcNow, warningNumber => WarningState.Suppressed))
                {
                    listener.RegisterEventSource(TestEvents.Log);

                    TestEvents log = TestEvents.Log;

                    // although we are suppressing the warning, it still should be captured (suppression only applies to console output and err/wrn files)
                    // however, this time a different label should be used
                    log.WarningEvent("Cookie 4");
                }

                text = writer.ToString();
            }

            XAssert.IsTrue(Regex.IsMatch(text, $"^{suppressedWarningEventLabel} DX{(int)EventId.WarningEvent:D4}: Cookie 4\\r?$", RegexOptions.Multiline));
        }
Example #3
0
        public void TrackingEventListener()
        {
            const int NumVerbose  = 9;
            const int NumInfo     = 8;
            const int NumWarning  = 7;
            const int NumError    = 6;
            const int NumCritical = 5;
            const int NumAlways   = 4;

            using (var listener = new TrackingEventListener(Events.Log))
            {
                listener.RegisterEventSource(TestEvents.Log);

                TestEvents log = TestEvents.Log;

                for (int i = 0; i < NumVerbose; i++)
                {
                    log.VerboseEvent("1");
                }

                for (int i = 0; i < NumInfo; i++)
                {
                    log.InfoEvent("1");
                }

                for (int i = 0; i < NumWarning; i++)
                {
                    log.WarningEvent("1");
                }

                for (int i = 0; i < NumError; i++)
                {
                    log.ErrorEvent("1");
                }

                for (int i = 0; i < NumCritical; i++)
                {
                    log.CriticalEvent("1");
                }

                for (int i = 0; i < NumAlways; i++)
                {
                    log.AlwaysEvent("1");
                }

                XAssert.AreEqual(listener.VerboseCount, NumVerbose);
                XAssert.AreEqual(listener.InformationalCount, NumInfo);
                XAssert.AreEqual(listener.WarningCount, NumWarning);
                XAssert.AreEqual(listener.TotalErrorCount, NumError + NumCritical);
                XAssert.AreEqual(listener.CriticalCount, NumCritical);
                XAssert.AreEqual(listener.AlwaysCount, NumAlways);
                XAssert.IsTrue(listener.HasFailures);
                XAssert.IsTrue(listener.HasFailuresOrWarnings);
            }

            using (var listener = new TrackingEventListener(Events.Log))
            {
                listener.RegisterEventSource(TestEvents.Log);

                TestEvents log = TestEvents.Log;

                XAssert.IsFalse(listener.HasFailures);
                XAssert.IsFalse(listener.HasFailuresOrWarnings);

                log.WarningEvent("1");

                XAssert.IsFalse(listener.HasFailures);
                XAssert.IsTrue(listener.HasFailuresOrWarnings);

                log.ErrorEvent("1");
                XAssert.IsTrue(listener.HasFailures);
                XAssert.IsTrue(listener.HasFailuresOrWarnings);
            }

            using (var listener = new TrackingEventListener(Events.Log))
            {
                listener.RegisterEventSource(TestEvents.Log);

                TestEvents log = TestEvents.Log;

                log.CriticalEvent("1");

                XAssert.IsTrue(listener.HasFailures);
                XAssert.IsTrue(listener.HasFailuresOrWarnings);
            }

            using (var listener = new TrackingEventListener(Events.Log, warningMapper: _ => WarningState.AsError))
            {
                listener.RegisterEventSource(TestEvents.Log);

                TestEvents log = TestEvents.Log;

                log.WarningEvent("1");

                XAssert.AreEqual(1, listener.TotalErrorCount);
                XAssert.IsTrue(listener.HasFailures);
                XAssert.IsTrue(listener.HasFailuresOrWarnings);
            }
        }
Example #4
0
        public void ErrorAndWarningListener()
        {
            string text;

            using (var writer = new StringWriter(CultureInfo.InvariantCulture))
            {
                using (var listener = new ErrorAndWarningEventListener(Events.Log, writer, DateTime.UtcNow, true, false, warningNumber => WarningState.AsWarning))
                {
                    listener.RegisterEventSource(TestEvents.Log);

                    TestEvents log = TestEvents.Log;

                    // should be captured
                    log.CriticalEvent("Cookie 1 ");
                    log.ErrorEvent("Cookie 2 ");

                    // shouldn't be captured
                    log.WarningEvent("Cookie 3 ");
                    log.AlwaysEvent("Cookie 4 ");
                    log.InfoEvent("Cookie 5 ");
                    log.VerboseEvent("Cookie 6 ");
                }

                text = writer.ToString();
            }

            XAssert.IsTrue(Regex.IsMatch(text, ".*Cookie 1 .*"));
            XAssert.IsTrue(Regex.IsMatch(text, ".*Cookie 2 .*"));
            XAssert.IsFalse(Regex.IsMatch(text, ".*Cookie 3 .*"));
            XAssert.IsFalse(Regex.IsMatch(text, ".*Cookie 4 .*"));
            XAssert.IsFalse(Regex.IsMatch(text, ".*Cookie 5 .*"));
            XAssert.IsFalse(Regex.IsMatch(text, ".*Cookie 6 .*"));

            using (var writer = new StringWriter(CultureInfo.InvariantCulture))
            {
                using (var listener = new ErrorAndWarningEventListener(Events.Log, writer, DateTime.UtcNow, false, true, warningNumber => WarningState.AsWarning))
                {
                    listener.RegisterEventSource(TestEvents.Log);

                    TestEvents log = TestEvents.Log;

                    // should be captured
                    log.WarningEvent("Cookie 3 ");

                    // shouldn't be captured
                    log.CriticalEvent("Cookie 1 ");
                    log.ErrorEvent("Cookie 2 ");
                    log.AlwaysEvent("Cookie 4 ");
                    log.InfoEvent("Cookie 5 ");
                    log.VerboseEvent("Cookie 6 ");
                }

                text = writer.ToString();
            }

            XAssert.IsFalse(Regex.IsMatch(text, ".*Cookie 1 .*"));
            XAssert.IsFalse(Regex.IsMatch(text, ".*Cookie 2 .*"));
            XAssert.IsTrue(Regex.IsMatch(text, ".*Cookie 3 .*"));
            XAssert.IsFalse(Regex.IsMatch(text, ".*Cookie 4 .*"));
            XAssert.IsFalse(Regex.IsMatch(text, ".*Cookie 5 .*"));
            XAssert.IsFalse(Regex.IsMatch(text, ".*Cookie 6 .*"));

            // repeat the same tests, but now suppress the warning message (i.e., emulate /noWarn:10002 (EventId.WarningEvent))
            // WarningMapper applies only to warning-level events, so we can check only that one
            // since the warning is being suppressed, nothing should be captured
            using (var writer = new StringWriter(CultureInfo.InvariantCulture))
            {
                using (var listener = new ErrorAndWarningEventListener(Events.Log, writer, DateTime.UtcNow, true, false, warningNumber => WarningState.Suppressed))
                {
                    listener.RegisterEventSource(TestEvents.Log);

                    TestEvents log = TestEvents.Log;

                    log.WarningEvent("Cookie 3 ");
                }

                text = writer.ToString();
            }

            XAssert.IsFalse(Regex.IsMatch(text, ".*Cookie 3 .*"));

            using (var writer = new StringWriter(CultureInfo.InvariantCulture))
            {
                using (var listener = new ErrorAndWarningEventListener(Events.Log, writer, DateTime.UtcNow, false, true, warningNumber => WarningState.Suppressed))
                {
                    listener.RegisterEventSource(TestEvents.Log);

                    TestEvents log = TestEvents.Log;

                    log.WarningEvent("Cookie 3 ");
                }

                text = writer.ToString();
            }

            XAssert.IsFalse(Regex.IsMatch(text, ".*Cookie 3 .*"));
        }