Пример #1
0
        public void DiagnosticsTracePerformanceTest()
        {
            MyTestObjectUnderTrace myObj = new MyTestObjectUnderTrace();
            int                 runs     = 100000000;
            StopWatch           sw;
            MyTestTraceListener listener;

            // use NoOp logging
            LogManager.Adapter = new NoOpLoggerFactoryAdapter();
            ILog log = LogManager.GetLogger(this.GetType());

            sw = new StopWatch();
            using (sw.Start("Time:{0} - log.InfoFormat + NoOpLogger"))
            {
                for (int i = 0; i < runs; i++)
                {
                    log.Info(m => m("some logger info {0}", (object)myObj));
                }
            }

            // Use unconfigured TraceSource
            TraceSource traceSource = new TraceSource("bla");

            sw = new StopWatch();
            using (sw.Start("Time:{0} - traceSource.TraceEvent + unconfigured TraceSource"))
            {
                for (int i = 0; i < runs; i++)
                {
                    if (traceSource.Switch.ShouldTrace(TraceEventType.Information))
                    {
                        traceSource.TraceEvent(TraceEventType.Information, -1, "some tracesource info {0}", (object)myObj);
                    }
                }
            }

            // use Common.Logging
            MyTestLoggerFactoryAdapter adapter = new MyTestLoggerFactoryAdapter(null);

            adapter.ShowLogName  = true;
            adapter.ShowDateTime = true;
            adapter.Level        = LogLevel.Warn;
            LogManager.Adapter   = adapter;
            log = LogManager.GetLogger(this.GetType());

            sw = new StopWatch();
            using (sw.Start("Time:{0} - log.InfoFormat"))
            {
                for (int i = 0; i < runs; i++)
                {
                    log.Info(m => m("some logger info {0}", (object)myObj));
                }
            }
            Assert.AreEqual(0, adapter.Messages.Count);

            // Use configured TraceSource
            traceSource = new TraceSource("DiagnosticsTracePerformanceTest");
            listener    = (MyTestTraceListener)traceSource.Listeners[0];
            sw          = new StopWatch();
            using (sw.Start("Time:{0} - traceSource.TraceEvent"))
            {
                for (int i = 0; i < runs; i++)
                {
                    if (traceSource.Switch.ShouldTrace(TraceEventType.Information))
                    {
                        traceSource.TraceEvent(TraceEventType.Information, -1, "some tracesource info {0}", (object)myObj);
                    }
                }
            }
            Assert.AreEqual(0, listener.Messages.Count);
        }
        public void DiagnosticsTracePerformanceTest()
        {
            MyTestObjectUnderTrace myObj = new MyTestObjectUnderTrace();
            int runs = 100000000;
            StopWatch sw;
            MyTestTraceListener listener;

            // use NoOp logging
            LogManager.Adapter = new NoOpLoggerFactoryAdapter();
            ILog log = LogManager.GetLogger(this.GetType());

            sw = new StopWatch();
            using (sw.Start("Time:{0} - log.InfoFormat + NoOpLogger"))
            {
                for (int i = 0; i < runs; i++)
                {
                    log.Info( m=>m("some logger info {0}", (object)myObj) );
                }
            }

            // Use unconfigured TraceSource
            TraceSource traceSource = new TraceSource("bla");
            sw = new StopWatch();
            using (sw.Start("Time:{0} - traceSource.TraceEvent + unconfigured TraceSource"))
            {
                for (int i = 0; i < runs; i++)
                {
                    if (traceSource.Switch.ShouldTrace(TraceEventType.Information))
                    {
                        traceSource.TraceEvent(TraceEventType.Information, -1, "some tracesource info {0}", (object)myObj);
                    }
                }
            }

            // use Common.Logging
            MyTestLoggerFactoryAdapter adapter = new MyTestLoggerFactoryAdapter(null);
            adapter.ShowLogName = true;
            adapter.ShowDateTime = true;
            adapter.Level = LogLevel.Warn;
            LogManager.Adapter = adapter;
            log = LogManager.GetLogger(this.GetType());

            sw = new StopWatch();
            using (sw.Start("Time:{0} - log.InfoFormat"))
            {
                for (int i = 0; i < runs; i++)
                {
                    log.Info(m => m("some logger info {0}", (object)myObj));
                }
            }
            Assert.AreEqual(0, adapter.Messages.Count);

            // Use configured TraceSource
            traceSource = new TraceSource("DiagnosticsTracePerformanceTest");
            listener = (MyTestTraceListener) traceSource.Listeners[0];
            sw = new StopWatch();
            using (sw.Start("Time:{0} - traceSource.TraceEvent"))
            {
                for (int i = 0; i < runs; i++)
                {
                    if (traceSource.Switch.ShouldTrace(TraceEventType.Information))
                    {
                        traceSource.TraceEvent(TraceEventType.Information, -1, "some tracesource info {0}", (object)myObj);
                    }
                }
            }
            Assert.AreEqual(0, listener.Messages.Count);
        }