/// <summary>
 /// Adds the elements of an array to the end of this LayoutRendererCollection.
 /// </summary>
 /// <param name="items">
 /// The array whose elements are to be added to the end of this LayoutRendererCollection.
 /// </param>
 public virtual void AddRange(LayoutRenderer[]items)
 {
     foreach (LayoutRenderer item in items)
     {
         this.List.Add(item);
     }
 }
示例#2
0
        private static LayoutRenderer ConvertToLiteral(LayoutRenderer renderer)
        {
            StringBuilder sb = new StringBuilder();

            renderer.Append(sb, LogEventInfo.CreateNullEvent());
            return(new LiteralLayoutRenderer(sb.ToString()));
        }
示例#3
0
        /// <summary>
        /// Renders the layout for the specified logging event by invoking layout renderers
        /// that make up the event.
        /// </summary>
        /// <param name="logEvent">The logging event.</param>
        /// <returns>The rendered layout.</returns>
        public string GetFormattedMessage(LogEventInfo logEvent)
        {
            if (_fixedText != null)
            {
                return(_fixedText);
            }

            string cachedValue = logEvent.GetCachedLayoutValue(this);

            if (cachedValue != null)
            {
                return(cachedValue);
            }

            int size = 0;

            for (int i = 0; i < _renderers.Length; ++i)
            {
                LayoutRenderer app = _renderers[i];
                try
                {
                    int ebs = app.GetEstimatedBufferSize(logEvent);
                    size += ebs;
                }
                catch (Exception ex)
                {
                    if (InternalLogger.IsWarnEnabled)
                    {
                        InternalLogger.Warn("Exception in {0}.GetEstimatedBufferSize(): {1}.", app.GetType().FullName, ex);
                    }
                }
            }
            StringBuilder builder = new StringBuilder(size);

            for (int i = 0; i < _renderers.Length; ++i)
            {
                LayoutRenderer app = _renderers[i];
                try
                {
                    app.Append(builder, logEvent);
                }
                catch (Exception ex)
                {
                    if (InternalLogger.IsWarnEnabled)
                    {
                        InternalLogger.Warn("Exception in {0}.Append(): {1}.", app.GetType().FullName, ex);
                    }
                }
            }

            string value = builder.ToString();

            logEvent.AddCachedLayoutValue(this, value);
            return(value);
        }
示例#4
0
        private static void ApplyLayoutRendererParameters(LayoutRenderer target, string parameterString)
        {
            int pos = 0;

            while (pos < parameterString.Length)
            {
                int nameStartPos = pos;
                while (pos < parameterString.Length && Char.IsWhiteSpace(parameterString[pos]))
                {
                    pos++;
                }
                while (pos < parameterString.Length && parameterString[pos] != '=')
                {
                    pos++;
                }
                int nameEndPos = pos;
                if (nameStartPos == nameEndPos)
                {
                    break;
                }

                pos++;

                // we've got a name - now get a value
                //

                StringBuilder valueBuf = new StringBuilder();
                while (pos < parameterString.Length)
                {
                    if (parameterString[pos] == '\\')
                    {
                        valueBuf.Append(parameterString[pos + 1]);
                        pos += 2;
                    }
                    else if (parameterString[pos] == ':')
                    {
                        pos++;
                        break;
                    }
                    else
                    {
                        valueBuf.Append(parameterString[pos]);
                        pos++;
                    }
                }

                string name  = parameterString.Substring(nameStartPos, nameEndPos - nameStartPos);
                string value = valueBuf.ToString();

                PropertyHelper.SetPropertyFromString(target, name, value, null);
            }
        }
示例#5
0
        internal static LayoutRenderer[] CompileLayout(Tokenizer sr, bool isNested, out string text)
        {
            ArrayList     result     = new ArrayList();
            StringBuilder literalBuf = new StringBuilder();

            int ch;

            int p0 = sr.Position;

            while ((ch = sr.Peek()) != -1)
            {
                if (isNested && (ch == '}' || ch == ':'))
                {
                    break;
                }
                sr.Read();

                if (ch == '$' && sr.Peek() == '{')
                {
                    if (literalBuf.Length > 0)
                    {
                        result.Add(new LiteralLayoutRenderer(literalBuf.ToString()));
                        literalBuf.Length = 0;
                    }

                    LayoutRenderer newLayoutRenderer = ParseLayoutRenderer(sr);
                    if (newLayoutRenderer.IsAppDomainFixed())
                    {
                        newLayoutRenderer = ConvertToLiteral(newLayoutRenderer);
                    }
                    result.Add(newLayoutRenderer);
                    // layout renderer
                }
                else
                {
                    literalBuf.Append((char)ch);
                }
            }

            if (literalBuf.Length > 0)
            {
                result.Add(new LiteralLayoutRenderer(literalBuf.ToString()));
                literalBuf.Length = 0;
            }
            int p1 = sr.Position;

            MergeLiterals(result);
            text = sr.Substring(p0, p1);

            return((LayoutRenderer[])result.ToArray(typeof(LayoutRenderer)));
        }
示例#6
0
        /// <summary>
        /// Creates the layout renderer object based on its layout renderer name and sets its properties from parameters string.
        /// </summary>
        /// <param name="name">The name of the layout renderer (e.g. <code>message</code> or <code>aspnet-request</code>)</param>
        /// <param name="parameters">Parameters to the layout renderer.</param>
        /// <returns>A new instance of the <see cref="LayoutRenderer"/> object.</returns>
        public static LayoutRenderer CreateLayoutRenderer(string name, string parameters)
        {
            Type t = _targets[name.ToLower()];

            if (t != null)
            {
                LayoutRenderer la = FactoryHelper.CreateInstance(t) as LayoutRenderer;
                if (la != null)
                {
                    if (parameters != null && parameters.Length > 0)
                    {
                        ApplyLayoutRendererParameters(la, parameters);
                    }

                    return(la);
                }
                else
                {
                    return(CreateUnknownLayoutRenderer(name, parameters));
                }
            }
            return(CreateUnknownLayoutRenderer(name, parameters));
        }
 /// <summary>
 /// Initializes a new instance of the LayoutRendererCollection class, containing elements
 /// copied from an array.
 /// </summary>
 /// <param name="items">
 /// The array whose elements are to be added to the new LayoutRendererCollection.
 /// </param>
 public LayoutRendererCollection(LayoutRenderer[]items)
 {
     this.AddRange(items);
 }
 /// <summary>
 /// Removes the first occurrence of a specific LayoutRenderer from this LayoutRendererCollection.
 /// </summary>
 /// <param name="value">
 /// The LayoutRenderer value to remove from this LayoutRendererCollection.
 /// </param>
 public virtual void Remove(LayoutRenderer value)
 {
     this.List.Remove(value);
 }
 /// <summary>
 /// Inserts an element into the LayoutRendererCollection at the specified index
 /// </summary>
 /// <param name="index">
 /// The index at which the LayoutRenderer is to be inserted.
 /// </param>
 /// <param name="value">
 /// The LayoutRenderer to insert.
 /// </param>
 public virtual void Insert(int index, LayoutRenderer value)
 {
     this.List.Insert(index, value);
 }
示例#10
0
 /// <summary>
 /// Return the zero-based index of the first occurrence of a specific value
 /// in this LayoutRendererCollection
 /// </summary>
 /// <param name="value">
 /// The LayoutRenderer value to locate in the LayoutRendererCollection.
 /// </param>
 /// <returns>
 /// The zero-based index of the first occurrence of the _ELEMENT value if found;
 /// -1 otherwise.
 /// </returns>
 public virtual int IndexOf(LayoutRenderer value)
 {
     return this.List.IndexOf(value);
 }
示例#11
0
 /// <summary>
 /// Determines whether a specfic LayoutRenderer value is in this LayoutRendererCollection.
 /// </summary>
 /// <param name="value">
 /// The LayoutRenderer value to locate in this LayoutRendererCollection.
 /// </param>
 /// <returns>
 /// true if value is found in this LayoutRendererCollection;
 /// false otherwise.
 /// </returns>
 public virtual bool Contains(LayoutRenderer value)
 {
     return this.List.Contains(value);
 }
示例#12
0
 /// <summary>
 /// Adds an instance of type LayoutRenderer to the end of this LayoutRendererCollection.
 /// </summary>
 /// <param name="value">
 /// The LayoutRenderer to be added to the end of this LayoutRendererCollection.
 /// </param>
 public virtual void Add(LayoutRenderer value)
 {
     this.List.Add(value);
 }
示例#13
0
 /// <summary>
 /// Adds an instance of type LayoutRenderer to the end of this LayoutRendererCollection.
 /// </summary>
 /// <param name="value">
 /// The LayoutRenderer to be added to the end of this LayoutRendererCollection.
 /// </param>
 public virtual void Add(LayoutRenderer value)
 {
     this.List.Add(value);
 }
示例#14
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);
        }
示例#15
0
 /// <summary>
 /// Removes the first occurrence of a specific LayoutRenderer from this LayoutRendererCollection.
 /// </summary>
 /// <param name="value">
 /// The LayoutRenderer value to remove from this LayoutRendererCollection.
 /// </param>
 public virtual void Remove(LayoutRenderer value)
 {
     this.List.Remove(value);
 }
示例#16
0
 /// <summary>
 /// Inserts an element into the LayoutRendererCollection at the specified index
 /// </summary>
 /// <param name="index">
 /// The index at which the LayoutRenderer is to be inserted.
 /// </param>
 /// <param name="value">
 /// The LayoutRenderer to insert.
 /// </param>
 public virtual void Insert(int index, LayoutRenderer value)
 {
     this.List.Insert(index, value);
 }
示例#17
0
 /// <summary>
 /// Return the zero-based index of the first occurrence of a specific value
 /// in this LayoutRendererCollection
 /// </summary>
 /// <param name="value">
 /// The LayoutRenderer value to locate in the LayoutRendererCollection.
 /// </param>
 /// <returns>
 /// The zero-based index of the first occurrence of the _ELEMENT value if found;
 /// -1 otherwise.
 /// </returns>
 public virtual int IndexOf(LayoutRenderer value)
 {
     return(this.List.IndexOf(value));
 }
示例#18
0
 /// <summary>
 /// Determines whether a specfic LayoutRenderer value is in this LayoutRendererCollection.
 /// </summary>
 /// <param name="value">
 /// The LayoutRenderer value to locate in this LayoutRendererCollection.
 /// </param>
 /// <returns>
 /// true if value is found in this LayoutRendererCollection;
 /// false otherwise.
 /// </returns>
 public virtual bool Contains(LayoutRenderer value)
 {
     return(this.List.Contains(value));
 }