A property value corresponding to a simple, scalar type.
Inheritance: LogEventPropertyValue
 public bool TryConvertToScalar(object value, ILogEventPropertyValueFactory propertyValueFactory, out ScalarValue result)
 {
     var type = value.GetType();
     #if USE_REFLECTION_40
     if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(Nullable<>))
     #else
     if (!type.IsConstructedGenericType || type.GetGenericTypeDefinition() != typeof(Nullable<>))
     #endif
     {
         result = null;
         return false;
     }
     #if USE_DYNAMIC
     var dynamicValue = (dynamic)value;
     var innerValue = dynamicValue.HasValue ? (object)dynamicValue.Value : null;
     #elif USE_REFLECTION_40
     var targetType = type.GetGenericArguments()[0];
     var innerValue = Convert.ChangeType(value, targetType, null);
     #else
     var targetType = type.GenericTypeArguments[0];
     var innerValue = Convert.ChangeType(value, targetType);
     #endif
     result = propertyValueFactory.CreatePropertyValue(innerValue) as ScalarValue;
     return result != null;
 }
        public bool TryConvertToScalar(object value, ILogEventPropertyValueFactory propertyValueFactory, out ScalarValue result)
        {
            if (_scalarTypes.Contains(value.GetType()))
            {
                result = new ScalarValue(value);
                return true;
            }

            result = null;
            return false;
        }
Ejemplo n.º 3
0
        public bool TryConvertToScalar(object value, ILogEventPropertyValueFactory propertyValueFactory, out ScalarValue result)
        {
            if (value.GetType().GetTypeInfo().IsEnum)
            {
                result = new ScalarValue(value);
                return true;
            }

            result = null;
            return false;
        }
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
        {
            var del = value as Delegate;
            if (del != null)
            {
                result = new ScalarValue(del.ToString());
                return true;
            }

            result = null;
            return false;
        }
        public bool TryConvertToScalar(object value, ILogEventPropertyValueFactory propertyValueFactory, out ScalarValue result)
        {
            // These types and their subclasses are property-laden and deep;
            // most sinks will convert them to strings.
            if (value is Type || value is MemberInfo)
            {
                result = new ScalarValue(value);
                return true;
            }

            result = null;
            return false;
        }
        public Boolean TryConvertToScalar(Object value, ILogEventPropertyValueFactory propertyValueFactory, out ScalarValue result)
        {
            var expression = (value as Expression);

            if (expression != null)
            {
                result = new ScalarValue(expression.ToString());
                return true;
            }

            result = null;
            return false;
        }
        public bool TryConvertToScalar(object value, ILogEventPropertyValueFactory propertyValueFactory, out ScalarValue result)
        {
            var type = value.GetType();
            if (!type.IsConstructedGenericType || type.GetGenericTypeDefinition() != typeof(Nullable<>))
            {
                result = null;
                return false;
            }

            var targetType = type.GenericTypeArguments[0];
            var innerValue = Convert.ChangeType(value, targetType);
            result = propertyValueFactory.CreatePropertyValue(innerValue) as ScalarValue;
            return result != null;
        }
        public bool TryConvertToScalar(object value, ILogEventPropertyValueFactory propertyValueFactory, out ScalarValue result)
        {
            var type = value.GetType();
            if (!type.IsConstructedGenericType || type.GetGenericTypeDefinition() != typeof(Nullable<>))
            {
                result = null;
                return false;
            }

            var dynamicValue = (dynamic)value;
            var innerValue = dynamicValue.HasValue ? (object)dynamicValue.Value : null;
            result = propertyValueFactory.CreatePropertyValue(innerValue) as ScalarValue;

            return result != null;
        }
        public bool TryConvertToScalar(object value, ILogEventPropertyValueFactory propertyValueFactory, out ScalarValue result)
        {
            #if USE_REFLECTION_40
            if (value.GetType().IsEnum)
            #else
            if (value.GetType().GetTypeInfo().IsEnum)
            #endif
            {
                result = new ScalarValue(value);
                return true;
            }

            result = null;
            return false;
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Create properties from the provided log event.
        /// </summary>
        /// <param name="logEvent">The log event.</param>
        /// <returns>A dictionary with properties representing the log event.</returns>
        public static IReadOnlyDictionary<string, LogEventPropertyValue> GetOutputProperties(LogEvent logEvent)
        {
            var result = logEvent.Properties.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            // "Special" output properties like Message will override any properties with the same name
            // when used in format strings; this doesn't affect the rendering of the message template,
            // which uses only the log event properties.

            result[MessagePropertyName] = new LogEventPropertyMessageValue(logEvent.MessageTemplate, logEvent.Properties);
            result[TimestampPropertyName] = new ScalarValue(logEvent.Timestamp);
            result[LevelPropertyName] = new ScalarValue(logEvent.Level);
            result[NewLinePropertyName] = new ScalarValue(Environment.NewLine);

            var exception = logEvent.Exception == null ? "" : (logEvent.Exception + Environment.NewLine);
            result[ExceptionPropertyName] = new ScalarValue(exception);

            return result;
        }
        public bool TryConvertToScalar(object value, ILogEventPropertyValueFactory propertyValueFactory, out ScalarValue result)
        {
            var bytes = value as byte[];
            if (bytes == null)
            {
                result = null;
                return false;
            }

            if (bytes.Length > MaximumByteArrayLength)
            {
                var start = string.Concat(bytes.Take(16).Select(b => b.ToString("X2")));
                var description = start + "... (" + bytes.Length + " bytes)";
                result = new ScalarValue(description);
            }
            else
            {
                result = new ScalarValue(bytes.ToArray());                
            }

            return true;
        }
        /// <summary>
        /// Escapes Dots in Strings and does nothing to objects
        /// </summary>
        protected virtual ScalarValue DotEscapeFieldName(ScalarValue value)
        {
            if (value.Value is string)
            {
                return new ScalarValue(DotEscapeFieldName((string)value.Value));
            }

            return value;
        }
        static LogEventPropertyValue MakeStructure(object value, IEnumerable<PropertyInfo> loggedProperties, Dictionary<PropertyInfo, bool> scalars, ILogEventPropertyValueFactory propertyValueFactory, Type type)
        {
            var structureProperties = new List<LogEventProperty>();
            foreach (var pi in loggedProperties)
            {
                object propValue;
                try
                {
                    propValue = pi.GetValue(value);
                }
                catch (TargetInvocationException ex)
                {
                    SelfLog.WriteLine("The property accessor {0} threw exception {1}", pi, ex);
                    propValue = "The property accessor threw an exception: " + ex.InnerException.GetType().Name;
                }

                LogEventPropertyValue pv;
                bool stringify;

                if (propValue == null)
                {
                    pv = new ScalarValue(null);
                }
                else if (scalars.TryGetValue(pi, out stringify))
                {
                    pv = MakeScalar(propValue, stringify);
                }
                else
                {
                    pv = propertyValueFactory.CreatePropertyValue(propValue, true);
                }

                structureProperties.Add(new LogEventProperty(pi.Name, pv));
            }
            return new StructureValue(structureProperties, type.Name);
        }
 /// <summary>
 /// Escapes Dots in Strings and does nothing to objects
 /// </summary>
 protected virtual ScalarValue DotEscapeFieldName(ScalarValue value)
 {
     var s = value.Value as string;
     return s != null ? new ScalarValue(DotEscapeFieldName(s)) : value;
 }