Esempio n. 1
0
        public void Trace_FiltersBySourceAndLevel()
        {
            _mockTextWriter.Setup(p => p.WriteLine("Test Information"));

            _mockTraceWriter.Setup(p => p.Trace(TraceLevel.Info, Host.TraceSource.Host, "Test Information", null));
            _mockTraceWriter.Setup(p => p.Trace(TraceLevel.Info, "TestSource", "Test Information With Source", null));
            _mockTraceWriter.Setup(p => p.Trace(TraceLevel.Info, null, "Test Information No Source", null));
            _mockTraceWriter.Setup(p => p.Trace(TraceLevel.Warning, null, "Test Warning", null));
            _mockTraceWriter.Setup(p => p.Trace(TraceLevel.Error, null, "Test Error", null));
            Exception ex = new Exception("Kaboom!");

            _mockTraceWriter.Setup(p => p.Trace(TraceLevel.Error, null, "Test Error With Exception", ex));

            // expect these to be logged to both
            _traceWriter.Info("Test Information", Host.TraceSource.Host);

            // don't expect these to be logged to text writer (based on level filter)
            _traceWriter.Warning("Test Warning");
            _traceWriter.Error("Test Error");
            _traceWriter.Error("Test Error With Exception", ex);

            // don't expect these to be logged to text writer (based on source filter)
            _traceWriter.Info("Test Information With Source", "TestSource");
            _traceWriter.Info("Test Information No Source");

            _mockTextWriter.VerifyAll();
            _mockTraceWriter.VerifyAll();
        }
        public void Trace_FiltersBySourceAndLevel()
        {
            _mockTextWriter.Setup(p => p.WriteLine("Test Information"));

            _mockTraceWriter.Setup(p => p.Trace(It.Is <TraceEvent>(
                                                    q => q.Level == TraceLevel.Info &&
                                                    q.Source == Host.TraceSource.Host &&
                                                    q.Message == "Test Information")));
            _mockTraceWriter.Setup(p => p.Trace(It.Is <TraceEvent>(
                                                    q => q.Level == TraceLevel.Info &&
                                                    q.Source == "TestSource" &&
                                                    q.Message == "Test Information With Source")));
            _mockTraceWriter.Setup(p => p.Trace(It.Is <TraceEvent>(
                                                    q => q.Level == TraceLevel.Info &&
                                                    q.Source == null &&
                                                    q.Message == "Test Information No Source")));
            _mockTraceWriter.Setup(p => p.Trace(It.Is <TraceEvent>(q =>
                                                                   q.Level == TraceLevel.Warning &&
                                                                   q.Source == null &&
                                                                   q.Message == "Test Warning")));
            _mockTraceWriter.Setup(p => p.Trace(It.Is <TraceEvent>(q =>
                                                                   q.Level == TraceLevel.Error &&
                                                                   q.Source == null &&
                                                                   q.Message == "Test Error")));
            _mockTraceWriter2.Setup(p => p.Trace(It.Is <TraceEvent>(q =>
                                                                    q.Level == TraceLevel.Error &&
                                                                    q.Source == null &&
                                                                    q.Message == "Test Error")));
            Exception ex = new Exception("Kaboom!");

            _mockTraceWriter.Setup(p => p.Trace(It.Is <TraceEvent>(q =>
                                                                   q.Level == TraceLevel.Error &&
                                                                   q.Source == null &&
                                                                   q.Message == "Test Error With Exception")));
            _mockTraceWriter2.Setup(p => p.Trace(It.Is <TraceEvent>(q =>
                                                                    q.Level == TraceLevel.Error &&
                                                                    q.Source == null &&
                                                                    q.Message == "Test Error With Exception")));

            // expect this to be logged to both tracer1 and textWriter
            _traceWriter.Info("Test Information", Host.TraceSource.Host);

            // don't expect these to be logged to text writer (based on level filter)
            // expect the errors to be logged to both traceWriters
            _traceWriter.Warning("Test Warning");
            _traceWriter.Error("Test Error");
            _traceWriter.Error("Test Error With Exception", ex);

            // don't expect these to be logged to text writer (based on source filter)
            // only written to traceWriter1
            _traceWriter.Info("Test Information With Source", "TestSource");
            _traceWriter.Info("Test Information No Source");

            _mockTextWriter.VerifyAll();
            _mockTraceWriter.VerifyAll();
            _mockTraceWriter2.VerifyAll();
        }
Esempio n. 3
0
        public void Trace_FiltersByLevel()
        {
            _mockTextWriter.Setup(p => p.WriteLine("Test Information"));
            _mockTextWriter.Setup(p => p.WriteLine("Test Warning"));
            _mockTextWriter.Setup(p => p.WriteLine("Test Error"));
            _mockTextWriter.Setup(p => p.WriteLine("Test Error With Exception"));
            _mockTextWriter.Setup(p => p.WriteLine("System.Exception: Kaboom!"));
            _mockTextWriter.Setup(p => p.WriteLine("Test Information With Source"));
            _mockTextWriter.Setup(p => p.WriteLine("Test Information No Source"));

            _mockTraceWriter.Setup(p => p.Trace(It.Is <TraceEvent>(
                                                    q => q.Level == TraceLevel.Info &&
                                                    q.Source == Host.TraceSource.Host &&
                                                    q.Message == "Test Information")));
            _mockTraceWriter.Setup(p => p.Trace(It.Is <TraceEvent>(
                                                    q => q.Level == TraceLevel.Info &&
                                                    q.Source == "TestSource" &&
                                                    q.Message == "Test Information With Source")));
            _mockTraceWriter.Setup(p => p.Trace(It.Is <TraceEvent>(
                                                    q => q.Level == TraceLevel.Info &&
                                                    q.Source == null &&
                                                    q.Message == "Test Information No Source")));
            _mockTraceWriter.Setup(p => p.Trace(It.Is <TraceEvent>(q =>
                                                                   q.Level == TraceLevel.Warning &&
                                                                   q.Source == null &&
                                                                   q.Message == "Test Warning")));
            _mockTraceWriter.Setup(p => p.Trace(It.Is <TraceEvent>(q =>
                                                                   q.Level == TraceLevel.Error &&
                                                                   q.Source == null &&
                                                                   q.Message == "Test Error")));
            _mockTraceWriter2.Setup(p => p.Trace(It.Is <TraceEvent>(q =>
                                                                    q.Level == TraceLevel.Error &&
                                                                    q.Source == null &&
                                                                    q.Message == "Test Error")));
            Exception ex = new Exception("Kaboom!");

            _mockTraceWriter.Setup(p => p.Trace(It.Is <TraceEvent>(q =>
                                                                   q.Level == TraceLevel.Error &&
                                                                   q.Source == null &&
                                                                   q.Message == "Test Error With Exception")));
            _mockTraceWriter2.Setup(p => p.Trace(It.Is <TraceEvent>(q =>
                                                                    q.Level == TraceLevel.Error &&
                                                                    q.Source == null &&
                                                                    q.Message == "Test Error With Exception")));

            // expect these to be logged everywhere
            _traceWriter.Info("Test Information", Host.TraceSource.Host);
            _traceWriter.Warning("Test Warning");
            _traceWriter.Error("Test Error");
            _traceWriter.Error("Test Error With Exception", ex);
            _traceWriter.Info("Test Information With Source", "TestSource");
            _traceWriter.Info("Test Information No Source");

            _mockTextWriter.VerifyAll();
            _mockTraceWriter.VerifyAll();
            _mockTraceWriter2.VerifyAll();
        }
        public void Trace_AppliesLevelFilter()
        {
            try
            {
                var traceWriter = new ConsoleTraceWriter(TraceLevel.Info);
                using (var writer = new StringWriter())
                {
                    Console.SetOut(writer);

                    traceWriter.Verbose("Test Verbose");
                    traceWriter.Info("Test Info");
                    traceWriter.Warning("Test Warning");
                    traceWriter.Error("Test Error");

                    traceWriter.Flush();

                    string   output = writer.ToString();
                    string[] lines  = output.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                    Assert.Equal(3, lines.Length);
                    Assert.Equal("Test Info", lines[0]);
                    Assert.Equal("Test Warning", lines[1]);
                    Assert.Equal("Test Error", lines[2]);
                    Assert.False(output.Contains("Trace Verbose"));
                }
            }
            finally
            {
                var standardOutput = new StreamWriter(Console.OpenStandardOutput());
                standardOutput.AutoFlush = true;
                Console.SetOut(standardOutput);
            }
        }