Пример #1
0
 public void Test4()
 {
     Layout l = new Layout("${rot13:${event-context:aaa}}");
     LogEventInfo lei = new LogEventInfo(LogLevel.Info, "aaa", "bbb");
     lei.Context["aaa"] = "HELLO";
     Assert.AreEqual("URYYB", l.GetFormattedMessage(lei));
 }
Пример #2
0
 public void SingleParamTest()
 {
     Layout l = new Layout("${mdc:item=AAA}");
     Assert.AreEqual(1, l.Renderers.Length);
     MDCLayoutRenderer mdc = l.Renderers[0] as MDCLayoutRenderer;
     Assert.IsNotNull(mdc);
     Assert.AreEqual("AAA", mdc.Item);
 }
Пример #3
0
 public void ValueWithColonTest()
 {
     Layout l = new Layout("${mdc:item=AAA\\:}");
     Assert.AreEqual(1, l.Renderers.Length);
     MDCLayoutRenderer mdc = l.Renderers[0] as MDCLayoutRenderer;
     Assert.IsNotNull(mdc);
     Assert.AreEqual("AAA:", mdc.Item);
 }
Пример #4
0
        public void Test1()
        {
            Layout l = new Layout("${event-context:aaa}");
            l.Initialize();
            LogEventInfo lei = new LogEventInfo(LogLevel.Info, "aaa", "bbb");

            // empty
            Assert.AreEqual("", l.GetFormattedMessage(lei));
        }
Пример #5
0
 public void BaseDirFileCombineTest()
 {
     Layout l = new Layout("${basedir:file=aaa.txt}");
     AssertLayoutRendererOutput(l, Path.Combine(baseDir, "aaa.txt"));
 }
Пример #6
0
 public void BaseDirCombineTest()
 {
     Layout l = new Layout("${basedir:dir=aaa}");
     AssertLayoutRendererOutput(l, Path.Combine(baseDir,"aaa"));
 }
Пример #7
0
 public void BaseDirTest()
 {
     Layout l = new Layout("${basedir}");
     AssertLayoutRendererOutput(l, baseDir);
 }
Пример #8
0
 public void DefaultValueWithBracketTest()
 {
     Layout l = new Layout("${mdc:AAA\\}\\:}");
     Assert.AreEqual(l.Text,"${mdc:AAA\\}\\:}");
     Assert.AreEqual(1, l.Renderers.Length);
     MDCLayoutRenderer mdc = l.Renderers[0] as MDCLayoutRenderer;
     Assert.IsNotNull(mdc);
     Assert.AreEqual("AAA}:", mdc.Item);
 }
Пример #9
0
 public void Test3()
 {
     Layout l = new Layout("${rot13:text=HELLO}");
     LogEventInfo lei = new LogEventInfo(LogLevel.Info, "aaa", "bbb");
     Assert.AreEqual("URYYB", l.GetFormattedMessage(lei));
 }
Пример #10
0
 /// <summary>
 /// Evaluates the specified text by expadinging all layout renderers.
 /// </summary>
 /// <param name="text">The text to be evaluated.</param>
 /// <param name="logEvent">Log event to be used for evaluation</param>
 /// <returns>The input text with all occurences of ${} replaced with
 /// values provided by the appropriate layout renderers.</returns>
 public static string Evaluate(string text, LogEventInfo logEvent)
 {
     Layout l = new Layout(text);
     return l.GetFormattedMessage(logEvent);
 }
Пример #11
0
 public void DefaultValueTest()
 {
     Layout l = new Layout("${mdc:BBB}");
     Assert.AreEqual(1, l.Renderers.Length);
     MDCLayoutRenderer mdc = l.Renderers[0] as MDCLayoutRenderer;
     Assert.IsNotNull(mdc);
     Assert.AreEqual("BBB", mdc.Item);
 }
Пример #12
0
 public void UnclosedTest()
 {
     Layout l = new Layout("${message");
 }
Пример #13
0
        private static LayoutRenderer ParseLayoutRenderer(Tokenizer sr)
        {
            int ch;

            ch = sr.Read();
            if (ch != '{')
                throw new NLogConfigurationException("'{' expected in layout specification");

            string name = ParseLayoutRendererName(sr);
            LayoutRenderer lr = LayoutRendererFactory.CreateLayoutRenderer(name, null);

            ch = sr.Read();
            while (ch != -1 && ch != '}')
            {
                string parameterName = ParseParameterName(sr).Trim();
                if (sr.Peek() == '=')
                {
                    sr.Read(); // skip the '='
                    PropertyInfo pi = PropertyHelper.GetPropertyInfo(lr, parameterName);
                    if (pi == null)
                    {
                        ParseParameterValue(sr);
                    }
                    else
                    {
                        if (typeof(Layout) == pi.PropertyType)
                        {
                            Layout nestedLayout = new Layout();
                            string txt;
                            LayoutRenderer[] renderers = CompileLayout(sr, true, out txt);

                            nestedLayout.SetRenderers(renderers, txt);
                            pi.SetValue(lr, nestedLayout, null);
                        }
                        else
                        {
                            string value = ParseParameterValue(sr);
                            PropertyHelper.SetPropertyFromString(lr, parameterName, value, null);
                        }
                    }
                }
                else
                {
                    // what we've just read is not a parameterName, but a value
                    // assign it to a default property (denoted by empty string)

                    PropertyInfo pi = PropertyHelper.GetPropertyInfo(lr, "");
                    if (pi != null)
                    {
                        if (typeof(Layout) == pi.PropertyType)
                        {
                            pi.SetValue(lr, new Layout(parameterName), null);
                        }
                        else
                        {
                            string value = parameterName;
                            PropertyHelper.SetPropertyFromString(lr, pi.Name, value, null);
                        }
                    }
                    else
                    {
                        InternalLogger.Warn("{0} has no default property", lr.GetType().FullName);
                    }
                }
                ch = sr.Read();
            }
            return lr;
        }
Пример #14
0
 public void SimpleTest()
 {
     Layout l = new Layout("${message}");
     Assert.AreEqual(1, l.Renderers.Length);
     Assert.IsInstanceOfType(typeof(NLog.LayoutRenderers.MessageLayoutRenderer), l.Renderers[0]);
 }
Пример #15
0
        public void DoubleNestedLayoutWithDefaultLayoutParametersTest()
        {
            Layout l = new Layout("${file-contents:${basedir}/${file-contents:${basedir}/aaa.txt}/aaa.txt}");
            Assert.AreEqual(1, l.Renderers.Length);
            FileContentsLayoutRenderer lr = l.Renderers[0] as FileContentsLayoutRenderer;
            Assert.IsNotNull(lr);
            Assert.IsInstanceOfType(typeof(Layout), lr.FileName);
            Assert.AreEqual("${basedir}/${file-contents:${basedir}/aaa.txt}/aaa.txt", lr.FileName.Text);
            Assert.AreEqual(3, lr.FileName.Renderers.Length);
            Assert.IsInstanceOfType(typeof(LiteralLayoutRenderer), lr.FileName.Renderers[0]);
            Assert.IsInstanceOfType(typeof(FileContentsLayoutRenderer), lr.FileName.Renderers[1]);
            Assert.IsInstanceOfType(typeof(LiteralLayoutRenderer), lr.FileName.Renderers[2]);

            LiteralLayoutRenderer lr1 = (LiteralLayoutRenderer)lr.FileName.Renderers[0];
            FileContentsLayoutRenderer fc = (FileContentsLayoutRenderer)lr.FileName.Renderers[1];
            LiteralLayoutRenderer lr2 = (LiteralLayoutRenderer)lr.FileName.Renderers[2];

            Assert.AreEqual("${basedir}/aaa.txt", fc.FileName.Text);

        }
Пример #16
0
 public void NestedLayoutTest()
 {
     Layout l = new Layout("${file-contents:fileName=${basedir:padding=10}/aaa.txt:padding=12}");
     Assert.AreEqual(1, l.Renderers.Length);
     FileContentsLayoutRenderer lr = l.Renderers[0] as FileContentsLayoutRenderer;
     Assert.IsNotNull(lr);
     Assert.IsInstanceOfType(typeof(Layout), lr.FileName);
     Assert.AreEqual("${basedir:padding=10}/aaa.txt", lr.FileName.Text);
     Assert.AreEqual(1, lr.FileName.Renderers.Length);
     Assert.AreEqual(12, lr.Padding);
 }
Пример #17
0
 public void EmptyValueTest()
 {
     Layout l = new Layout("${mdc:item=}");
     Assert.AreEqual(1, l.Renderers.Length);
     MDCLayoutRenderer mdc = l.Renderers[0] as MDCLayoutRenderer;
     Assert.IsNotNull(mdc);
     Assert.AreEqual("", mdc.Item);
 }
Пример #18
0
 public void DefaultValueWithOtherParametersTest()
 {
     Layout l = new Layout("${mdc:BBB:padding=3:padcharacter=X}");
     Assert.AreEqual(1, l.Renderers.Length);
     MDCLayoutRenderer mdc = l.Renderers[0] as MDCLayoutRenderer;
     Assert.IsNotNull(mdc);
     Assert.AreEqual("BBB", mdc.Item);
     Assert.AreEqual(3, mdc.Padding);
     Assert.AreEqual('X', mdc.PadCharacter);
 }
Пример #19
0
 public void EnvironmentSimpleTest()
 {
     Layout l = new Layout("${environment:PATH}");
     AssertLayoutRendererOutput(l, System.Environment.GetEnvironmentVariable("PATH"));
 }
Пример #20
0
        private static LayoutRenderer ParseLayoutRenderer(Tokenizer sr)
        {
            int ch;

            ch = sr.Read();
            if (ch != '{')
            {
                throw new NLogConfigurationException("'{' expected in layout specification");
            }

            string         name = ParseLayoutRendererName(sr);
            LayoutRenderer lr   = LayoutRendererFactory.CreateLayoutRenderer(name, null);

            ch = sr.Read();
            while (ch != -1 && ch != '}')
            {
                string parameterName = ParseParameterName(sr).Trim();
                if (sr.Peek() == '=')
                {
                    sr.Read(); // skip the '='
                    PropertyInfo pi = PropertyHelper.GetPropertyInfo(lr, parameterName);
                    if (pi == null)
                    {
                        ParseParameterValue(sr);
                    }
                    else
                    {
                        if (typeof(Layout) == pi.PropertyType)
                        {
                            Layout           nestedLayout = new Layout();
                            string           txt;
                            LayoutRenderer[] renderers = CompileLayout(sr, true, out txt);

                            nestedLayout.SetRenderers(renderers, txt);
                            pi.SetValue(lr, nestedLayout, null);
                        }
                        else
                        {
                            string value = ParseParameterValue(sr);
                            PropertyHelper.SetPropertyFromString(lr, parameterName, value, null);
                        }
                    }
                }
                else
                {
                    // what we've just read is not a parameterName, but a value
                    // assign it to a default property (denoted by empty string)

                    PropertyInfo pi = PropertyHelper.GetPropertyInfo(lr, "");
                    if (pi != null)
                    {
                        if (typeof(Layout) == pi.PropertyType)
                        {
                            pi.SetValue(lr, new Layout(parameterName), null);
                        }
                        else
                        {
                            string value = parameterName;
                            PropertyHelper.SetPropertyFromString(lr, pi.Name, value, null);
                        }
                    }
                    else
                    {
                        InternalLogger.Warn("{0} has no default property", lr.GetType().FullName);
                    }
                }
                ch = sr.Read();
            }
            return(lr);
        }