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))); } }
private LogEventProperty GetLogProperty(DestructedProperty destProperty, ILogEventPropertyValueFactory propertyValueFactory) { var value = propertyValueFactory.CreatePropertyValue(destProperty.Value, destProperty.NeedsDestruct); return(new LogEventProperty(destProperty.Name, value)); }
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))); } }
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; }
bool IDestructuringPolicy.TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, [MaybeNullWhen(false)] out LogEventPropertyValue result) { if (value is ContainerElement container) { result = propertyValueFactory.CreatePropertyValue(new { Type = container.GetType(), Children = container.ToArray() }, true); return(true); } if (value is Element element) { result = propertyValueFactory.CreatePropertyValue(new { Type = element.GetType() }, true); return(true); } result = null; return(false); }
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))); } }
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 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 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); }
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); }
/// <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, [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 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 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 is T tValue) { result = propertyValueFactory.CreatePropertyValue(value: _transform(tValue), destructureObjects: true).AsSensitive(); return(true); } result = null; return(false); }
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 DateTimeZone dtz) { result = propertyValueFactory.CreatePropertyValue(dtz.Id); return(true); } else { result = null; return(false); } }
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result) { var props = value.GetType().GetTypeInfo().DeclaredProperties; var logEventProperties = new List <LogEventProperty>(); foreach (var propertyInfo in props) { switch (propertyInfo.Name.ToLower()) { case "cardnumber": case "password": logEventProperties.Add(new LogEventProperty(propertyInfo.Name, propertyValueFactory.CreatePropertyValue("***"))); break; default: logEventProperties.Add(new LogEventProperty(propertyInfo.Name, propertyValueFactory.CreatePropertyValue(propertyInfo.GetValue(value)))); break; } } result = new StructureValue(logEventProperties); return(true); }
public virtual bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue?result) { if (value is T t) { Validator?.Invoke(t); result = propertyValueFactory.CreatePropertyValue(Pattern.Format(t)); return(true); } else { result = null; return(false); } }
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) { 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 request = value as RegisterNewUserRequest; if (request == null) { result = null; return(false); } var logEventProperties = new List <LogEventProperty> { new LogEventProperty(nameof(request.Claims), propertyValueFactory.CreatePropertyValue(request.Claims)), new LogEventProperty(nameof(request.Email), propertyValueFactory.CreatePropertyValue(request.Email)), new LogEventProperty(nameof(request.Password), propertyValueFactory.CreatePropertyValue("****")), new LogEventProperty(nameof(request.Roles), propertyValueFactory.CreatePropertyValue(request.Roles)), new LogEventProperty(nameof(request.UserName), propertyValueFactory.CreatePropertyValue(request.UserName)) }; result = new StructureValue(logEventProperties); return(true); }
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result) { if (value is T tValue && currentValue != value) { currentValue = value; var propValue = _stringify ? tValue.ToString() as object : tValue; result = propertyValueFactory.CreatePropertyValue(value: propValue, destructureObjects: true).AsSensitive(); currentValue = null; return(true); } result = null; return(false); }
private LogEventPropertyValue WriteAttributes(ILogEventPropertyValueFactory propertyValueFactory, IAttributesTable value) { var props = new List <LogEventProperty>(); foreach (var propertyName in value.GetNames()) { // skip id if (propertyName != _idPropertyName) { props.Add(new LogEventProperty(propertyName, propertyValueFactory.CreatePropertyValue(value[propertyName], true))); } } return(new StructureValue(props)); }
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) { 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(nameof(value)); } if (!this._canApply(value.GetType())) { result = (LogEventPropertyValue)null; return(false); } object obj = this._projection(value); result = propertyValueFactory.CreatePropertyValue(obj, true); return(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; } var maskedAttribute = pi.GetCustomAttribute <LogMaskedAttribute>(); if (maskedAttribute != null) { // Only for string values if (propValue is string) { FormatMaskedValue(ref propValue, maskedAttribute); } } 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)); }
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) { 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); }
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(); } }
public bool TryDestructure(object value, ILogEventPropertyValueFactory factory, out LogEventPropertyValue result) { result = null; if (!(value is PatchObject po)) { return(false); } var propList = new List <LogEventProperty>(); foreach (var props in po.GetType().GetProperties()) { var propValue = props.GetValue(po); if (propValue is IPartial p && p.IsPresent) { propList.Add(new LogEventProperty(props.Name, factory.CreatePropertyValue(p.RawValue, 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); }
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)); } }
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) { 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(); } }
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); }
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)); } }
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; }
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) { return value.GetType() .GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty) .Select(p => new LogEventProperty(p.Name, recursive.CreatePropertyValue(p.GetValue(value), true))); }