Example #1
0
        public void LogWriterExceptionsDontPropagate()
        {
            var setupLog           = new SetupLog();
            var exceptionLogWriter = new ExceptionThrowingLogWriter <TraceEntry>(setupLog);
            var listLogWriter      = new ListLogWriter <TraceEntry>(setupLog);

            var traceManager = new TraceManager(setupLog);

            traceManager.Config.Writers.Add(new TraceWriterConfig()
            {
                LogWriterConfig = new UseExistingLogWriterConfig(exceptionLogWriter, disposeOnStop: true),
                Switches        =
                {
                    { Tracer.All, new OnOffTraceSwitch(true) }
                }
            });
            traceManager.Config.Writers.Add(new TraceWriterConfig()
            {
                LogWriterConfig = new UseExistingLogWriterConfig(listLogWriter, disposeOnStop: true),
                Switches        =
                {
                    { Tracer.All, new OnOffTraceSwitch(true) }
                }
            });

            using (traceManager)
            {
                traceManager.Start();

                var tracer = traceManager.TracerFor(this);
                tracer.Info("Info");
                tracer.Warn("Warn");

                Assert.Equal(2, listLogWriter.Count());
                Assert.Equal(2, exceptionLogWriter.CountExceptionsThrown);

                // First write exception is reported in SetupLog
                // TODO: Replace logging to SetupLog with TraceWriter reporting its current status
                Assert.Equal(1, traceManager.SetupLog.Count(traceEntry => traceEntry.TraceLevel >= TraceLevel.Error && traceEntry.Details != null));
            }

            Assert.Equal(3, exceptionLogWriter.CountExceptionsThrown);

            // Exceptions should be reported in the SetupLog
            Assert.Equal(2, traceManager.SetupLog.Count(traceEntry => traceEntry.TraceLevel >= TraceLevel.Error && traceEntry.Details != null));
        }
        public void InnerWriterExceptionsAreHandled()
        {
            var        innerLogWriter           = new ExceptionThrowingLogWriter <MessageEntry>(SetupLog);
            var        backgroundMultiLogWriter = new BackgroundMultiLogWriter(SetupLog);
            ILogWriter logWriter = backgroundMultiLogWriter.CreateProxyFor(innerLogWriter);
            IEntryWriter <MessageEntry> entryWriter;

            Assert.True(logWriter.TryGetEntryWriter(out entryWriter));

            using (backgroundMultiLogWriter)
            {
                backgroundMultiLogWriter.Start();
                ExampleHelper.LogTestMessages(entryWriter, 6);
            }

            _testOutputHelper.WriteLine("Setup messages:");
            _testOutputHelper.WriteEntries(SetupLog);
            Assert.Equal(7, SetupLog.Where((traceEntry, index) => traceEntry.TraceLevel >= TraceLevel.Error).Count());
        }