public void RoutesToCommonLogging()
        {
            //            CommonLoggingAppender appender = new CommonLoggingAppender();
            //            appender.Layout = new PatternLayout("%level - %class.%method: %message");
            //            BasicConfigurator.Configure(stm);

            Stream stm = this.GetType().Assembly.GetManifestResourceStream(this.GetType().FullName + "_log4net.config.xml");
            XmlConfigurator.Configure(stm);

            CapturingLoggerFactoryAdapter adapter = new CapturingLoggerFactoryAdapter();
            LogManager.Adapter = adapter;
            
            string message = "testmessage";
            Exception exception = new Exception("testexception");

            adapter.ClearLastEvent();
            log4net.LogManager.GetLogger(this.GetType()).Debug(message, exception);
            Assert.AreEqual(this.GetType().FullName, adapter.LastEvent.Source.Name);
            Assert.AreEqual(string.Format("{0} - {1}.{2}: {3}", Level.Debug, this.GetType().FullName, MethodBase.GetCurrentMethod().Name ,  message), adapter.LastEvent.MessageObject.ToString());
            Assert.AreSame(exception, adapter.LastEvent.Exception);

            adapter.ClearLastEvent();
            log4net.LogManager.GetLogger(this.GetType()).Warn(message, exception);
            Assert.AreEqual(this.GetType().FullName, adapter.LastEvent.Source.Name);
            Assert.AreEqual(string.Format("{0} - {1}.{2}: {3}", Level.Warn, this.GetType().FullName, MethodBase.GetCurrentMethod().Name ,  message), adapter.LastEvent.MessageObject.ToString());
            Assert.AreSame(exception, adapter.LastEvent.Exception);
        }
        public void AcceptsNullCategory()
        {
            CapturingLoggerFactoryAdapter factoryAdapter = new CapturingLoggerFactoryAdapter();
            LogManager.Adapter = factoryAdapter;

            CommonLoggingTraceListener l = new CommonLoggingTraceListener();
            l.DefaultTraceEventType = (TraceEventType)0xFFFF;

            AssertExpectedLogLevel(l, TraceEventType.Start, LogLevel.Trace);
            AssertExpectedLogLevel(l, TraceEventType.Stop, LogLevel.Trace);
            AssertExpectedLogLevel(l, TraceEventType.Suspend, LogLevel.Trace);
            AssertExpectedLogLevel(l, TraceEventType.Resume, LogLevel.Trace);
            AssertExpectedLogLevel(l, TraceEventType.Transfer, LogLevel.Trace);
            AssertExpectedLogLevel(l, TraceEventType.Verbose, LogLevel.Debug);
            AssertExpectedLogLevel(l, TraceEventType.Information, LogLevel.Info);
            AssertExpectedLogLevel(l, TraceEventType.Warning, LogLevel.Warn);
            AssertExpectedLogLevel(l, TraceEventType.Error, LogLevel.Error);
            AssertExpectedLogLevel(l, TraceEventType.Critical, LogLevel.Fatal);

            factoryAdapter.ClearLastEvent();
            l.DefaultTraceEventType = TraceEventType.Warning;
            l.Write("some message", null);
            Assert.AreEqual(string.Format("{0}.{1}", l.Name, ""), factoryAdapter.LastEvent.Source.Name);
            Assert.AreEqual(LogLevel.Warn, factoryAdapter.LastEvent.Level);
            Assert.AreEqual("some message", factoryAdapter.LastEvent.RenderedMessage);
            Assert.AreEqual(null, factoryAdapter.LastEvent.Exception);
        }
示例#3
0
        public void AcceptsNullCategory()
        {
            CapturingLoggerFactoryAdapter factoryAdapter = new CapturingLoggerFactoryAdapter();

            LogManager.Adapter = factoryAdapter;

            CommonLoggingTraceListener l = new CommonLoggingTraceListener();

            l.DefaultTraceEventType = (TraceEventType)0xFFFF;

            AssertExpectedLogLevel(l, TraceEventType.Start, LogLevel.Trace);
            AssertExpectedLogLevel(l, TraceEventType.Stop, LogLevel.Trace);
            AssertExpectedLogLevel(l, TraceEventType.Suspend, LogLevel.Trace);
            AssertExpectedLogLevel(l, TraceEventType.Resume, LogLevel.Trace);
            AssertExpectedLogLevel(l, TraceEventType.Transfer, LogLevel.Trace);
            AssertExpectedLogLevel(l, TraceEventType.Verbose, LogLevel.Debug);
            AssertExpectedLogLevel(l, TraceEventType.Information, LogLevel.Info);
            AssertExpectedLogLevel(l, TraceEventType.Warning, LogLevel.Warn);
            AssertExpectedLogLevel(l, TraceEventType.Error, LogLevel.Error);
            AssertExpectedLogLevel(l, TraceEventType.Critical, LogLevel.Fatal);

            factoryAdapter.ClearLastEvent();
            l.DefaultTraceEventType = TraceEventType.Warning;
            l.Write("some message", null);
            Assert.AreEqual(string.Format("{0}.{1}", l.Name, ""), factoryAdapter.LastEvent.Source.Name);
            Assert.AreEqual(LogLevel.Warn, factoryAdapter.LastEvent.Level);
            Assert.AreEqual("some message", factoryAdapter.LastEvent.RenderedMessage);
            Assert.AreEqual(null, factoryAdapter.LastEvent.Exception);
        }
        public void DoesNotLogBelowFilterLevel()
        {
            CapturingLoggerFactoryAdapter factoryAdapter = new CapturingLoggerFactoryAdapter();
            LogManager.Adapter = factoryAdapter;

            CommonLoggingTraceListener l = new CommonLoggingTraceListener();
            l.Filter = new EventTypeFilter(SourceLevels.Warning);
            factoryAdapter.ClearLastEvent();
            l.TraceEvent(null, "sourceName", TraceEventType.Information, -1, "format {0}", "Information");
            Assert.AreEqual(null, factoryAdapter.LastEvent);

            AssertExpectedLogLevel(l, TraceEventType.Warning, LogLevel.Warn);
            AssertExpectedLogLevel(l, TraceEventType.Error, LogLevel.Error);
        }
        public void AdapterClearsEvents()
        {
            CapturingLoggerFactoryAdapter adapter = new CapturingLoggerFactoryAdapter();
            CapturingLogger testLogger = (CapturingLogger) adapter.GetLogger("test");
            testLogger.Trace("message1");
            testLogger.Trace("message2");
            Assert.IsNotNull(adapter.LastEvent);
            Assert.AreEqual(2, adapter.LoggerEvents.Count);

            adapter.ClearLastEvent();
            Assert.IsNull(adapter.LastEvent);
            adapter.Clear();
            Assert.IsNull(adapter.LastEvent);
            Assert.AreEqual(0, adapter.LoggerEvents.Count);
        }
示例#6
0
        public void DoesNotLogBelowFilterLevel()
        {
            CapturingLoggerFactoryAdapter factoryAdapter = new CapturingLoggerFactoryAdapter();

            LogManager.Adapter = factoryAdapter;

            CommonLoggingTraceListener l = new CommonLoggingTraceListener();

            l.Filter = new EventTypeFilter(SourceLevels.Warning);
            factoryAdapter.ClearLastEvent();
            l.TraceEvent(null, "sourceName", TraceEventType.Information, -1, "format {0}", "Information");
            Assert.AreEqual(null, factoryAdapter.LastEvent);

            AssertExpectedLogLevel(l, TraceEventType.Warning, LogLevel.Warn);
            AssertExpectedLogLevel(l, TraceEventType.Error, LogLevel.Error);
        }
        public void AdapterClearsEvents()
        {
            CapturingLoggerFactoryAdapter adapter = new CapturingLoggerFactoryAdapter();
            CapturingLogger testLogger            = (CapturingLogger)adapter.GetLogger("test");

            testLogger.Trace("message1");
            testLogger.Trace("message2");
            Assert.IsNotNull(adapter.LastEvent);
            Assert.AreEqual(2, adapter.LoggerEvents.Count);

            adapter.ClearLastEvent();
            Assert.IsNull(adapter.LastEvent);
            adapter.Clear();
            Assert.IsNull(adapter.LastEvent);
            Assert.AreEqual(0, adapter.LoggerEvents.Count);
        }
        public void RoutesToCommonLogging()
        {

            // configure for capturing
            CapturingLoggerFactoryAdapter adapter = new CapturingLoggerFactoryAdapter();
            LogManager.Adapter = adapter;
            var configuration = new LoggerConfiguration().ReadFrom.AppSettings().Enrich.WithProperty("Common.Logging.Type", typeof(CommonLoggingSerilogTests).FullName);
            var logger = configuration.CreateLogger();

            var exception = new Exception();

            adapter.ClearLastEvent();

            var position = new { Latitude = 25, Longitude = 134 };
            var elapsedMs = 34;
            logger.Error(exception, "Processed {@Position} in {Elapsed:000} ms.", position, elapsedMs);

            Assert.AreEqual(typeof(CommonLoggingSerilogTests).FullName, adapter.LastEvent.Source.Name);
            Assert.AreEqual("Processed { Latitude: 25, Longitude: 134 } in 034 ms.", adapter.LastEvent.RenderedMessage);
            Assert.AreSame(exception, adapter.LastEvent.Exception);
        }
        public void RoutesToCommonLogging()
        {
            LoggingConfiguration cfg = new LoggingConfiguration();
            CommonLoggingTarget target = new CommonLoggingTarget("${level:uppercase=true}|${logger}|${message}");
            cfg.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, target));

            NLogLogManager.Configuration = cfg;

            // configure for capturing
            CapturingLoggerFactoryAdapter adapter = new CapturingLoggerFactoryAdapter();
            LogManager.Adapter = adapter;

            string msg = "testmessage";

            Exception ex = new Exception("testexception");

            adapter.ClearLastEvent();
            NLogLogManager.GetLogger("myLogger").DebugException(msg, ex);
            Assert.AreEqual("myLogger", adapter.LastEvent.Source.Name);
            Assert.AreEqual(string.Format("DEBUG|myLogger|{0}", msg), adapter.LastEvent.RenderedMessage);
            Assert.AreSame(ex, adapter.LastEvent.Exception);
        }