コード例 #1
0
        IEnumerable <LogEventProperty> GetProperties(object value, ILogEventPropertyValueFactory recursive)
        {
            foreach (var prop in value.GetType().GetPropertiesRecursive())
            {
                object propValue;
                try
                {
                    propValue = prop.GetValue(value);
                }
                catch (TargetParameterCountException)
                {
                    // These properties would ideally be ignored; since they never produce values they're not
                    // of concern to auditing and exceptions can be suppressed.
                    SelfLog.WriteLine("The property accessor {0} is a non-default indexer", prop);
                    continue;
                }
                catch (TargetInvocationException ex)
                {
                    SelfLog.WriteLine("The property accessor {0} threw exception: {1}", prop, ex);

                    if (_propagateExceptions)
                    {
                        throw;
                    }

                    propValue = "The property accessor threw an exception: " + ex.InnerException.GetType().Name;
                }
                yield return(new LogEventProperty(prop.Name, recursive.CreatePropertyValue(propValue, true)));
            }
        }
コード例 #2
0
        public bool TryConvertToScalar(object value, ILogEventPropertyValueFactory propertyValueFactory, out ScalarValue result)
        {
            var type = value.GetType();

            if (!
#if NET40
                type.IsGenericType
#else
                type.IsConstructedGenericType
#endif
                || type.GetGenericTypeDefinition() != typeof(Nullable <>))
            {
                result = null;
                return(false);
            }

            var targetType =
#if NET40
                type.GetGenericArguments()
#else
                type.GenericTypeArguments
#endif
                [0];

            var innerValue = Convert.ChangeType(value, targetType);
            result = propertyValueFactory.CreatePropertyValue(innerValue) as ScalarValue;
            return(result != null);
        }
コード例 #3
0
    private static LogEventPropertyValue Destructure(JObject jo, ILogEventPropertyValueFactory propertyValueFactory)
    {
        string?typeTag = null;
        var    props   = new List <LogEventProperty>(jo.Count);

        foreach (var prop in jo.Properties())
        {
            if (prop.Name == "$type")
            {
                if (prop.Value is JValue typeVal && typeVal.Value is string)
                {
                    typeTag = (string)typeVal.Value;
                    continue;
                }
            }
            else if (!LogEventProperty.IsValidName(prop.Name))
            {
                return(DestructureToDictionaryValue(jo, propertyValueFactory));
            }

            props.Add(new LogEventProperty(prop.Name, propertyValueFactory.CreatePropertyValue(prop.Value, true)));
        }

        return(new StructureValue(props, typeTag));
    }
コード例 #4
0
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
        {
            if (value is Interval interval)
            {
                if (interval.HasStart)
                {
                    // ReSharper disable once ConvertIfStatementToConditionalTernaryExpression
                    if (interval.HasEnd) // start and end
                    {
                        result = propertyValueFactory.CreatePropertyValue(new { interval.Start, interval.End }, destructureObjects: true);
                    }
                    else // start only
                    {
                        result = propertyValueFactory.CreatePropertyValue(new { interval.Start }, destructureObjects: true);
                    }
                }
                else if (interval.HasEnd) // end only
                {
                    result = propertyValueFactory.CreatePropertyValue(new { interval.End }, destructureObjects: true);
                }
                else // neither
                {
                    result = new StructureValue(new LogEventProperty[0]);
                }

                return(true);
            }
            else
            {
                result = null;
                return(false);
            }
        }
コード例 #5
0
 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 TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue?result)
    {
        if (value is Geometry geometry && GeometryTypes.Contains(value.GetType()))
        {
            result = WriteGeometry(propertyValueFactory, geometry);
            return(true);
        }

        if (value is FeatureCollection featureCollection)
        {
            result = WriteFeatureCollection(propertyValueFactory, featureCollection);
            return(true);
        }

        if (value is IFeature feature)
        {
            result = WriteFeature(propertyValueFactory, feature);
            return(true);
        }

        if (value is IAttributesTable attributesTable)
        {
            result = WriteAttributes(propertyValueFactory, attributesTable);
            return(true);
        }

        result = null;
        return(false);
    }
コード例 #7
0
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
        {
            var jo = value as JObject;

            if (jo != null)
            {
                result = Destructure(jo, propertyValueFactory);
                return(true);
            }

            var ja = value as JArray;

            if (ja != null)
            {
                result = Destructure(ja, propertyValueFactory);
                return(true);
            }

            var jv = value as JValue;

            if (jv != null)
            {
                result = Destructure(jv, propertyValueFactory);
                return(true);
            }

            result = null;
            return(false);
        }
コード例 #8
0
 public bool TryCreateLogEventProperty(string name, object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventProperty property)
 {
     property = new LogEventProperty(
         name: _sensitiveInformationType?.ToString() ?? name,
         value: propertyValueFactory.CreatePropertyValue(value).AsSensitive());
     return(true);
 }
コード例 #9
0
        static IEnumerable <LogEventProperty> GetProperties(object value, ILogEventPropertyValueFactory recursive)
        {
            foreach (var prop in value.GetType().GetPropertiesRecursive())
            {
                object propValue;
                try
                {
#if NET40
                    propValue = prop.GetValue(value, null);
#else
                    propValue = prop.GetValue(value);
#endif
                }
                catch (TargetParameterCountException)
                {
                    SelfLog.WriteLine("The property accessor {0} is a non-default indexer", prop);
                    continue;
                }
                catch (TargetInvocationException ex)
                {
                    SelfLog.WriteLine("The property accessor {0} threw exception {1}", prop, ex);
                    propValue = "The property accessor threw an exception: " + ex.InnerException.GetType().Name;
                }
                yield return(new LogEventProperty(prop.Name, recursive.CreatePropertyValue(propValue, true)));
            }
        }
コード例 #10
0
        static IEnumerable <LogEventProperty> GetProperties(object value, ILogEventPropertyValueFactory recursive)
        {
            var valueType = value.GetType();
            var props     = valueType.GetProperties().Where(p => p.CanRead &&
                                                            p.GetGetMethod().IsPublic&&
                                                            !p.GetGetMethod().IsStatic&&
                                                            (p.Name != "Item" || p.GetIndexParameters().Length == 0));

            foreach (var prop in props)
            {
                object propValue;
                try
                {
                    propValue = prop.GetValue(value, null);
                }
                catch (TargetParameterCountException)
                {
                    SelfLog.WriteLine("The property accessor {0} is a non-default indexer", prop);
                    continue;
                }
                catch (TargetInvocationException ex)
                {
                    SelfLog.WriteLine("The property accessor {0} threw exception {1}", prop, ex);
                    propValue = "The property accessor threw an exception: " + ex.InnerException.GetType().Name;
                }
                yield return(new LogEventProperty(prop.Name, recursive.CreatePropertyValue(propValue, true)));
            }
        }
コード例 #11
0
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory,
                                   out LogEventPropertyValue result)
        {
            if (value is TodoItemQuery todoItemQuery)
            {
                result = new StructureValue(new List <LogEventProperty>
                {
                    new LogEventProperty(nameof(todoItemQuery.Id), new ScalarValue(todoItemQuery.Id)),
                    new LogEventProperty(nameof(todoItemQuery.NamePattern), new ScalarValue(todoItemQuery.NamePattern)),
                    new LogEventProperty(nameof(todoItemQuery.IsComplete), new ScalarValue(todoItemQuery.IsComplete)),
                    new LogEventProperty(nameof(todoItemQuery.Owner),
                                         new ScalarValue(todoItemQuery.Owner.GetNameOrDefault())),
                    new LogEventProperty(nameof(todoItemQuery.PageIndex), new ScalarValue(todoItemQuery.PageIndex)),
                    new LogEventProperty(nameof(todoItemQuery.PageSize), new ScalarValue(todoItemQuery.PageSize)),
                    new LogEventProperty(nameof(todoItemQuery.SortBy), new ScalarValue(todoItemQuery.SortBy)),
                    new LogEventProperty(nameof(todoItemQuery.IsSortAscending),
                                         new ScalarValue(todoItemQuery.IsSortAscending))
                });

                return(true);
            }

            result = null;
            return(false);
        }
    private LogEventPropertyValue WriteFeature(ILogEventPropertyValueFactory propertyValueFactory, IFeature value)
    {
        var props = new List <LogEventProperty> {
            new("type", new ScalarValue("Feature"))
        };

        // Add the id here if present.
        if (value.GetOptionalId(_idPropertyName) is { } id)
        {
            props.Add(new LogEventProperty("id", propertyValueFactory.CreatePropertyValue(id, true)));
        }

        // bbox (optional)
        if (WriteBBox(value.BoundingBox, value.Geometry) is { } bBox)
        {
            props.Add(bBox);
        }

        // geometry
        if (value.Geometry != null)
        {
            props.Add(new LogEventProperty("geometry", propertyValueFactory.CreatePropertyValue(value.Geometry, true)));
        }

        // properties
        if (value.Attributes != null)
        {
            props.Add(new LogEventProperty("properties", propertyValueFactory.CreatePropertyValue(value.Attributes, true)));
        }

        return(new StructureValue(props));
    }
コード例 #13
0
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
        {
            var unityObject = value as UnityEngine.Object;

            result = unityObject == null ? null : new UnityObjectValue(unityObject);
            return(unityObject != null);
        }
        private LogEventProperty GetLogProperty(DestructedProperty destProperty,
                                                ILogEventPropertyValueFactory propertyValueFactory)
        {
            var value = propertyValueFactory.CreatePropertyValue(destProperty.Value, destProperty.NeedsDestruct);

            return(new LogEventProperty(destProperty.Name, value));
        }
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
        {
            if (value is T)
            {
                var properties = value.GetType().GetAllProperties();

                var logEventProperties = new List <LogEventProperty>();

                foreach (var propertyInfo in properties)
                {
                    var propValue = propertyValueFactory.CreatePropertyValue(propertyInfo.GetValue(value), destructureObjects: true);

                    if (_propertyNames.Contains(propertyInfo.Name) == _shouldContain)
                    {
                        propValue = propValue.AsSensitive();
                    }

                    logEventProperties.Add(new LogEventProperty(propertyInfo.Name, propValue));
                }

                result = new StructureValue(logEventProperties);
                return(true);
            }

            result = null;
            return(false);
        }
コード例 #16
0
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
        {
            var t = value.GetType();

            lock (_cacheLock)
            {
                if (_ignored.Contains(t))
                {
                    result = null;
                    return(false);
                }

                Func <object, ILogEventPropertyValueFactory, LogEventPropertyValue> cached;
                if (_cache.TryGetValue(t, out cached))
                {
                    result = cached(value, propertyValueFactory);
                    return(true);
                }
            }

            var ti = t.GetTypeInfo();

            var logAsScalar = ti.GetCustomAttribute <LogAsScalarAttribute>();

            if (logAsScalar != null)
            {
                lock (_cacheLock)
                    _cache[t] = (o, f) => MakeScalar(o, logAsScalar.IsMutable);
            }
            else
            {
                var properties = t.GetPropertiesRecursive()
                                 .ToList();
                if (properties.Any(pi =>
                                   pi.GetCustomAttribute <LogAsScalarAttribute>() != null ||
                                   pi.GetCustomAttribute <NotLoggedAttribute>() != null ||
                                   pi.GetCustomAttribute <LogMaskedAttribute>() != null))
                {
                    var loggedProperties = properties
                                           .Where(pi => pi.GetCustomAttribute <NotLoggedAttribute>() == null)
                                           .ToList();

                    var scalars = loggedProperties
                                  .Where(pi => pi.GetCustomAttribute <LogAsScalarAttribute>() != null)
                                  .ToDictionary(pi => pi, pi => pi.GetCustomAttribute <LogAsScalarAttribute>().IsMutable);

                    lock (_cacheLock)
                        _cache[t] = (o, f) => MakeStructure(o, loggedProperties, scalars, f, t);
                }
                else
                {
                    lock (_cacheLock)
                        _ignored.Add(t);
                }
            }

            return(TryDestructure(value, propertyValueFactory, out result));
        }
コード例 #17
0
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
        {
            var t = value.GetType();
            lock (_cacheLock)
            {
                if (_ignored.Contains(t))
                {
                    result = null;
                    return false;
                }

                Func<object, ILogEventPropertyValueFactory, LogEventPropertyValue> cached;
                if (_cache.TryGetValue(t, out cached))
                {
                    result = cached(value, propertyValueFactory);
                    return true;
                }
            }

            var ti = t.GetTypeInfo();

            var logAsScalar = ti.GetCustomAttribute<LogAsScalarAttribute>();
            if (logAsScalar != null)
            {
                lock (_cacheLock)
                    _cache[t] = (o, f) => MakeScalar(o, logAsScalar.IsMutable);

            }
            else
            {
                var properties = t.GetPropertiesRecursive()
                    .ToList();
                if (properties.Any(pi =>
                    pi.GetCustomAttribute<LogAsScalarAttribute>() != null ||
                    pi.GetCustomAttribute<NotLoggedAttribute>() != null))
                {
                    var loggedProperties = properties
                        .Where(pi => pi.GetCustomAttribute<NotLoggedAttribute>() == null)
                        .ToList();

                    var scalars = loggedProperties
                        .Where(pi => pi.GetCustomAttribute<LogAsScalarAttribute>() != null)
                        .ToDictionary(pi => pi, pi => pi.GetCustomAttribute<LogAsScalarAttribute>().IsMutable);

                    lock (_cacheLock)
                        _cache[t] = (o, f) => MakeStructure(value, loggedProperties, scalars, f, t);
                }
                else
                {
                    lock(_cacheLock)
                        _ignored.Add(t);
                }
            }

            return TryDestructure(value, propertyValueFactory, out result);
        }
コード例 #18
0
 /// <inheritdoc />
 public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
 {
     result = null;
     if (!(value is IDefinition definition))
     {
         return(false);
     }
     result = propertyValueFactory.CreatePropertyValue($"{definition.SystemName}:{definition.Type?.SystemName}");
     return(true);
 }
コード例 #19
0
 public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
 {
     if (value is IResponse response)
     {
         result = propertyValueFactory.CreatePropertyValue(MyDestructureFunction(response));
         return(true);
     }
     result = null;
     return(false);
 }
コード例 #20
0
            public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, [MaybeNullWhen(false)] out LogEventPropertyValue result)
            {
                if (value is DependencyProperty prop)
                {
                    result = propertyValueFactory.CreatePropertyValue(new { prop.Name, Type = prop.PropertyType, prop.OwningType }, true);
                    return(true);
                }

                result = null;
                return(false);
            }
コード例 #21
0
        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);
        }
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
        {
            if (value is TransformedObject destObj)
            {
                result = Destruct(destObj, propertyValueFactory);
                return(true);
            }

            result = null;
            return(false);
        }
コード例 #23
0
        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;
        }
コード例 #24
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;
        }
コード例 #25
0
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, [NotNullWhen(true)] out LogEventPropertyValue?result)
        {
            if (value is Version version)
            {
                result = new ScalarValue(version.Major);
                return(true);
            }

            result = null;
            return(false);
        }
コード例 #26
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);
        }
コード例 #27
0
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
        {
            if (value is JObject json)
            {
                result = propertyValueFactory.CreatePropertyValue(json.Properties().ToDictionary(i => i.Name, i => i.Value), true);
                return(true);
            }

            result = null;
            return(false);
        }
コード例 #28
0
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
        {
            if (value is Delegate del)
            {
                result = new ScalarValue(del.ToString());
                return(true);
            }

            result = null;
            return(false);
        }
コード例 #29
0
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
        {
            if (value is T tValue)
            {
                result = propertyValueFactory.CreatePropertyValue(value: _transform(tValue), destructureObjects: true).AsSensitive();
                return(true);
            }

            result = null;
            return(false);
        }
コード例 #30
0
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
        {
            if (value is JValue json)
            {
                result = propertyValueFactory.CreatePropertyValue(json.Value, true);
                return(true);
            }

            result = null;
            return(false);
        }
コード例 #31
0
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
        {
            if (value == null || value is IEnumerable)
            {
                result = null;
                return(false);
            }

            result = Structure(value, propertyValueFactory);

            return(true);
        }
コード例 #32
0
        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;
        }
コード例 #33
0
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
        {
            if (value == null || value.GetType() != typeof(TDestructure))
            {
                result = null;
                return(false);
            }

            result = BuildStructure(value, propertyValueFactory);

            return(true);
        }
コード例 #34
0
    private static LogEventPropertyValue DestructureToDictionaryValue(JObject jo, ILogEventPropertyValueFactory propertyValueFactory)
    {
        var elements = jo.Properties().Select(
            prop =>
            new KeyValuePair <ScalarValue, LogEventPropertyValue>(
                new ScalarValue(prop.Name),
                propertyValueFactory.CreatePropertyValue(prop.Value, true)
                )
            );

        return(new DictionaryValue(elements));
    }
コード例 #35
0
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory,
                                   out LogEventPropertyValue result)
        {
            if (!(value is IGatewayEvent evt))
            {
                result = null;
                return(false);
            }

            var props = new List <LogEventProperty>
            {
                new("Type", new ScalarValue(evt.EventType())),
            };

            void AddMessage(ulong id, ulong channelId, ulong?guildId, ulong?author)
            {
                props.Add(new LogEventProperty("MessageId", new ScalarValue(id)));
                props.Add(new LogEventProperty("ChannelId", new ScalarValue(channelId)));
                props.Add(new LogEventProperty("GuildId", new ScalarValue(guildId ?? 0)));

                if (author != null)
                {
                    props.Add(new LogEventProperty("AuthorId", new ScalarValue(author)));
                }
            }

            if (value is MessageCreateEvent mc)
            {
                AddMessage(mc.Id, mc.ChannelId, mc.GuildId, mc.Author.Id);
            }
            else if (value is MessageUpdateEvent mu)
            {
                AddMessage(mu.Id, mu.ChannelId, mu.GuildId.Value, mu.Author.Value?.Id);
            }
            else if (value is MessageDeleteEvent md)
            {
                AddMessage(md.Id, md.ChannelId, md.GuildId, null);
            }
            else if (value is MessageReactionAddEvent mra)
            {
                AddMessage(mra.MessageId, mra.ChannelId, mra.GuildId, null);
                props.Add(new LogEventProperty("ReactingUserId", new ScalarValue(mra.Emoji)));
                props.Add(new LogEventProperty("Emoji", new ScalarValue(mra.Emoji.Name)));
            }

            // Want shard last, just for visual reasons
            // TODO: D#+ update means we can't pull shard ID out of this, what do?
            // props.Add(new LogEventProperty("Shard", new ScalarValue(dea.Client.ShardId)));

            result = new StructureValue(props);
            return(true);
        }
コード例 #36
0
        public Boolean TryDestructure(Object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
        {
            ScalarValue scalar;

            if (TryConvertToScalar(value, propertyValueFactory, out scalar))
            {
                result = scalar;
                return true;
            }

            result = null;
            return false;
        }
コード例 #37
0
        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;
        }
コード例 #38
0
        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;
        }
コード例 #39
0
        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;
        }
コード例 #40
0
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
        {
            var type = value.GetType();
            if (!type.IsConstructedGenericType || type.GetGenericTypeDefinition() != typeof(Nullable<>))
            {
                result = null;
                return false;
            }

            var dynamicValue = (dynamic)value;
            result = propertyValueFactory.CreatePropertyValue(dynamicValue.HasValue ? dynamicValue.Value : null, true);

            return true;
        }
コード例 #41
0
        public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
        {
            if (value == null) throw new ArgumentNullException("value");

            if (!_canApply(value.GetType()))
            {
                result = null;
                return false;
            }

            var projected = _projection(value);
            result = propertyValueFactory.CreatePropertyValue(projected, true);
            return true;
        }
コード例 #42
0
        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;
        }
コード例 #43
0
        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;
        }
コード例 #44
0
 public bool TryDestructure(object value,
                            ILogEventPropertyValueFactory propertyValueFactory,
                            out LogEventPropertyValue result)
 {
     result = null;
     var wrapper = value as WrappedJObject;
     if (wrapper == null)
     {
         return false;
     }
     var data = wrapper.Value as JObject;
     if (data == null)
     {
         return false;
     }
     var values = ReadProperties(data);
     result = new StructureValue(values);
     return true;
 }
コード例 #45
0
        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;
        }
コード例 #46
0
 static IEnumerable<LogEventProperty> GetProperties(object value, ILogEventPropertyValueFactory recursive)
 {
     var properties =
     #if USE_REFLECTION_40
         value.GetType().GetProperties().Where(p => p.CanRead &&
                                                     p.GetGetMethod().IsPublic &&
                                                     !p.GetGetMethod().IsStatic &&
                                                     (p.Name != "Item" || p.GetIndexParameters().Length == 0));
     #else
         value.GetType().GetPropertiesRecursive();
     #endif
     foreach (var prop in properties)
     {
         object propValue;
         try
         {
     #if USE_REFLECTION_40
             propValue = prop.GetValue(value, null);
     #else
             propValue = prop.GetValue(value);
     #endif
         }
         catch (TargetParameterCountException)
         {
             SelfLog.WriteLine("The property accessor {0} is a non-default indexer", prop);
             continue;
         }
         catch (TargetInvocationException ex)
         {
             SelfLog.WriteLine("The property accessor {0} threw exception {1}", prop, ex);
             propValue = "The property accessor threw an exception: " + ex.InnerException.GetType().Name;
         }
         yield return new LogEventProperty(prop.Name, recursive.CreatePropertyValue(propValue, true));
     }
 }
コード例 #47
0
        static IEnumerable<LogEventProperty> GetProperties(object value, ILogEventPropertyValueFactory recursive)
        {
            var valueType = value.GetType();
            var props = valueType.GetProperties().Where(p => p.CanRead &&
                                                            p.GetGetMethod().IsPublic &&
                                                            !p.GetGetMethod().IsStatic);

            foreach (var prop in props)
            {
                object propValue;
                try
                {
                    propValue = prop.GetValue(value, null);
                }
                catch (TargetInvocationException ex)
                {
                    SelfLog.WriteLine("The property accessor {0} threw exception {1}", prop, ex);
                    propValue = "The property accessor threw an exception: " + ex.InnerException.GetType().Name;
                }
                yield return new LogEventProperty(prop.Name, recursive.CreatePropertyValue(propValue, true));
            }
        }
コード例 #48
0
        static IEnumerable<LogEventProperty> GetProperties(object value, ILogEventPropertyValueFactory recursive)
        {
            var seenNames = new HashSet<string>();

            var valueType = value.GetType().GetTypeInfo();
            while (valueType.AsType() != typeof(object))
            {
                var props = valueType.DeclaredProperties.Where(p => p.CanRead &&
                                                                    p.GetMethod.IsPublic &&
                                                                    !p.GetMethod.IsStatic);

                foreach (var prop in props)
                {
                    if (seenNames.Contains(prop.Name))
                        continue;

                    seenNames.Add(prop.Name);

                    object propValue;
                    try
                    {
                        propValue = prop.GetValue(value);
                    }
                    catch (TargetInvocationException ex)
                    {
                        SelfLog.WriteLine("The property accessor {0} threw exception {1}", prop, ex);
                        propValue = "The property accessor threw an exception: " + ex.InnerException.GetType().Name;
                    }
                    yield return new LogEventProperty(prop.Name, recursive.CreatePropertyValue(propValue, true));
                }

                valueType = valueType.BaseType.GetTypeInfo();
            }
        }
コード例 #49
0
ファイル: SerilogModule.cs プロジェクト: automagic/poshgit2
            public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
            {
                var status = value as IRepositoryStatus;

                if (status == null)
                {
                    result = null;
                    return false;
                }

                var projection = new
                {
                    GitDir = status.GitDir,
                    Index = status.Index?.ToString(),
                    Working = status.Working?.ToString(),
                    Branch = status.Branch
                };

                result = propertyValueFactory.CreatePropertyValue(projection, true);

                return true;

            }
コード例 #50
0
ファイル: SerilogModule.cs プロジェクト: automagic/poshgit2
            public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
            {
                var cwd = value as ICurrentWorkingDirectory;

                if (cwd == null)
                {
                    result = null;
                    return false;
                }

                var projection = new
                {
                    CWD = cwd.CWD,
                    IsValid = cwd.IsValid
                };

                result = propertyValueFactory.CreatePropertyValue(projection, true);

                return true;
            }
コード例 #51
0
        IEnumerable<LogEventProperty> GetProperties(object value, ILogEventPropertyValueFactory recursive)
        {
            foreach (var prop in value.GetType().GetPropertiesRecursive())
            {
                object propValue;
                try
                {
                    propValue = prop.GetValue(value);
                }
                catch (TargetParameterCountException)
                {
                    // These properties would ideally be ignored; since they never produce values they're not
                    // of concern to auditing and exceptions can be suppressed.
                    SelfLog.WriteLine("The property accessor {0} is a non-default indexer", prop);
                    continue;
                }
                catch (TargetInvocationException ex)
                {
                    SelfLog.WriteLine("The property accessor {0} threw exception: {1}", prop, ex);

                    if (_propagateExceptions)
                        throw;

                    propValue = "The property accessor threw an exception: " + ex.InnerException.GetType().Name;
                }
                yield return new LogEventProperty(prop.Name, recursive.CreatePropertyValue(propValue, true));
            }
        }
コード例 #52
0
 static IEnumerable<LogEventProperty> GetProperties(object value, ILogEventPropertyValueFactory recursive)
 {
     return value.GetType()
         .GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty)
         .Select(p => new LogEventProperty(p.Name, recursive.CreatePropertyValue(p.GetValue(value), true)));
 }
コード例 #53
0
        static IEnumerable<LogEventProperty> GetProperties(object value, ILogEventPropertyValueFactory recursive)
        {
            var valueType = value.GetType().GetTypeInfo();
            while (valueType.AsType() != typeof(object))
            {
                var props = valueType.DeclaredProperties.Where(p => p.CanRead &&
                                                                    p.GetMethod.IsPublic &&
                                                                    !p.GetMethod.IsStatic);

                foreach (var prop in props)
                {
                    yield return new LogEventProperty(prop.Name, recursive.CreatePropertyValue(prop.GetValue(value), true));
                }

                valueType = valueType.BaseType.GetTypeInfo();
            }
        }
コード例 #54
0
 static IEnumerable<LogEventProperty> GetProperties(object value, ILogEventPropertyValueFactory recursive)
 {
     foreach (var prop in value.GetType().GetPropertiesRecursive())
     {
         object propValue;
         try
         {
             propValue = prop.GetValue(value);
         }
         catch (TargetParameterCountException)
         {
             SelfLog.WriteLine("The property accessor {0} is a non-default indexer", prop);
             continue;
         }
         catch (TargetInvocationException ex)
         {
             SelfLog.WriteLine("The property accessor {0} threw exception {1}", prop, ex);
             propValue = "The property accessor threw an exception: " + ex.InnerException.GetType().Name;
         }
         yield return new LogEventProperty(prop.Name, recursive.CreatePropertyValue(propValue, true));
     }
 }
コード例 #55
0
        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);
        }