Ejemplo n.º 1
0
        private static LayoutRenderer ParseLayoutRenderer(Tokenizer sr)
        {
            int ch;

            ch = sr.Read();
            if (ch != '{')
                throw new LogConfigurationException("'{' 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;
        }
Ejemplo n.º 2
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);
 }