Write events to a string
Inheritance: log4net.Appender.AppenderSkeleton
Esempio n. 1
0
		[Test] public void TestGlobalPropertiesPattern()
		{
			StringAppender stringAppender = new StringAppender();
			stringAppender.Layout = new PatternLayout("%property{prop1}");

			ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
			BasicConfigurator.Configure(rep, stringAppender);

			ILog log1 = LogManager.GetLogger(rep.Name, "TestGlobalProperiesPattern");

			log1.Info("TestMessage");
			Assert.AreEqual(SystemInfo.NullText, stringAppender.GetString(), "Test no global properties value set");
			stringAppender.Reset();

			GlobalContext.Properties["prop1"] = "val1";

			log1.Info("TestMessage");
			Assert.AreEqual("val1", stringAppender.GetString(), "Test global properties value set");
			stringAppender.Reset();

			GlobalContext.Properties.Remove("prop1");

			log1.Info("TestMessage");
			Assert.AreEqual(SystemInfo.NullText, stringAppender.GetString(), "Test global properties value removed");
			stringAppender.Reset();
		}
Esempio n. 2
0
		/// <summary>
		/// Test that a repository can be shutdown and reconfigured
		/// </summary>
		[Test] public void TestShutdownAndReconfigure()
		{
			// Create unique repository
			ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());

			// Create appender and configure repos
			StringAppender stringAppender = new StringAppender();
			stringAppender.Layout = new PatternLayout("%m");
			BasicConfigurator.Configure(rep, stringAppender);

			// Get logger from repos
			ILog log1 = LogManager.GetLogger(rep.Name, "logger1");

			log1.Info("TestMessage1");
			Assert.AreEqual("TestMessage1", stringAppender.GetString(), "Test logging configured");
			stringAppender.Reset();

			rep.Shutdown();

			log1.Info("TestMessage2");
			Assert.AreEqual("", stringAppender.GetString(), "Test not logging while shutdown");
			stringAppender.Reset();

			// Create new appender and configure
			stringAppender = new StringAppender();
			stringAppender.Layout = new PatternLayout("%m");
			BasicConfigurator.Configure(rep, stringAppender);

			log1.Info("TestMessage3");
			Assert.AreEqual("TestMessage3", stringAppender.GetString(), "Test logging re-configured");
			stringAppender.Reset();
		}
Esempio n. 3
0
		public void TestThreadPropertiesPattern()
		{
			StringAppender stringAppender = new StringAppender();
			stringAppender.Layout = new PatternLayout("%property{" +  Utils.PROPERTY_KEY + "}");

			ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
			BasicConfigurator.Configure(rep, stringAppender);

			ILog log1 = LogManager.GetLogger(rep.Name, "TestThreadProperiesPattern");

			log1.Info("TestMessage");
			Assert.AreEqual(SystemInfo.NullText, stringAppender.GetString(), "Test no thread properties value set");
			stringAppender.Reset();

			ThreadContext.Properties[Utils.PROPERTY_KEY] = "val1";

			log1.Info("TestMessage");
			Assert.AreEqual("val1", stringAppender.GetString(), "Test thread properties value set");
			stringAppender.Reset();

			ThreadContext.Properties.Remove(Utils.PROPERTY_KEY);

			log1.Info("TestMessage");
			Assert.AreEqual(SystemInfo.NullText, stringAppender.GetString(), "Test thread properties value removed");
			stringAppender.Reset();
		}
Esempio n. 4
0
		[Test] public void TestThreadStackPattern()
		{
			StringAppender stringAppender = new StringAppender();
			stringAppender.Layout = new PatternLayout("%property{prop1}");

			ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
			BasicConfigurator.Configure(rep, stringAppender);

			ILog log1 = LogManager.GetLogger(rep.Name, "TestThreadStackPattern");

			log1.Info("TestMessage");
			Assert.AreEqual(SystemInfo.NullText, stringAppender.GetString(), "Test no thread stack value set");
			stringAppender.Reset();

			using(ThreadContext.Stacks["prop1"].Push("val1"))
			{
				log1.Info("TestMessage");
				Assert.AreEqual("val1", stringAppender.GetString(), "Test thread stack value set");
				stringAppender.Reset();
			}

			log1.Info("TestMessage");
			Assert.AreEqual(SystemInfo.NullText, stringAppender.GetString(), "Test thread stack value removed");
			stringAppender.Reset();
		}
Esempio n. 5
0
		[Test] public void TestFormatString()
		{
			StringAppender stringAppender = new StringAppender();
			stringAppender.Layout = new PatternLayout("%message");

			ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
			BasicConfigurator.Configure(rep, stringAppender);

			ILog log1 = LogManager.GetLogger(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.FatalFormat("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();
		}
        public void TestStackTracePattern()
        {
            StringAppender stringAppender = new StringAppender();
            stringAppender.Layout = new PatternLayout("%stacktrace{2}");

            ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
            BasicConfigurator.Configure(rep, stringAppender);

            ILog log1 = LogManager.GetLogger(rep.Name, "TestStackTracePattern");

            log1.Info("TestMessage");
            Assert.AreEqual("RuntimeMethodHandle._InvokeMethodFast > PatternLayoutTest.TestStackTracePattern", stringAppender.GetString(), "stack trace value set");
            stringAppender.Reset();
        }
Esempio n. 7
0
        public void SetupRepository()
        {
            hierarchy = new Repository.Hierarchy.Hierarchy();

            stringAppender = new StringAppender();
            stringAppender.Layout = new SimpleLayout();
            stringAppender.ActivateOptions();

            asyncAppender = new AsyncAppender();
            asyncAppender.AddAppender(stringAppender);
            asyncAppender.ActivateOptions();

            BasicConfigurator.Configure(hierarchy, asyncAppender);
        }
		public async Task TestLogicalThreadPropertiesPatternAsyncAwait()
		{
			StringAppender stringAppender = new StringAppender();
			stringAppender.Layout = new PatternLayout("%property{" + Utils.PROPERTY_KEY + "}");

			ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
			BasicConfigurator.Configure(rep, stringAppender);

			ILog log1 = LogManager.GetLogger(rep.Name, "TestLogicalThreadPropertiesPattern");

			log1.Info("TestMessage");
			Assert.AreEqual(SystemInfo.NullText, stringAppender.GetString(), "Test no logical thread stack value set");
			stringAppender.Reset();

			string testValueForCurrentContext = "Outer";
			LogicalThreadContext.Properties[Utils.PROPERTY_KEY] = testValueForCurrentContext;

			log1.Info("TestMessage");
			Assert.AreEqual(testValueForCurrentContext, stringAppender.GetString(), "Test logical thread properties value set");
			stringAppender.Reset();

			var strings = await Task.WhenAll(Enumerable.Range(0, 10).Select(x => SomeWorkProperties(x.ToString())));

			// strings should be ["00AA0BB0", "01AA1BB1", "02AA2BB2", ...]
			for (int i = 0; i < strings.Length; i++)
			{
				Assert.AreEqual(string.Format("{0}{1}AA{1}BB{1}", testValueForCurrentContext, i), strings[i], "Test logical thread properties expected sequence");
			}

			log1.Info("TestMessage");
			Assert.AreEqual(testValueForCurrentContext, stringAppender.GetString(), "Test logical thread properties value set");
			stringAppender.Reset();

			LogicalThreadContext.Properties.Remove(Utils.PROPERTY_KEY);

			log1.Info("TestMessage");
			Assert.AreEqual(SystemInfo.NullText, stringAppender.GetString(), "Test logical thread properties value removed");
			stringAppender.Reset();
		}
Esempio n. 9
0
		[Test] public void TestLogFormatApi_NoError()
		{
			StringAppender stringAppender = new StringAppender();
			stringAppender.Threshold = Level.Fatal;
			stringAppender.Layout = new PatternLayout("%level:%message");

			ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
			BasicConfigurator.Configure(rep, stringAppender);

			ILog log1 = LogManager.GetLogger(rep.Name, "TestLogFormatApi_Error");

			// ***
			log1.Error("TestMessage");
			Assert.AreEqual("", stringAppender.GetString(), "Test simple ERROR event 1");
			stringAppender.Reset();

			// ***
			log1.Error("TestMessage", null);
			Assert.AreEqual("", stringAppender.GetString(), "Test simple ERROR event 2");
			stringAppender.Reset();

			// ***
			log1.Error("TestMessage", new Exception("Exception message"));
			Assert.AreEqual("", stringAppender.GetString(), "Test simple ERROR event 3");
			stringAppender.Reset();

			// ***
			log1.ErrorFormat("a{0}", "1");
			Assert.AreEqual("", stringAppender.GetString(), "Test formatted ERROR event with 1 parm");
			stringAppender.Reset();

			// ***
			log1.ErrorFormat("a{0}b{1}", "1", "2");
			Assert.AreEqual("", stringAppender.GetString(), "Test formatted ERROR event with 2 parm");
			stringAppender.Reset();

			// ***
			log1.ErrorFormat("a{0}b{1}c{2}", "1", "2", "3");
			Assert.AreEqual("", stringAppender.GetString(), "Test formatted ERROR event with 3 parm");
			stringAppender.Reset();


			// ***
			log1.ErrorFormat("a{0}b{1}c{2}d{3}e{4}f", "Q", "W", "E", "R", "T", "Y");
			Assert.AreEqual("", stringAppender.GetString(), "Test formatted ERROR event with 5 parms (only 4 used)");
			stringAppender.Reset();

			// ***
			log1.ErrorFormat(null, "Before {0} After {1}", "Middle", "End");
			Assert.AreEqual("", stringAppender.GetString(), "Test formatting with null provider");
			stringAppender.Reset();

			// ***
			log1.ErrorFormat(new System.Globalization.CultureInfo("en"), "Before {0} After {1}", "Middle", "End");
			Assert.AreEqual("", stringAppender.GetString(), "Test formatting with 'en' provider");
			stringAppender.Reset();
		}
Esempio n. 10
0
        public void TestStackTracePattern()
        {
            StringAppender stringAppender = new StringAppender();
            stringAppender.Layout = NewPatternLayout("%stacktrace{2}");

            ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
            BasicConfigurator.Configure(rep, stringAppender);

            ILog log1 = LogManager.GetLogger(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();
            PatternLayout layout = NewPatternLayout();
            layout.AddConverter("message-as-name", typeof(MessageAsNamePatternConverter));
            layout.ConversionPattern = "%message-as-name{2}";
            layout.ActivateOptions();
            stringAppender.Layout = layout;
            ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
            BasicConfigurator.Configure(rep, stringAppender);
            ILog log1 = LogManager.GetLogger(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();
        }
Esempio n. 12
0
		[Test] public void TestAddingCustomPattern()
		{
			StringAppender stringAppender = new StringAppender();
			PatternLayout layout = new PatternLayout();

			layout.AddConverter("TestAddingCustomPattern", typeof(TestMessagePatternConverter));
			layout.ConversionPattern = "%TestAddingCustomPattern";
			layout.ActivateOptions();

			stringAppender.Layout = layout;

			ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
			BasicConfigurator.Configure(rep, stringAppender);

			ILog log1 = LogManager.GetLogger(rep.Name, "TestAddingCustomPattern");

			log1.Info("TestMessage");
			Assert.AreEqual("TestMessage", stringAppender.GetString(), "%TestAddingCustomPattern not registered");
			stringAppender.Reset();	
		}
		public void TestExceptionPattern()
		{
			StringAppender stringAppender = new StringAppender();
			PatternLayout layout = NewPatternLayout("%exception{stacktrace}");
			stringAppender.Layout = layout;

			ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
			BasicConfigurator.Configure(rep, stringAppender);

			ILog log1 = LogManager.GetLogger(rep.Name, "TestExceptionPattern");

			Exception exception = new Exception("Oh no!");
			log1.Info("TestMessage", exception);

			Assert.AreEqual(SystemInfo.NullText, stringAppender.GetString());

			stringAppender.Reset();
		}
Esempio n. 14
0
        public void BracketsInStackTracesAreEscapedProperly() {
            XmlLayout layout = new XmlLayout();
            StringAppender stringAppender = new StringAppender();
            stringAppender.Layout = layout;

            ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
            BasicConfigurator.Configure(rep, stringAppender);
            ILog log1 = LogManager.GetLogger(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);
            }
        }
Esempio n. 15
0
		static async Task<string> SomeWorkStack(string stackName)
		{
			StringAppender stringAppender = new StringAppender();
			stringAppender.Layout = new PatternLayout("%property{" + Utils.PROPERTY_KEY + "}");

			ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
			BasicConfigurator.Configure(rep, stringAppender);

			ILog log = LogManager.GetLogger(rep.Name, "TestLogicalThreadStackPattern");

			using (LogicalThreadContext.Stacks[Utils.PROPERTY_KEY].Push(stackName))
			{
				log.Info("TestMessage");
				Assert.AreEqual(string.Format("Outer {0}", stackName), stringAppender.GetString(), "Test logical thread stack value set");
				stringAppender.Reset();

				await MoreWorkStack(log, "A");
				log.Info("TestMessage");
				await MoreWorkStack(log, "B");
				log.Info("TestMessage");
			}

			return stringAppender.GetString();
		}
Esempio n. 16
0
		public void TestPropertyIllegalCharacterMaskingInName()
		{
			LoggingEventData evt = CreateBaseEvent();
			evt.Properties["Property\uFFFF"] = "mask this ->\uFFFF";

			XmlLayout layout = new XmlLayout();
			StringAppender stringAppender = new StringAppender();
			stringAppender.Layout = layout;

			ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
			BasicConfigurator.Configure(rep, stringAppender);
			ILog log1 = LogManager.GetLogger(rep.Name, "TestThreadProperiesPattern");

			log1.Logger.Log(new LoggingEvent(evt));

			string expected = CreateEventNode("Property?", "mask this -&gt;?");

			Assert.AreEqual(expected, stringAppender.GetString());
		}
Esempio n. 17
0
		public void TestPropertyCharacterEscaping()
		{
			LoggingEventData evt = CreateBaseEvent();
			evt.Properties["Property1"] = "prop1 \"quoted\"";

			XmlLayout layout = new XmlLayout();
			StringAppender stringAppender = new StringAppender();
			stringAppender.Layout = layout;

			ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
			BasicConfigurator.Configure(rep, stringAppender);
			ILog log1 = LogManager.GetLogger(rep.Name, "TestThreadProperiesPattern");

			log1.Logger.Log(new LoggingEvent(evt));

			string expected = CreateEventNode("Property1", "prop1 &quot;quoted&quot;");

			Assert.AreEqual(expected, stringAppender.GetString());
		}
Esempio n. 18
0
        public void BracketsInStackTracesAreEscapedProperly() {
            XmlLayout layout = new XmlLayout();
            StringAppender stringAppender = new StringAppender();
            stringAppender.Layout = layout;

            ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
            BasicConfigurator.Configure(rep, stringAppender);
            ILog log1 = LogManager.GetLogger(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 startOfExceptionElement = log.IndexOf("<exception>");
            var sub = log.Substring(startOfExceptionElement + 11);
            StringAssert.StartsWith("System.NullReferenceException: Object reference not set to an instance of an object", sub);
            StringAssert.Contains("at log4net.Tests.Layout.XmlLayoutTest.&lt;&gt;c__DisplayClass4.&lt;BracketsInStackTracesAreEscapedProperly&gt;b__3(Int32 foo)", sub);
        }
Esempio n. 19
0
		public void TestLogicalThreadStackPatternNullVal2()
		{
			StringAppender stringAppender = new StringAppender();
			stringAppender.Layout = new PatternLayout("%property{" + Utils.PROPERTY_KEY + "}");

			ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
			BasicConfigurator.Configure(rep, stringAppender);

			ILog log1 = LogManager.GetLogger(rep.Name, "TestThreadStackPattern");

			log1.Info("TestMessage");
			Assert.AreEqual(SystemInfo.NullText, stringAppender.GetString(), "Test no logical thread stack value set");
			stringAppender.Reset();

			using (LogicalThreadContext.Stacks[Utils.PROPERTY_KEY].Push("val1"))
			{
				log1.Info("TestMessage");
				Assert.AreEqual("val1", stringAppender.GetString(), "Test logical thread stack value set");
				stringAppender.Reset();

				using (LogicalThreadContext.Stacks[Utils.PROPERTY_KEY].Push(null))
				{
					log1.Info("TestMessage");
					Assert.AreEqual("val1 ", stringAppender.GetString(), "Test logical thread stack value pushed null");
					stringAppender.Reset();
				}
			}

			log1.Info("TestMessage");
			Assert.AreEqual(SystemInfo.NullText, stringAppender.GetString(), "Test logical thread stack value removed");
			stringAppender.Reset();
		}
Esempio n. 20
0
		public void TestBackgroundThreadContextProperty()
		{
			StringAppender stringAppender = new StringAppender();
			stringAppender.Layout = new PatternLayout("%property{DateTimeTodayToString}");

            ILoggerRepository rep = LogManager.CreateRepository(TestBackgroundThreadContextPropertyRepository = "TestBackgroundThreadContextPropertyRepository" + Guid.NewGuid().ToString());
			BasicConfigurator.Configure(rep, stringAppender);

			Thread thread = new Thread(new ThreadStart(ExecuteBackgroundThread));
			thread.Start();

			Thread.CurrentThread.Join(2000);
		}
Esempio n. 21
0
		[Test] public void TestLogFormatApi_Debug()
		{
			StringAppender stringAppender = new StringAppender();
			stringAppender.Layout = new PatternLayout("%level:%message");

			ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
			BasicConfigurator.Configure(rep, stringAppender);

			ILog log1 = LogManager.GetLogger(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:TestMessageSystem.Exception: Exception message\r\n", 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 System.Globalization.CultureInfo("en"), "Before {0} After {1}", "Middle", "End");
			Assert.AreEqual("DEBUG:Before Middle After End", stringAppender.GetString(), "Test formatting with 'en' provider");
			stringAppender.Reset();
		}
Esempio n. 22
0
        public void BracketsInStackTracesKeepLogWellFormed() {
            XmlLayout layout = new XmlLayout();
            StringAppender stringAppender = new StringAppender();
            stringAppender.Layout = layout;

            ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
            BasicConfigurator.Configure(rep, stringAppender);
            ILog log1 = LogManager.GetLogger(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());
        }
Esempio n. 23
0
		public void TestBase64PropertyEventLogging()
		{
			LoggingEventData evt = CreateBaseEvent();
			evt.Properties["Property1"] = "prop1";

			XmlLayout layout = new XmlLayout();
			layout.Base64EncodeProperties = true;
			StringAppender stringAppender = new StringAppender();
			stringAppender.Layout = layout;

			ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
			BasicConfigurator.Configure(rep, stringAppender);
			ILog log1 = LogManager.GetLogger(rep.Name, "TestThreadProperiesPattern");

			log1.Logger.Log(new LoggingEvent(evt));

			string expected = CreateEventNode("Property1", "cHJvcDE=");

			Assert.AreEqual(expected, stringAppender.GetString());
		}
Esempio n. 24
0
		static async Task<string> SomeWorkProperties(string propertyName)
		{
			StringAppender stringAppender = new StringAppender();
			stringAppender.Layout = new PatternLayout("%property{" + Utils.PROPERTY_KEY + "}");

			ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
			BasicConfigurator.Configure(rep, stringAppender);

			ILog log = LogManager.GetLogger(rep.Name, "TestLogicalThreadStackPattern");
			log.Info("TestMessage");

			// set a new one
			LogicalThreadContext.Properties[Utils.PROPERTY_KEY] = propertyName;
			log.Info("TestMessage");

			await MoreWorkProperties(log, "A");
			log.Info("TestMessage");
			await MoreWorkProperties(log, "B");
			log.Info("TestMessage");
			return stringAppender.GetString();
		}