private static void WriteChangedProperty(object obj, PropertyInfo property, string oldValue, string newValue, bool withOld = true) { var attributes = GetAttributes <LogPropertyAttribute>(property); if (attributes != null && attributes.Length > 0) { foreach (var logPropertyAttribute in attributes) { if (logPropertyAttribute == null) { continue; } if (withOld) { TaskManager.Write(OLD_PREFIX + logPropertyAttribute.GetLogString(obj, property)); } TaskManager.Write(NEW_PREFIX + logPropertyAttribute.GetLogString(property.Name, newValue)); } } else { var name = LogExtensionHelper.DecorateName(property.Name); if (withOld) { TaskManager.Write(OLD_PREFIX + LogExtensionHelper.CombineString(name, oldValue)); } TaskManager.Write(NEW_PREFIX + LogExtensionHelper.CombineString(name, newValue)); } }
/// <summary> /// Write log in the format "name: value" /// </summary> public static void WriteVariable(string name, string value) { try { TaskManager.Write(LogExtensionHelper.CombineString(name, value)); } catch (Exception ex) { WriteException(ex); } }
/// <summary> /// Log a value of the property from expression parameter /// </summary> public static T LogProperty <T, TU>(this T obj, Expression <Func <T, TU> > expression, string nameInLog = null) where T : class { try { var property = ObjectUtils.GetProperty(obj, expression); var propertyName = string.IsNullOrEmpty(nameInLog) ? LogExtensionHelper.DecorateName(property.Name) : nameInLog; var propertyValue = LogExtensionHelper.GetString(property.GetValue(obj, null)); TaskManager.Write(LogExtensionHelper.CombineString(propertyName, propertyValue)); } catch (Exception ex) { WriteException(ex); } return(obj); }
/// <summary> /// Log values of variables which will be written with variable name /// </summary> /// <param name="variablesObs">Dynamic object with variables instead of properties like: new {varA, varB}</param> /// <param name="prefix">Add the string before a variable name</param> public static void WriteVariables(object variablesObs, string prefix = "") { try { if (variablesObs == null) { throw new ArgumentException("variablesObs"); } foreach (var property in variablesObs.GetType().GetProperties()) { var parameterName = LogExtensionHelper.DecorateName(property.Name); var parameterValue = LogExtensionHelper.GetString(property.GetValue(variablesObs, null)); TaskManager.Write(prefix + LogExtensionHelper.CombineString(parameterName, parameterValue)); } } catch (Exception ex) { WriteException(ex); } }