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))); } }
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); }
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)); }
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); } }
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); }
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); }
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); }
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))); } }
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))); } }
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)); }
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); }
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)); }
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); }
/// <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); }
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); }
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); }
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); }
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 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, [NotNullWhen(true)] out LogEventPropertyValue?result) { if (value is Version version) { result = new ScalarValue(version.Major); return(true); } result = null; return(false); }
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) { if (value is JObject json) { result = propertyValueFactory.CreatePropertyValue(json.Properties().ToDictionary(i => i.Name, i => i.Value), true); return(true); } result = null; return(false); }
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); }
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); }
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); }
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); }
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 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); }
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)); }
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); }
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; }
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) { // 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 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 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; }
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; }
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; }
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 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; }
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; }
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)); } }
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)); } }
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(); } }
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; }
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; }
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)); } }
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))); }
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(); } }
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)); } }
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); }