public void BracketsInStackTracesKeepLogWellFormed() { LayoutLoggerXml layout = new LayoutLoggerXml(); StringAppender stringAppender = new StringAppender(); stringAppender.Layout = layout; ILoggerController rep = LogManager.CreateController(Guid.NewGuid().ToString()); BasicConfig.Config(rep, stringAppender); ILog log1 = LogManager.Get(rep.Name, "TestLogger"); Action <int> bar = foo => { try { throw new NullReferenceException(); } catch (Exception ex) { log1.Error(string.Format("Error {0}", foo), ex); } }; bar(42); // really only asserts there is no exception var loggedDoc = new XmlDocument(); loggedDoc.LoadXml(stringAppender.GetString()); }
public void TestThreadStackPattern2() { StringAppender stringAppender = new StringAppender(); stringAppender.Layout = new LayoutPattern("%property{" + Utils.PROPERTY_KEY + "}"); ILoggerController rep = LogManager.CreateController(Guid.NewGuid().ToString()); BasicConfig.Config(rep, stringAppender); ILog log1 = LogManager.Get(rep.Name, "TestThreadStackPattern"); log1.Info("TestMessage"); Assert.AreEqual(TextDefault.NullText, stringAppender.GetString(), "Test no thread stack value set"); stringAppender.Reset(); using (ThreadContext.Stacks[Utils.PROPERTY_KEY].Push("val1")) { log1.Info("TestMessage"); Assert.AreEqual("val1", stringAppender.GetString(), "Test thread stack value set"); stringAppender.Reset(); using (ThreadContext.Stacks[Utils.PROPERTY_KEY].Push("val2")) { log1.Info("TestMessage"); Assert.AreEqual("val1 val2", stringAppender.GetString(), "Test thread stack value pushed 2nd val"); stringAppender.Reset(); } } log1.Info("TestMessage"); Assert.AreEqual(TextDefault.NullText, stringAppender.GetString(), "Test thread stack value removed"); stringAppender.Reset(); }
public void MethodNameCategoryTest() { CategoryTraceListener categoryTraceListener = new CategoryTraceListener(); Trace.Listeners.Clear(); Trace.Listeners.Add(categoryTraceListener); ILoggerController rep = LogManager.CreateController(Guid.NewGuid().ToString()); AppenderTrace traceAppender = new AppenderTrace(); LayoutPattern methodLayout = new LayoutPattern("%method"); methodLayout.Activate(); traceAppender.Category = methodLayout; traceAppender.Layout = new LayoutLoggerSimple(); traceAppender.Activate(); BasicConfig.Config(rep, traceAppender); ILog log = LogManager.Get(rep.Name, GetType()); log.Debug("Message"); Assert.AreEqual( System.Reflection.MethodInfo.GetCurrentMethod().Name, categoryTraceListener.Category); }
public void TestShutdownAndReconfigure() { ILoggerController rep = LogManager.CreateController(Guid.NewGuid().ToString()); StringAppender stringAppender = new StringAppender(); stringAppender.Layout = new LayoutPattern("%m"); BasicConfig.Config(rep, stringAppender); ILog log1 = LogManager.Get(rep.Name, "logger1"); log1.Info("TestMessage1"); Assert.AreEqual("TestMessage1", stringAppender.GetString(), "Test logging configured"); stringAppender.Reset(); rep.Terminate(); log1.Info("TestMessage2"); Assert.AreEqual("", stringAppender.GetString(), "Test not logging while shutdown"); stringAppender.Reset(); stringAppender = new StringAppender(); stringAppender.Layout = new LayoutPattern("%m"); BasicConfig.Config(rep, stringAppender); log1.Info("TestMessage3"); Assert.AreEqual("TestMessage3", stringAppender.GetString(), "Test logging re-configured"); stringAppender.Reset(); }
public void TestGlobalPropertiesPattern() { StringAppender stringAppender = new StringAppender(); stringAppender.Layout = NewPatternLayout("%property{" + Utils.PROPERTY_KEY + "}"); ILoggerController rep = LogManager.CreateController(Guid.NewGuid().ToString()); BasicConfig.Config(rep, stringAppender); ILog log1 = LogManager.Get(rep.Name, "TestGlobalProperiesPattern"); log1.Info("TestMessage"); Assert.AreEqual(TextDefault.NullText, stringAppender.GetString(), "Test no global properties value set"); stringAppender.Reset(); GlobalContext.Set(Utils.PROPERTY_KEY, "val1"); log1.Info("TestMessage"); Assert.AreEqual("val1", stringAppender.GetString(), "Test global properties value set"); stringAppender.Reset(); GlobalContext.Remove(Utils.PROPERTY_KEY); log1.Info("TestMessage"); Assert.AreEqual(TextDefault.NullText, stringAppender.GetString(), "Test global properties value removed"); stringAppender.Reset(); }
public void TestThreadSafety() { ILoggerController rep = LogManager.CreateController(Guid.NewGuid().ToString()); var memoryAppender = new AppenderMemory(); var patternLayout = new LayoutPattern(); memoryAppender.Layout = patternLayout; memoryAppender.Activate(); BasicConfig.Config(rep, memoryAppender); cThreadsRunning = cThreadsMax; var threads = Enumerable.Range(0, cThreadsMax) .Select(i => new Thread(LogMessages(rep.Name))) .ToList(); foreach (var thread in threads) { thread.Start(); } long cEventsRead = 0; while (cThreadsRunning > 0) { var events = memoryAppender.PopAllInputs(); cEventsRead += events.Length; } foreach (var thread in threads) { thread.Join(); } cEventsRead += memoryAppender.PopAllInputs().Length; Assert.AreEqual(cEventsExpected, cEventsRead, "Log events were lost."); }
public void TestLogFormatApi_NoException() { StringAppender stringAppender = new StringAppender(); stringAppender.Threshold = Level.Off; stringAppender.Layout = new LayoutPattern("%level:%message"); ILoggerController rep = LogManager.CreateController(Guid.NewGuid().ToString()); BasicConfig.Config(rep, stringAppender); ILog log1 = LogManager.Get(rep.Name, "TestLogFormatApi_Exception"); // *** log1.Exception("TestMessage"); Assert.AreEqual("", stringAppender.GetString(), "Test simple EXCEPTION event 1"); stringAppender.Reset(); // *** log1.Exception("TestMessage", null); Assert.AreEqual("", stringAppender.GetString(), "Test simple EXCEPTION event 2"); stringAppender.Reset(); // *** log1.Exception("TestMessage", new Exception("Exception message")); Assert.AreEqual("", stringAppender.GetString(), "Test simple EXCEPTION event 3"); stringAppender.Reset(); // *** log1.ExceptionFormat("a{0}", "1"); Assert.AreEqual("", stringAppender.GetString(), "Test formatted EXCEPTION event with 1 parm"); stringAppender.Reset(); // *** log1.ExceptionFormat("a{0}b{1}", "1", "2"); Assert.AreEqual("", stringAppender.GetString(), "Test formatted EXCEPTION event with 2 parm"); stringAppender.Reset(); // *** log1.ExceptionFormat("a{0}b{1}c{2}", "1", "2", "3"); Assert.AreEqual("", stringAppender.GetString(), "Test formatted EXCEPTION event with 3 parm"); stringAppender.Reset(); // *** log1.ExceptionFormat("a{0}b{1}c{2}d{3}e{4}f", "Q", "W", "E", "R", "T", "Y"); Assert.AreEqual("", stringAppender.GetString(), "Test formatted EXCEPTION event with 5 parms (only 4 used)"); stringAppender.Reset(); // *** log1.ExceptionFormat(null, "Before {0} After {1}", "Middle", "End"); Assert.AreEqual("", stringAppender.GetString(), "Test formatting with null provider"); stringAppender.Reset(); // *** log1.ExceptionFormat(new CultureInfo("en"), "Before {0} After {1}", "Middle", "End"); Assert.AreEqual("", stringAppender.GetString(), "Test formatting with 'en' provider"); stringAppender.Reset(); }
public void TestLogFormatApi_Debug() { StringAppender stringAppender = new StringAppender(); stringAppender.Layout = new LayoutPattern("%level:%message"); ILoggerController rep = LogManager.CreateController(Guid.NewGuid().ToString()); BasicConfig.Config(rep, stringAppender); ILog log1 = LogManager.Get(rep.Name, "TestLogFormatApi_Debug"); // *** log1.Debug("TestMessage"); Assert.AreEqual("DEBUG:TestMessage", stringAppender.GetString(), "Test simple DEBUG event 1"); stringAppender.Reset(); // *** log1.Debug("TestMessage", null); Assert.AreEqual("DEBUG:TestMessage", stringAppender.GetString(), "Test simple DEBUG event 2"); stringAppender.Reset(); // *** log1.Debug("TestMessage", new Exception("Exception message")); Assert.AreEqual("DEBUG:TestMessage\r\nException: Message: Exception message" + Environment.NewLine + "System.Exception: Exception message" + Environment.NewLine, stringAppender.GetString(), "Test simple DEBUG event 3"); stringAppender.Reset(); // *** log1.DebugFormat("a{0}", "1"); Assert.AreEqual("DEBUG:a1", stringAppender.GetString(), "Test formatted DEBUG event with 1 parm"); stringAppender.Reset(); // *** log1.DebugFormat("a{0}b{1}", "1", "2"); Assert.AreEqual("DEBUG:a1b2", stringAppender.GetString(), "Test formatted DEBUG event with 2 parm"); stringAppender.Reset(); // *** log1.DebugFormat("a{0}b{1}c{2}", "1", "2", "3"); Assert.AreEqual("DEBUG:a1b2c3", stringAppender.GetString(), "Test formatted DEBUG event with 3 parm"); stringAppender.Reset(); // *** log1.DebugFormat("a{0}b{1}c{2}d{3}e{4}f", "Q", "W", "E", "R", "T", "Y"); Assert.AreEqual("DEBUG:aQbWcEdReTf", stringAppender.GetString(), "Test formatted DEBUG event with 5 parms (only 4 used)"); stringAppender.Reset(); // *** log1.DebugFormat(null, "Before {0} After {1}", "Middle", "End"); Assert.AreEqual("DEBUG:Before Middle After End", stringAppender.GetString(), "Test formatting with null provider"); stringAppender.Reset(); // *** log1.DebugFormat(new CultureInfo("en"), "Before {0} After {1}", "Middle", "End"); Assert.AreEqual("DEBUG:Before Middle After End", stringAppender.GetString(), "Test formatting with 'en' provider"); stringAppender.Reset(); }
public void TestFormatString() { StringAppender stringAppender = new StringAppender(); stringAppender.Layout = new LayoutPattern("%message"); ILoggerController rep = LogManager.CreateController(Guid.NewGuid().ToString()); BasicConfig.Config(rep, stringAppender); ILog log1 = LogManager.Get(rep.Name, "TestFormatString"); // *** log1.Info("TestMessage"); Assert.AreEqual("TestMessage", stringAppender.GetString(), "Test simple INFO event"); stringAppender.Reset(); // *** log1.DebugFormat("Before {0} After", "Middle"); Assert.AreEqual("Before Middle After", stringAppender.GetString(), "Test simple formatted DEBUG event"); stringAppender.Reset(); // *** log1.InfoFormat("Before {0} After", "Middle"); Assert.AreEqual("Before Middle After", stringAppender.GetString(), "Test simple formatted INFO event"); stringAppender.Reset(); // *** log1.WarnFormat("Before {0} After", "Middle"); Assert.AreEqual("Before Middle After", stringAppender.GetString(), "Test simple formatted WARN event"); stringAppender.Reset(); // *** log1.ErrorFormat("Before {0} After", "Middle"); Assert.AreEqual("Before Middle After", stringAppender.GetString(), "Test simple formatted ERROR event"); stringAppender.Reset(); // *** log1.ExceptionFormat("Before {0} After", "Middle"); Assert.AreEqual("Before Middle After", stringAppender.GetString(), "Test simple formatted FATAL event"); stringAppender.Reset(); // *** log1.InfoFormat("Before {0} After {1}", "Middle", "End"); Assert.AreEqual("Before Middle After End", stringAppender.GetString(), "Test simple formatted INFO event 2"); stringAppender.Reset(); // *** log1.InfoFormat("IGNORE THIS WARNING - EXCEPTION EXPECTED Before {0} After {1} {2}", "Middle", "End"); Assert.AreEqual(STRING_FORMAT_ERROR, stringAppender.GetString(), "Test formatting error"); stringAppender.Reset(); }
private void SetupController() { controller_ = new LoggerController(); eventRaisingAppender_ = new EventRaisingAppender(); eventRaisingAppender_.LoggingEventAppended += eventRaisingAppender_LoggingEventAppended; controller_.Root.Level = Level.All; controller_.Root.AddAppender(eventRaisingAppender_); BasicConfig.Config(controller_, eventRaisingAppender_); logger_ = controller_.Get("test"); }
public void AddingMultipleAppenders2() { CountingAppender alpha = new CountingAppender(); CountingAppender beta = new CountingAppender(); BasicConfig.Config(alpha, beta); ILog log = LogManager.Get(GetType()); log.Debug("Hello World"); Assert.AreEqual(1, alpha.Counter); Assert.AreEqual(1, beta.Counter); }
public void TestExceptionEvaluatorNoTriggerOnSubClass() { bufferingForwardingAppender_.Trigger = new TriggerLoggerException(typeof(Exception), false); bufferingForwardingAppender_.Activate(); BasicConfig.Config(controller_, bufferingForwardingAppender_); ILogger logger = controller_.Get("TestExceptionEvaluatorNoTriggerOnSubClass"); logger.Log(typeof(EvaluatorTest), Level.Warn, "Warn message logged", null); logger.Log(typeof(EvaluatorTest), Level.Warn, "Warn message logged", null); Assert.AreEqual(0, countingAppender_.Counter, "Test 2 events buffered"); logger.Log(typeof(EvaluatorTest), Level.Warn, "Warn message logged", new ApplicationException()); Assert.AreEqual(0, countingAppender_.Counter, "Test 3 events buffered"); }
public void TestLevelEvaluator() { bufferingForwardingAppender_.Trigger = new TriggerLoggerLevel(Level.Info); bufferingForwardingAppender_.Activate(); BasicConfig.Config(controller_, bufferingForwardingAppender_); ILogger logger = controller_.Get("TestLevelEvaluator"); logger.Log(typeof(EvaluatorTest), Level.Debug, "Debug message logged", null); logger.Log(typeof(EvaluatorTest), Level.Debug, "Debug message logged", null); Assert.AreEqual(0, countingAppender_.Counter, "Test 2 events buffered"); logger.Log(typeof(EvaluatorTest), Level.Info, "Info message logged", null); Assert.AreEqual(3, countingAppender_.Counter, "Test 3 events flushed on Info message."); }
public void TestBackgroundThreadContextProperty() { StringAppender stringAppender = new StringAppender(); stringAppender.Layout = new LayoutPattern("%property{DateTimeTodayToString}"); ILoggerController rep = LogManager.CreateController(TestBackgroundThreadContextPropertyRepository = "TestBackgroundThreadContextPropertyRepository" + Guid.NewGuid().ToString()); BasicConfig.Config(rep, stringAppender); System.Threading.Thread thread = new System.Threading.Thread(new ThreadStart(ExecuteBackgroundThread)); thread.Start(); System.Threading.Thread.CurrentThread.Join(2000); }
public void TestStackTracePattern() { StringAppender stringAppender = new StringAppender(); stringAppender.Layout = NewPatternLayout("%stacktrace{2}"); ILoggerController rep = LogManager.CreateController(Guid.NewGuid().ToString()); BasicConfig.Config(rep, stringAppender); ILog log1 = LogManager.Get(rep.Name, "TestStackTracePattern"); log1.Info("TestMessage"); StringAssert.EndsWith("PatternLayoutTest.TestStackTracePattern", stringAppender.GetString(), "stack trace value set"); stringAppender.Reset(); }
public void NamedPatternConverterWithPrecision2ShouldStripLessLeadingStuffIfPresent() { StringAppender stringAppender = new StringAppender(); LayoutPattern layout = NewPatternLayout(); layout.AddConverter("message-as-name", typeof(MessageAsNamePatternConverter)); layout.Pattern = "%message-as-name{2}"; layout.Activate(); stringAppender.Layout = layout; ILoggerController rep = LogManager.CreateController(Guid.NewGuid().ToString()); BasicConfig.Config(rep, stringAppender); ILog log1 = LogManager.Get(rep.Name, "TestAddingCustomPattern"); log1.Info("NoDots"); Assert.AreEqual("NoDots", stringAppender.GetString(), "%message-as-name not registered"); stringAppender.Reset(); log1.Info("One.Dot"); Assert.AreEqual("One.Dot", stringAppender.GetString(), "%message-as-name not registered"); stringAppender.Reset(); log1.Info("Tw.o.Dots"); Assert.AreEqual("o.Dots", stringAppender.GetString(), "%message-as-name not registered"); stringAppender.Reset(); log1.Info("TrailingDot."); Assert.AreEqual("TrailingDot.", stringAppender.GetString(), "%message-as-name not registered"); stringAppender.Reset(); log1.Info(".LeadingDot"); Assert.AreEqual("LeadingDot", stringAppender.GetString(), "%message-as-name not registered"); stringAppender.Reset(); // empty string and other evil combinations as tests for of-by-one mistakes in index calculations log1.Info(string.Empty); Assert.AreEqual(string.Empty, stringAppender.GetString(), "%message-as-name not registered"); stringAppender.Reset(); log1.Info("x"); Assert.AreEqual("x", stringAppender.GetString(), "%message-as-name not registered"); stringAppender.Reset(); log1.Info("."); Assert.AreEqual(".", stringAppender.GetString(), "%message-as-name not registered"); stringAppender.Reset(); }
public void TestExceptionPattern() { StringAppender stringAppender = new StringAppender(); LayoutPattern layout = NewPatternLayout("%exception{stacktrace}"); stringAppender.Layout = layout; ILoggerController rep = LogManager.CreateController(Guid.NewGuid().ToString()); BasicConfig.Config(rep, stringAppender); ILog log1 = LogManager.Get(rep.Name, "TestExceptionPattern"); Exception exception = new Exception("Oh no!"); log1.Info("TestMessage", exception); Assert.AreEqual(TextDefault.NullText, stringAppender.GetString()); stringAppender.Reset(); }
public void TestAddingCustomPattern() { StringAppender stringAppender = new StringAppender(); LayoutPattern layout = NewPatternLayout(); layout.AddConverter("TestAddingCustomPattern", typeof(TestMessagePatternConverter)); layout.Pattern = "%TestAddingCustomPattern"; layout.Activate(); stringAppender.Layout = layout; ILoggerController rep = LogManager.CreateController(Guid.NewGuid().ToString()); BasicConfig.Config(rep, stringAppender); ILog log1 = LogManager.Get(rep.Name, "TestAddingCustomPattern"); log1.Info("TestMessage"); Assert.AreEqual("TestMessage", stringAppender.GetString(), "%TestAddingCustomPattern not registered"); stringAppender.Reset(); }
public void TestPropertyCharacterEscaping() { LogObjectData evt = CreateBaseEvent(); evt.PropertySet["Property1"] = "prop1 \"quoted\""; LayoutLoggerXml layout = new LayoutLoggerXml(); StringAppender stringAppender = new StringAppender(); stringAppender.Layout = layout; ILoggerController rep = LogManager.CreateController(Guid.NewGuid().ToString()); BasicConfig.Config(rep, stringAppender); ILog log1 = LogManager.Get(rep.Name, "TestThreadProperiesPattern"); log1.Logger.Log(new LogObject(evt)); string expected = CreateEventNode("Property1", "prop1 "quoted""); Assert.AreEqual(expected, stringAppender.GetString()); }
private void SetupController() { controller_ = new LoggerController(); countingAppender_ = new CountingAppender(); countingAppender_.Activate(); bufferingForwardingAppender_ = new AppenderLoggerBufferForward(); bufferingForwardingAppender_.AddAppender(countingAppender_); bufferingForwardingAppender_.Size = 0; bufferingForwardingAppender_.ClearFilter(); bufferingForwardingAppender_.Trigger = null; //m_bufferingForwardingAppender.Fix = FixFlags.Partial; bufferingForwardingAppender_.Lossy = false; bufferingForwardingAppender_.TriggerLossy = null; bufferingForwardingAppender_.Threshold = Level.All; bufferingForwardingAppender_.Activate(); BasicConfig.Config(controller_, bufferingForwardingAppender_); }
public void TestPropertyIllegalCharacterMaskingInName() { LogObjectData evt = CreateBaseEvent(); evt.PropertySet["Property\uFFFF"] = "mask this ->\uFFFF"; LayoutLoggerXml layout = new LayoutLoggerXml(); StringAppender stringAppender = new StringAppender(); stringAppender.Layout = layout; ILoggerController rep = LogManager.CreateController(Guid.NewGuid().ToString()); BasicConfig.Config(rep, stringAppender); ILog log1 = LogManager.Get(rep.Name, "TestThreadProperiesPattern"); log1.Logger.Log(new LogObject(evt)); string expected = CreateEventNode("Property?", "mask this ->?"); Assert.AreEqual(expected, stringAppender.GetString()); }
public void TestBase64PropertyEventLogging() { LogObjectData evt = CreateBaseEvent(); evt.PropertySet["Property1"] = "prop1"; LayoutLoggerXml layout = new LayoutLoggerXml(); layout.base64EncodeProperties = true; StringAppender stringAppender = new StringAppender(); stringAppender.Layout = layout; ILoggerController rep = LogManager.CreateController(Guid.NewGuid().ToString()); BasicConfig.Config(rep, stringAppender); ILog log1 = LogManager.Get(rep.Name, "TestThreadProperiesPattern"); log1.Logger.Log(new LogObject(evt)); string expected = CreateEventNode("Property1", "cHJvcDE="); Assert.AreEqual(expected, stringAppender.GetString()); }
public void DefaultCategoryTest() { CategoryTraceListener categoryTraceListener = new CategoryTraceListener(); Trace.Listeners.Clear(); Trace.Listeners.Add(categoryTraceListener); ILoggerController rep = LogManager.CreateController(Guid.NewGuid().ToString()); AppenderTrace traceAppender = new AppenderTrace(); traceAppender.Layout = new LayoutLoggerSimple(); traceAppender.Activate(); BasicConfig.Config(rep, traceAppender); ILog log = LogManager.Get(rep.Name, GetType()); log.Debug("Message"); Assert.AreEqual( GetType().ToString(), categoryTraceListener.Category); }
public void BracketsInStackTracesAreEscapedProperly() { LayoutLoggerXml layout = new LayoutLoggerXml(); StringAppender stringAppender = new StringAppender(); stringAppender.Layout = layout; ILoggerController rep = LogManager.CreateController(Guid.NewGuid().ToString()); BasicConfig.Config(rep, stringAppender); ILog log1 = LogManager.Get(rep.Name, "TestLogger"); Action <int> bar = foo => { try { throw new NullReferenceException(); } catch (Exception ex) { log1.Error(string.Format("Error {0}", foo), ex); } }; bar(42); var log = stringAppender.GetString(); var startOfExceptionText = log.IndexOf("<exception>", StringComparison.InvariantCulture) + 11; var endOfExceptionText = log.IndexOf("</exception>", StringComparison.InvariantCulture); var sub = log.Substring(startOfExceptionText, endOfExceptionText - startOfExceptionText); if (sub.StartsWith("<![CDATA[")) { StringAssert.EndsWith("]]>", sub); } else { StringAssert.DoesNotContain("<", sub); StringAssert.DoesNotContain(">", sub); } }