Example #1
0
        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));
            }
        }
Example #2
0
 /// <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);
     }
 }
Example #3
0
        /// <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);
        }
Example #4
0
        /// <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);
            }
        }
Example #5
0
        private static T LogPropertyIfChanged <T>(T obj, PropertyInfo property, object newValue, bool withOld = true)
            where T : class
        {
            if (property == null)
            {
                return(obj);
            }

            object oldValue;

            try
            {
                oldValue = property.GetValue(obj, null);
            }
            catch (Exception ex)
            {
                TaskManager.Write("Cant get {0} property", property.Name);
                return(obj);
            }

            var oldValueStr = LogExtensionHelper.GetString(oldValue);
            var newValueStr = LogExtensionHelper.GetString(newValue);

            if (oldValue == null || newValue == null)
            {
                if (oldValue != newValue)
                {
                    WriteChangedProperty(obj, property, oldValueStr, newValueStr, withOld);
                }
            }
            else if (!oldValueStr.Equals(newValueStr))
            {
                WriteChangedProperty(obj, property, oldValueStr, newValueStr, withOld);
            }

            return(obj);
        }