Example #1
0
        public void ConfigTraceSourceTest()
        {
            TraceSource _tracer = new TraceSource("CAS.CodeProtect.UnitTests.TraceSource");

            Assert.IsNotNull(_tracer);
            Assert.AreEqual(1, _tracer.Listeners.Count);
            Dictionary <string, TraceListener> _listeners = _tracer.Listeners.Cast <TraceListener>().ToDictionary <TraceListener, string>(x => x.Name);

            Assert.IsTrue(_listeners.ContainsKey("LogFile"));
            TraceListener _listener = _listeners["LogFile"];

            Assert.IsNotNull(_listener);
            Assert.IsInstanceOfType(_listener, typeof(AdvancedDelimitedListTraceListener));
            AdvancedDelimitedListTraceListener _advancedListener = _listener as AdvancedDelimitedListTraceListener;

            Assert.IsNotNull(_advancedListener.Filter);
            Assert.IsInstanceOfType(_advancedListener.Filter, typeof(EventTypeFilter));
            EventTypeFilter _eventTypeFilter = _advancedListener.Filter as EventTypeFilter;

            Assert.AreEqual(SourceLevels.All, _eventTypeFilter.EventType);
            //Writer
            using (TestTextWriter _writer = new TestTextWriter())
            {
                _advancedListener.Writer = _writer;
                _tracer.TraceEvent(TraceEventType.Error, 0, "Test message");
                Assert.IsTrue(_writer.WriteCount > 5);
                int _writeCount = _writer.WriteCount;
                _advancedListener.Flush();
                Assert.AreEqual <int>(_writeCount, _writer.WriteCount);
                _advancedListener.Close();
                Assert.AreEqual <int>(_writeCount, _writer.WriteCount);
            }

            //Test Switch
            Assert.IsNotNull(_tracer.Switch);
            Assert.AreEqual <string>("CAS.CodeProtect.UnitTests.TraceSource.Switch", _tracer.Switch.DisplayName);
            Assert.AreEqual <SourceLevels>(SourceLevels.All, _tracer.Switch.Level);

            //Trace
            Assert.IsFalse(Trace.Listeners.Cast <TraceListener>().Where <TraceListener>(x => x.Name == "LogFile").Any <TraceListener>());
        }