コード例 #1
0
        public void NoErrorsWhileSendingToTraceSourceOnlyLogsOriginalMessageToCategoryTraceSources()
        {
            LogSource errorsTraceSource = new LogSource("errors", SourceLevels.All);

            errorsTraceSource.Listeners.Add(new ErrorsMockTraceListener());

            LogSource failingTraceSource = new LogSource("logging", SourceLevels.All);

            failingTraceSource.Listeners.Add(new MockTraceListener());
            LogSource loggingTraceSource = new LogSource("logging2", SourceLevels.All);

            loggingTraceSource.Listeners.Add(new MockTraceListener());

            LogWriter writer =
                new LogWriterImpl(new ILogFilter[0],
                                  new LogSource[] { failingTraceSource, loggingTraceSource },
                                  errorsTraceSource,
                                  "default");

            LogEntry logEntry = new LogEntry();

            logEntry.Message    = originalMessage;
            logEntry.Severity   = TraceEventType.Critical;
            logEntry.Categories = new string[] { "logging", "logging2" };

            writer.Write(logEntry);

            writer.Dispose();
            Assert.AreEqual(0, ErrorsMockTraceListener.Entries.Count);
            Assert.AreEqual(2, MockTraceListener.Entries.Count);
            Assert.AreSame(logEntry, MockTraceListener.LastEntry);
        }
コード例 #2
0
        public void UsedMandatoryTraceSourceIfThereAreMatchingAndMissingCategories()
        {
            Dictionary <string, LogSource> traceSources = new Dictionary <string, LogSource>();

            traceSources.Add("newcat1", new LogSource("newcat1"));
            traceSources.Add("newcat2", new LogSource("newcat2"));
            traceSources.Add("newcat3", new LogSource("newcat3"));
            traceSources.Add("newcat4", new LogSource("newcat4"));
            LogSource mandatoryTraceSource = new LogSource("mandatory");
            LogSource errorsTraceSource    = new LogSource("errors", SourceLevels.All);
            LogWriter logWriter            = new LogWriterImpl(emptyFilters, traceSources, mandatoryTraceSource, emptyTraceSource, errorsTraceSource, "default", false, false);

            string[] categories = new string[] { "newcat1", "newcat2", "newcat5", "newcat6" };
            LogEntry logEntry   = new LogEntry();

            logEntry.Categories = categories;
            IEnumerable <LogSource> matchingTraceSources = logWriter.GetMatchingTraceSources(logEntry);

            logWriter.Dispose();
            Dictionary <string, LogSource> matchingTraceSourcesDictionary = new Dictionary <string, LogSource>();

            foreach (LogSource traceSource in matchingTraceSources)
            {
                matchingTraceSourcesDictionary.Add(traceSource.Name, traceSource);
            }

            Assert.AreEqual(3, matchingTraceSourcesDictionary.Count);
            Assert.IsTrue(matchingTraceSourcesDictionary.ContainsKey(categories[0]));
            Assert.IsTrue(matchingTraceSourcesDictionary.ContainsKey(categories[1]));
            Assert.IsTrue(matchingTraceSourcesDictionary.ContainsKey(mandatoryTraceSource.Name));
            Assert.AreEqual(0, ErrorsMockTraceListener.Entries.Count);
        }
コード例 #3
0
        public void ErrorWhileSendingToTraceSourceLogsWarningAndLogsOriginalMessageToNonFailingTraceSources()
        {
            LogSource errorsTraceSource = new LogSource("errors", SourceLevels.All);

            errorsTraceSource.Listeners.Add(new ErrorsMockTraceListener());

            LogSource failingTraceSource = new LogSource("failing", SourceLevels.All);

            failingTraceSource.Listeners.Add(new ExceptionThrowingMockTraceListener());
            LogSource loggingTraceSource = new LogSource("logging", SourceLevels.All);

            loggingTraceSource.Listeners.Add(new MockTraceListener());

            LogWriter writer =
                new LogWriterImpl(new ILogFilter[0],
                                  new LogSource[] { failingTraceSource, loggingTraceSource },
                                  errorsTraceSource,
                                  "default");

            LogEntry logEntry = new LogEntry();

            logEntry.Message    = originalMessage;
            logEntry.Severity   = TraceEventType.Critical;
            logEntry.Categories = new string[] { "failing", "logging" };

            writer.Write(logEntry);

            writer.Dispose();
            Assert.AreEqual(1, ErrorsMockTraceListener.Entries.Count);
            Assert.AreEqual(TraceEventType.Error, ErrorsMockTraceListener.LastEntry.Severity);
            Assert.IsTrue(MatchTemplate(ErrorsMockTraceListener.LastEntry.Message, Resources.TraceSourceFailed));
            Assert.AreEqual(1, MockTraceListener.Entries.Count);
            Assert.AreSame(logEntry, MockTraceListener.LastEntry);
        }
コード例 #4
0
        public void CanFindMatchingCategories()
        {
            Dictionary <string, LogSource> traceSources = new Dictionary <string, LogSource>();

            traceSources.Add("newcat1", new LogSource("newcat1"));
            traceSources.Add("newcat2", new LogSource("newcat2"));
            traceSources.Add("newcat3", new LogSource("newcat3"));
            traceSources.Add("newcat4", new LogSource("newcat4"));
            LogWriter logWriter = new LogWriterImpl(emptyFilters, traceSources, new LogSource("errors"), "default");

            string[] categories = new string[] { "newcat1", "newcat2", "newcat5", "newcat6" };
            LogEntry logEntry   = new LogEntry();

            logEntry.Categories = categories;
            IEnumerable <LogSource> matchingTraceSources = logWriter.GetMatchingTraceSources(logEntry);

            logWriter.Dispose();

            Dictionary <string, LogSource> matchingTraceSourcesDictionary = new Dictionary <string, LogSource>();

            foreach (LogSource traceSource in matchingTraceSources)
            {
                matchingTraceSourcesDictionary.Add(traceSource.Name, traceSource);
            }

            Assert.AreEqual(2, matchingTraceSourcesDictionary.Count);
            Assert.IsTrue(matchingTraceSourcesDictionary.ContainsKey(categories[0]));
            Assert.IsTrue(matchingTraceSourcesDictionary.ContainsKey(categories[1]));
            Assert.IsFalse(matchingTraceSourcesDictionary.ContainsKey(categories[2]));
        }
コード例 #5
0
        public void DoesNotReportMissingCategoriesWhenThereAreNotMissingCategoriesAndDefaultIsNotConfigured()
        {
            Dictionary <string, LogSource> traceSources = new Dictionary <string, LogSource>();

            traceSources.Add("newcat1", new LogSource("newcat1"));
            traceSources.Add("newcat2", new LogSource("newcat2"));
            traceSources.Add("newcat3", new LogSource("newcat3"));
            traceSources.Add("newcat4", new LogSource("newcat4"));
            LogSource errorsTraceSource = new LogSource("errors", SourceLevels.All);

            errorsTraceSource.Listeners.Add(new ErrorsMockTraceListener());
            LogWriter logWriter = new LogWriterImpl(emptyFilters, traceSources, emptyTraceSource, emptyTraceSource, errorsTraceSource, "default", false, false);

            string[] categories = new string[] { "newcat1", "newcat2" };
            LogEntry logEntry   = new LogEntry();

            logEntry.Categories = categories;
            IEnumerable <LogSource> matchingTraceSources = logWriter.GetMatchingTraceSources(logEntry);

            logWriter.Dispose();
            Dictionary <string, LogSource> matchingTraceSourcesDictionary = new Dictionary <string, LogSource>();

            foreach (LogSource traceSource in matchingTraceSources)
            {
                matchingTraceSourcesDictionary.Add(traceSource.Name, traceSource);
            }

            Assert.AreEqual(2, matchingTraceSourcesDictionary.Count);
            Assert.AreEqual(0, ErrorsMockTraceListener.Entries.Count);
        }
コード例 #6
0
        public void MissingCategoriesWarningIsLoggedIfLogWarningFlagIsTrue()
        {
            LogSource errorsTraceSource = new LogSource("errors", SourceLevels.All);

            errorsTraceSource.Listeners.Add(new ErrorsMockTraceListener());

            LogSource failingTraceSource = new LogSource("logging", SourceLevels.All);

            failingTraceSource.Listeners.Add(new MockTraceListener());
            LogSource loggingTraceSource = new LogSource("logging2", SourceLevels.All);

            loggingTraceSource.Listeners.Add(new MockTraceListener());

            LogWriter writer =
                new LogWriterImpl(new ILogFilter[0],
                                  new LogSource[] { failingTraceSource, loggingTraceSource },
                                  emptyTraceSource,
                                  emptyTraceSource,
                                  errorsTraceSource,
                                  "default",
                                  false,
                                  true);

            LogEntry logEntry = new LogEntry();

            logEntry.Message    = originalMessage;
            logEntry.Severity   = TraceEventType.Critical;
            logEntry.Categories = new string[] { "logging", "logging2", "logging3" };

            writer.Write(logEntry);

            writer.Dispose();
            Assert.AreEqual(1, ErrorsMockTraceListener.Entries.Count);
            Assert.AreEqual(TraceEventType.Error, ErrorsMockTraceListener.LastEntry.Severity);
            Assert.IsTrue(MatchTemplate(ErrorsMockTraceListener.LastEntry.Message, Resources.MissingCategories));
            Assert.AreEqual(2, MockTraceListener.Entries.Count);
            Assert.AreSame(logEntry, MockTraceListener.LastEntry);
        }
コード例 #7
0
        public void NoErrorsWhileSendingToTraceSourceOnlyLogsOriginalMessageToCategoryTraceSources()
        {
            LogSource errorsTraceSource = new LogSource("errors", SourceLevels.All);
            errorsTraceSource.Listeners.Add(new ErrorsMockTraceListener());

            LogSource failingTraceSource = new LogSource("logging", SourceLevels.All);
            failingTraceSource.Listeners.Add(new MockTraceListener());
            LogSource loggingTraceSource = new LogSource("logging2", SourceLevels.All);
            loggingTraceSource.Listeners.Add(new MockTraceListener());

            LogWriter writer =
                new LogWriterImpl(new ILogFilter[0],
                              new LogSource[] { failingTraceSource, loggingTraceSource },
                              errorsTraceSource,
                              "default");

            LogEntry logEntry = new LogEntry();
            logEntry.Message = originalMessage;
            logEntry.Severity = TraceEventType.Critical;
            logEntry.Categories = new string[] { "logging", "logging2" };

            writer.Write(logEntry);

            writer.Dispose();
            Assert.AreEqual(0, ErrorsMockTraceListener.Entries.Count);
            Assert.AreEqual(2, MockTraceListener.Entries.Count);
            Assert.AreSame(logEntry, MockTraceListener.LastEntry);
        }
コード例 #8
0
        public void MissingCategoriesWarningIsLoggedIfLogWarningFlagIsTrue()
        {
            LogSource errorsTraceSource = new LogSource("errors", SourceLevels.All);
            errorsTraceSource.Listeners.Add(new ErrorsMockTraceListener());

            LogSource failingTraceSource = new LogSource("logging", SourceLevels.All);
            failingTraceSource.Listeners.Add(new MockTraceListener());
            LogSource loggingTraceSource = new LogSource("logging2", SourceLevels.All);
            loggingTraceSource.Listeners.Add(new MockTraceListener());

            LogWriter writer =
                new LogWriterImpl(new ILogFilter[0],
                              new LogSource[] { failingTraceSource, loggingTraceSource },
                              emptyTraceSource,
                              emptyTraceSource,
                              errorsTraceSource,
                              "default",
                              false,
                              true);

            LogEntry logEntry = new LogEntry();
            logEntry.Message = originalMessage;
            logEntry.Severity = TraceEventType.Critical;
            logEntry.Categories = new string[] { "logging", "logging2", "logging3" };

            writer.Write(logEntry);

            writer.Dispose();
            Assert.AreEqual(1, ErrorsMockTraceListener.Entries.Count);
            Assert.AreEqual(TraceEventType.Error, ErrorsMockTraceListener.LastEntry.Severity);
            Assert.IsTrue(MatchTemplate(ErrorsMockTraceListener.LastEntry.Message, Resources.MissingCategories));
            Assert.AreEqual(2, MockTraceListener.Entries.Count);
            Assert.AreSame(logEntry, MockTraceListener.LastEntry);
        }
コード例 #9
0
        public void ErrorWhileSendingToTraceSourceLogsWarningAndLogsOriginalMessageToNonFailingTraceSources()
        {
            LogSource errorsTraceSource = new LogSource("errors", SourceLevels.All);
            errorsTraceSource.Listeners.Add(new ErrorsMockTraceListener());

            LogSource failingTraceSource = new LogSource("failing", SourceLevels.All);
            failingTraceSource.Listeners.Add(new ExceptionThrowingMockTraceListener());
            LogSource loggingTraceSource = new LogSource("logging", SourceLevels.All);
            loggingTraceSource.Listeners.Add(new MockTraceListener());

            LogWriter writer =
                new LogWriterImpl(new ILogFilter[0],
                              new LogSource[] { failingTraceSource, loggingTraceSource },
                              errorsTraceSource,
                              "default");

            LogEntry logEntry = new LogEntry();
            logEntry.Message = originalMessage;
            logEntry.Severity = TraceEventType.Critical;
            logEntry.Categories = new string[] { "failing", "logging" };

            writer.Write(logEntry);

            writer.Dispose();
            Assert.AreEqual(1, ErrorsMockTraceListener.Entries.Count);
            Assert.AreEqual(TraceEventType.Error, ErrorsMockTraceListener.LastEntry.Severity);
            Assert.IsTrue(MatchTemplate(ErrorsMockTraceListener.LastEntry.Message, Resources.TraceSourceFailed));
            Assert.AreEqual(1, MockTraceListener.Entries.Count);
            Assert.AreSame(logEntry, MockTraceListener.LastEntry);
        }
コード例 #10
0
        public void UsesDefaultTraceSourceIfThereAreMissingCategoriesAndDefaultIsConfiguredAndMandatoryIsNotConfigured()
        {
            Dictionary<string, LogSource> traceSources = new Dictionary<string, LogSource>();
            traceSources.Add("newcat1", new LogSource("newcat1"));
            traceSources.Add("newcat2", new LogSource("newcat2"));
            traceSources.Add("newcat3", new LogSource("newcat3"));
            traceSources.Add("newcat4", new LogSource("newcat4"));
            LogSource mandatoryTraceSource = null;
            LogSource defaultTraceSource = new LogSource("default");
            LogSource errorsTraceSource = new LogSource("errors", SourceLevels.All);
            errorsTraceSource.Listeners.Add(new ErrorsMockTraceListener());
            LogWriter logWriter =
                new LogWriterImpl(emptyFilters, traceSources, mandatoryTraceSource, defaultTraceSource, errorsTraceSource, "default", false, false);

            string[] categories = new string[] { "newcat1", "newcat2", "newcat5", "newcat6" };
            LogEntry logEntry = new LogEntry();
            logEntry.Categories = categories;
            IEnumerable<LogSource> matchingTraceSources = logWriter.GetMatchingTraceSources(logEntry);

            logWriter.Dispose();
            Dictionary<string, LogSource> matchingTraceSourcesDictionary = new Dictionary<string, LogSource>();
            foreach (LogSource traceSource in matchingTraceSources)
            {
                matchingTraceSourcesDictionary.Add(traceSource.Name, traceSource);
            }

            Assert.AreEqual(3, matchingTraceSourcesDictionary.Count);
            Assert.IsTrue(matchingTraceSourcesDictionary.ContainsKey(categories[0]));
            Assert.IsTrue(matchingTraceSourcesDictionary.ContainsKey(categories[1]));
            Assert.IsTrue(matchingTraceSourcesDictionary.ContainsKey(defaultTraceSource.Name));
            Assert.AreEqual(0, ErrorsMockTraceListener.Entries.Count);
        }
コード例 #11
0
        public void DoesNotReportMissingCategoriesWhenThereAreNotMissingCategoriesAndDefaultIsNotConfigured()
        {
            Dictionary<string, LogSource> traceSources = new Dictionary<string, LogSource>();
            traceSources.Add("newcat1", new LogSource("newcat1"));
            traceSources.Add("newcat2", new LogSource("newcat2"));
            traceSources.Add("newcat3", new LogSource("newcat3"));
            traceSources.Add("newcat4", new LogSource("newcat4"));
            LogSource errorsTraceSource = new LogSource("errors", SourceLevels.All);
            errorsTraceSource.Listeners.Add(new ErrorsMockTraceListener());
            LogWriter logWriter = new LogWriterImpl(emptyFilters, traceSources, emptyTraceSource, emptyTraceSource, errorsTraceSource, "default", false, false);

            string[] categories = new string[] { "newcat1", "newcat2" };
            LogEntry logEntry = new LogEntry();
            logEntry.Categories = categories;
            IEnumerable<LogSource> matchingTraceSources = logWriter.GetMatchingTraceSources(logEntry);

            logWriter.Dispose();
            Dictionary<string, LogSource> matchingTraceSourcesDictionary = new Dictionary<string, LogSource>();
            foreach (LogSource traceSource in matchingTraceSources)
            {
                matchingTraceSourcesDictionary.Add(traceSource.Name, traceSource);
            }

            Assert.AreEqual(2, matchingTraceSourcesDictionary.Count);
            Assert.AreEqual(0, ErrorsMockTraceListener.Entries.Count);
        }
コード例 #12
0
        public void CanFindMatchingCategories()
        {
            Dictionary<string, LogSource> traceSources = new Dictionary<string, LogSource>();
            traceSources.Add("newcat1", new LogSource("newcat1"));
            traceSources.Add("newcat2", new LogSource("newcat2"));
            traceSources.Add("newcat3", new LogSource("newcat3"));
            traceSources.Add("newcat4", new LogSource("newcat4"));
            LogWriter logWriter = new LogWriterImpl(emptyFilters, traceSources, new LogSource("errors"), "default");

            string[] categories = new string[] { "newcat1", "newcat2", "newcat5", "newcat6" };
            LogEntry logEntry = new LogEntry();
            logEntry.Categories = categories;
            IEnumerable<LogSource> matchingTraceSources = logWriter.GetMatchingTraceSources(logEntry);

            logWriter.Dispose();

            Dictionary<string, LogSource> matchingTraceSourcesDictionary = new Dictionary<string, LogSource>();
            foreach (LogSource traceSource in matchingTraceSources)
            {
                matchingTraceSourcesDictionary.Add(traceSource.Name, traceSource);
            }

            Assert.AreEqual(2, matchingTraceSourcesDictionary.Count);
            Assert.IsTrue(matchingTraceSourcesDictionary.ContainsKey(categories[0]));
            Assert.IsTrue(matchingTraceSourcesDictionary.ContainsKey(categories[1]));
            Assert.IsFalse(matchingTraceSourcesDictionary.ContainsKey(categories[2]));
        }
コード例 #13
0
 public void Dispose()
 {
     _logWriterImpl.Dispose();
 }