Ejemplo n.º 1
0
        private static object ExtractValue(object pairs, PropertyInfo property, bool lowerKey, int depth)
        {
            var value = property.GetValue(pairs, null);

            if (value == null || IsPrimitive(value))
            {
                return(SplunkLogFormatter.FormatValue(value));
            }

            if (value is IEnumerable)
            {
                return(ExtractArray((IEnumerable)value, lowerKey, depth + 1));
            }

            var buf = new StringBuilder("{");

            return(buf.Append(ForLogging(value, lowerKey, depth + 1)).Append('}').ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Takes an anonymous object and converts the properties to the logging format.
        /// </summary>
        /// <param name="pairs">An object with property-value pairs.</param>
        /// <param name="lowerKey">Should the key string be lower cased.</param>
        /// <param name="depth">
        ///     The object traversal depth for this call, since this is a recursive function. Ensures we only log up to MaxDepth
        ///     levels before stopping reflection goodness.
        /// </param>
        /// <returns>
        ///     e.g.
        ///     ForLogging(new {FullName = "Jim", IsCow = false});
        ///     fullname="Jim", iscow="false"
        /// </returns>
        /// <see>VTGWEB-577</see>
        public static string ForLogging(object pairs, bool lowerKey = true, int depth = 0)
        {
            // bail if we have reached the max recursion depth
            if (depth >= MaxDepth)
            {
                return
                    ("log_error=The object being logged contains too much complexity. Please rethink the purpose of this log message and simplify what is being logged.");
            }
            if (pairs == null)
            {
                return(string.Empty);
            }
            if (pairs is string)
            {
                return((string)pairs);
            }
            if (IsPrimitive(pairs))
            {
                return(SplunkLogFormatter.FormatValue(pairs).ToString());
            }

            var strings = new List <string>();

            var type = pairs.GetType();

            foreach (var property in type.GetProperties())
            {
                var key = property.Name;

                if (lowerKey)
                {
                    key = key.ToLower();
                }

                var value = ExtractValue(pairs, property, lowerKey, depth);
                var pair  = string.Format("{0}={1}", key, value);

                strings.Add(pair);
            }

            return(string.Join(", ", strings));
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Takes an anonymous object and converts the properties to the logging format.
        /// </summary>
        /// <param name="pairs">An object with property-value pairs.</param>
        /// <param name="lowerKey">Should the key string be lower cased.</param>
        /// <returns>
        ///     e.g.
        ///     ForLogging(new {FullName = "Jim", IsCow = false});
        ///     fullname="Jim", iscow="false"
        /// </returns>
        /// <see>VTGWEB-577</see>
        public static string LogDictionary(IDictionary <string, object> pairs, bool lowerKey = true)
        {
            if (pairs == null)
            {
                return(string.Empty);
            }

            var strings = new List <string>();

            foreach (var item in pairs)
            {
                var key = item.Key;
                if (lowerKey)
                {
                    key = key.ToLower();
                }

                var value = SplunkLogFormatter.FormatValue(item.Value);
                var pair  = string.Format("{0}={1}", key, value);
                strings.Add(pair);
            }

            return(string.Join(", ", strings));
        }