Ejemplo n.º 1
0
        public void NamedPatternConverterWithPrecision1ShouldStripLeadingStuffIfPresent()
        {
            StringAppender stringAppender = new StringAppender();
            LayoutPattern  layout         = NewPatternLayout();

            layout.AddConverter("message-as-name", typeof(MessageAsNamePatternConverter));
            layout.Pattern = "%message-as-name{1}";
            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("Dot", stringAppender.GetString(), "%message-as-name not registered");
            stringAppender.Reset();

            log1.Info("Tw.o.Dots");
            Assert.AreEqual("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();
        }
Ejemplo n.º 2
0
        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();
        }
Ejemplo n.º 3
0
        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();
        }
Ejemplo n.º 4
0
        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());
        }
Ejemplo n.º 5
0
        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());
        }
Ejemplo n.º 6
0
        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);
            }
        }
Ejemplo n.º 7
0
        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());
        }