Пример #1
0
        public void AddNewTracerConfigToSameWriterAfterStarting()
        {
            var setupTracerFactory = new SetupLog();
            var logWriter          = new ListLogWriter <TraceEntry>(setupTracerFactory);

            var config = new TraceManagerConfig();

            config.UseLogWriter(logWriter, "A"); // Only logs for tracer A

            TraceManager traceManager;

            using (traceManager = new TraceManager(config, setupTracerFactory))
            {
                var tracerA = traceManager.GetTracer("A");
                var tracerB = traceManager.GetTracer("B");
                Assert.True(traceManager.IsStarted);

                Assert.True(tracerA.IsInfoEnabled());
                Assert.False(tracerB.IsInfoEnabled());

                // For option 1, see the previous test: ConfigCanBeChangedAfterStarting

                // Option 2: Add a new TraceWriterConfig, and restart, to enable B
                config.UseLogWriter(logWriter, "B", new OnOffTraceSwitch(true));
                Assert.False(tracerB.IsInfoEnabled()); // before the restart, tracerB is disabled
                traceManager.Start();

                Assert.True(tracerB.IsInfoEnabled());
                Assert.True(tracerB.IsDebugEnabled());
            }
        }
Пример #2
0
        public void ConfigCanBeChangedAfterStarting()
        {
            var setupTracerFactory = new SetupLog();
            var logWriter          = new ListLogWriter <TraceEntry>(setupTracerFactory);

            var config = new TraceManagerConfig();
            // TODO: Rename to tracerConfig?
            var traceWriterConfig = config.UseLogWriter(logWriter, "A"); // Only logs for tracer A

            TraceManager traceManager;

            using (traceManager = new TraceManager(config, setupTracerFactory))
            {
                var tracerA = traceManager.GetTracer("A");
                var tracerB = traceManager.GetTracer("B");

                Assert.True(traceManager.IsStarted);
                Assert.True(traceManager.LogManager.IsStarted);

                Assert.True(tracerA.IsInfoEnabled());
                Assert.False(tracerB.IsInfoEnabled());

                // Option 1: Change the config
                traceWriterConfig.Switches.Add("B", new OnOffTraceSwitch(true));
                traceManager.Start(); // Explicit restart required

                Assert.True(tracerB.IsInfoEnabled());
                Assert.True(tracerB.IsDebugEnabled());

                // For option 2, see the next test: AddNewTracerConfigToSameWriterAfterStarting
            }
        }
Пример #3
0
        public void UnitTestTracingWithGlobalTraceManager(ConfigForm configForm, int iteration)
        {
            // It can occur that the Tracer was obtained before the test starts
            Tracer tracer = TraceManager.Instance.TracerFor(this);

            // In a real app you probably don't have to reset the LogManager + TraceManager before configuring.
            // However for unit testing it can be a good idea to clear configuration from previous tests.
            LogManager.Instance.Reset(true);
            TraceManager.Instance.Reset(true);
            Assert.Same(LogManager.Instance, TraceManager.Instance.LogManager);

            // Traces sent to this list
            var setupTracerFactory = TraceManager.Instance.SetupTracerFactory;
            var listWriter         = new ListLogWriter <TraceEntry>(setupTracerFactory);

            // Add the list TraceWriter only for this class
            TraceManagerConfig config = TraceManager.Instance.Config;
            TraceWriterConfig  listTraceConfig;

            if (configForm == ConfigForm.ObjectGraph)
            {
                listTraceConfig = new TraceWriterConfig(listWriter)
                {
                    Switches =
                    {
                        { GetType(), new OnOffTraceSwitch(true) }
                    }
                };
                config.Writers.Add(listTraceConfig);
            }
            else if (configForm == ConfigForm.Fluent)
            {
                listTraceConfig = config.UseLogWriter(listWriter, GetType(), new OnOffTraceSwitch(true));
            }
            else
            {
                throw new NotImplementedException();
            }

            // restart to load config and assign writers
            TraceManager.Instance.Start();

            // Ensure start didn't result in any errors
            Assert.True(TraceManager.Instance.IsHealthy);

            tracer.Info("Info message");
            tracer.Debug("Debug message");
            Assert.Equal(2, listWriter.Count);

            // Remove the TraceWriterConfig just to ensure that everything returns to normal
            Assert.True(TraceManager.Instance.Config.Writers.Remove(listTraceConfig));
            // restart to reset config
            TraceManager.Instance.Start();

            LogJam.Internal.UnitTests.Trace.TraceManagerConfigTests.AssertEquivalentToDefaultTraceManagerConfig(TraceManager.Instance);

            // Now tracing goes to the debug window only, but not to the list
            tracer.Info("Not logged to list, but logged to debug out.");
            Assert.Equal(2, listWriter.Count);
        }