Beispiel #1
0
        public string Format(object value, bool indentMsg)
        {
            ChoStringMsgBuilder msg = new ChoStringMsgBuilder();

            if (((IDictionary)value).Count == 0)
            {
                msg.Append(ChoString.EmptyString);
            }
            else
            {
                msg.AppendLine();
                if (indentMsg)
                {
                    msg.AppendLine("[");
                    foreach (object token in ((IDictionary)value).Keys)
                    {
                        msg.AppendFormatLine(String.Format("{0} - {1}", ChoObject.ToString(token), ChoObject.ToString(((IDictionary)value)[token])));
                    }
                    msg.Append("]");
                }
                else
                {
                    foreach (object token in ((Hashtable)value).Keys)
                    {
                        msg.AppendLine(token.ToString());
                    }
                }
            }

            return(msg.ToString());
        }
        public string Format(object value, bool indentMsg)
        {
            ChoStringMsgBuilder msg = new ChoStringMsgBuilder();

            if (((Hashtable)value).Count == 0)
            {
                msg.Append(ChoString.EmptyString);
            }
            else
            {
                msg.AppendLine();
                if (indentMsg)
                {
                    msg.AppendLine("[");
                    foreach (object token in ((Hashtable)value).Values)
                    {
                        msg.AppendFormatLine(ChoObject.ToString(token));
                    }
                    msg.Append("]");
                }
                else
                {
                    foreach (object token in ((Hashtable)value).Values)
                    {
                        msg.AppendLine(ChoObject.ToString(token));
                    }
                }
            }

            return(msg.ToString());
        }
        protected virtual void TraceOutput(bool isDirty)
        {
            if (!LogCondition)
            {
                return;
            }

            if (ConfigObject == null)
            {
                throw new ChoConfigurationConstructionException("configObject");
            }

            try
            {
                ChoStringMsgBuilder msg = new ChoStringMsgBuilder(String.Format("{0} {1}", isDirty ? "*" : " ", ChoObject.ToString(ConfigObject, ChoFormattableObject.ExtendedFormatName)));

                if (ConfigSection != null && MetaDataInfo != null)
                {
                    msg.AppendNewLine();
                    ChoStringMsgBuilder metaDataMsg = new ChoStringMsgBuilder(ChoObject.ToString(MetaDataInfo));
                    msg.Append(metaDataMsg.ToString());
                }

                Log(msg.ToString());
            }
            catch (Exception ex)
            {
                Trace.Write(ex);
                if (!Silent)
                {
                    throw;
                }
            }
        }
        private static string ToString(object target, bool collectErrMsgs, ChoMemberFormatterAttribute memberFormaterAttribute1,
                                       ChoMemberItemFormatterAttribute memberItemFormaterAttribute1, BindingFlags bindingFlags)
        {
            if (target == null)
            {
                return(String.Empty);
            }

            if (memberFormaterAttribute1 != null && memberFormaterAttribute1.CanFormat())
            {
                if (target.GetType().IsSimple())
                {
                    return(memberFormaterAttribute1.FormatObject(target));
                }
                else
                {
                    ChoStringMsgBuilder msg = new ChoStringMsgBuilder();
                    //msg.AppendLineIfNoNL(memberFormaterAttribute1.FormatObject(target));
                    msg.Append(memberFormaterAttribute1.FormatObject(target));
                    //msg.AppendNewLine();
                    if (collectErrMsgs)
                    {
                        GetErrorMsgs(target, msg);
                    }
                    return(msg.ToString());
                }
            }
            else if (target.GetType().IsSimple() || target.GetType().IsMethodImplemented("ToString"))
            {
                return(target.ToString());
            }
            else if (target is Delegate)
            {
                return(String.Format("{0}.{1}", ((Delegate)target).Target == null ? "[Static]" : ((Delegate)target).Target.GetType().FullName, ((Delegate)target).Method.Name));
            }
            else if (target is IEnumerable)
            {
                StringBuilder arrMsg = new StringBuilder();

                int count = 0;
                foreach (object item in (IEnumerable)target)
                {
                    count++;
                    arrMsg.AppendFormat("{0}{1}", ToString(item, collectErrMsgs, memberItemFormaterAttribute1, null, bindingFlags), Environment.NewLine);
                }

                return("[Count: {0}]{1}{2}".FormatString(count, Environment.NewLine, arrMsg.ToString()));
            }

            else
            {
                bool   foundMatchingFormatter = false;
                string retValue = ChoObject.Format(target, null, out foundMatchingFormatter);

                if (foundMatchingFormatter)
                {
                    if (target.GetType().IsSimple())
                    {
                        return(retValue);
                    }
                    else
                    {
                        ChoStringMsgBuilder msg = new ChoStringMsgBuilder();
                        msg.AppendFormat(retValue);
                        msg.AppendNewLine();
                        if (collectErrMsgs)
                        {
                            GetErrorMsgs(target, msg);
                        }
                        return(msg.ToString());
                    }
                }
                else
                {
                    ChoInterceptableObject interceptableObject = null;
                    try
                    {
                        if (ChoType.IsRealProxyObject(target.GetType()))
                        {
                            interceptableObject = ChoInterceptableObject.Silentable(target as ChoInterceptableObject);
                        }

                        ChoTypeFormatterAttribute objectFormatter = ChoType.GetAttribute(target.GetType(), typeof(ChoTypeFormatterAttribute)) as ChoTypeFormatterAttribute;
                        if (objectFormatter == null)
                        {
                            ChoStringMsgBuilder msg = new ChoStringMsgBuilder(memberFormaterAttribute1 == null ? String.Format("{0} State", target.GetType().FullName)
                                : memberFormaterAttribute1.Name);

                            //MemberInfo[] memberInfos = target.GetType().GetMembers(bindingFlags /*BindingFlags.Public | BindingFlags.Instance /*| BindingFlags.DeclaredOnly*/ /*| BindingFlags.GetField | BindingFlags.GetProperty*/);
                            IEnumerable <MemberInfo> memberInfos = ChoType.GetGetFieldsNProperties(target.GetType(), bindingFlags);
                            if (memberInfos == null || memberInfos.Count() == 0)
                            {
                                msg.AppendFormatLine(ChoStringMsgBuilder.Empty);
                            }
                            else
                            {
                                foreach (MemberInfo memberInfo in memberInfos)
                                {
                                    if (!ChoType.IsValidObjectMember(memberInfo))
                                    {
                                        continue;
                                    }

                                    ChoIgnoreMemberFormatterAttribute memberFormatterIgnoreAttribute = ChoType.GetMemberAttribute(memberInfo, typeof(ChoIgnoreMemberFormatterAttribute)) as ChoIgnoreMemberFormatterAttribute;
                                    if (memberFormatterIgnoreAttribute != null)
                                    {
                                        continue;
                                    }

                                    string memberText = GetNFormatMemberValue(target, memberInfo, collectErrMsgs, null, null, bindingFlags);

                                    int count = 0;
                                    if (GetCountFromMsg(ref memberText, ref count))
                                    {
                                        msg.AppendFormatLine("{0} [Length: {2}]: {1}", memberInfo.Name, memberText, count);
                                    }
                                    else if (!memberText.ContainsHeader())
                                    {
                                        msg.AppendFormatLine("{0}: {1}", memberInfo.Name, memberText);
                                    }
                                    else
                                    {
                                        msg.AppendFormatLine("{0}: {1}", memberInfo.Name, memberText);
                                    }
                                }
                            }
                            msg.AppendNewLine();

                            if (collectErrMsgs)
                            {
                                GetErrorMsgs(target, msg);
                            }

                            return(msg.ToString());
                        }
                        else
                        {
                            ChoStringMsgBuilder msg = new ChoStringMsgBuilder(ChoString.ExpandProperties(target, objectFormatter.Header));;
                            if (objectFormatter.HasFormatSpecified)
                            {
                                msg.AppendFormat(objectFormatter.FormatObject(target));
                            }
                            else
                            {
                                //MemberInfo[] memberInfos = target.GetType().GetMembers(bindingFlags /*BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance /*| BindingFlags.DeclaredOnly*/ /*| BindingFlags.GetField | BindingFlags.GetProperty*/);
                                IEnumerable <MemberInfo> memberInfos = ChoType.GetGetFieldsNProperties(target.GetType(), bindingFlags);

                                if (memberInfos == null || memberInfos.Count() == 0)
                                {
                                    msg.AppendFormatLine(ChoStringMsgBuilder.Empty);
                                }
                                else
                                {
                                    bool isPublicMember = true;
                                    foreach (MemberInfo memberInfo in memberInfos)
                                    {
                                        if (!ChoType.IsValidObjectMember(memberInfo))
                                        {
                                            continue;
                                        }

                                        isPublicMember = true;

                                        ChoIgnoreMemberFormatterAttribute memberFormatterIgnoreAttribute = ChoType.GetMemberAttribute(memberInfo, typeof(ChoIgnoreMemberFormatterAttribute)) as ChoIgnoreMemberFormatterAttribute;
                                        if (memberFormatterIgnoreAttribute != null)
                                        {
                                            continue;
                                        }

                                        ChoMemberFormatterAttribute     memberFormaterAttribute     = ChoType.GetMemberAttribute(memberInfo, typeof(ChoMemberFormatterAttribute)) as ChoMemberFormatterAttribute;
                                        ChoMemberItemFormatterAttribute memberItemFormaterAttribute = ChoType.GetMemberAttribute(memberInfo, typeof(ChoMemberItemFormatterAttribute)) as ChoMemberItemFormatterAttribute;

                                        if (memberInfo is PropertyInfo)
                                        {
                                            MethodInfo getMethod = ((PropertyInfo)memberInfo).GetGetMethod(true);
                                            isPublicMember = getMethod != null && getMethod.IsPublic;
                                        }
                                        else if (memberInfo is FieldInfo)
                                        {
                                            isPublicMember = ((FieldInfo)memberInfo).IsPublic;
                                        }
                                        else
                                        {
                                            continue;
                                        }

                                        if (isPublicMember || (!isPublicMember && memberFormaterAttribute != null))
                                        {
                                            object memberValue = ChoType.GetMemberValue(target, memberInfo);
                                            if (memberValue == target)
                                            {
                                                return(null);
                                            }

                                            string memberText = GetNFormatMemberValue(target, memberInfo, collectErrMsgs, memberFormaterAttribute, memberItemFormaterAttribute, bindingFlags);
                                            string memberName = memberFormaterAttribute == null || memberFormaterAttribute.Name.IsNullOrEmpty() ? memberInfo.Name : ChoPropertyManager.ExpandProperties(target, memberFormaterAttribute.Name);

                                            if (memberFormaterAttribute == null || !memberFormaterAttribute.CanFormat())
                                            {
                                                int count = 0;
                                                if (GetCountFromMsg(ref memberText, ref count))
                                                {
                                                    msg.AppendFormatLine("{0} [Length: {2}]: {1}", memberName, memberText, count);
                                                }
                                                else if (memberText.ContainsHeader())
                                                {
                                                    msg.AppendFormatLine("{0}: {1}", memberName, memberText);
                                                }
                                                else
                                                {
                                                    msg.AppendFormatLine("{0}: {1}", memberName, memberText);
                                                }
                                            }
                                            else if (memberFormaterAttribute.Name == ChoNull.NullString)
                                            {
                                                msg.Append(memberFormaterAttribute.PostFormat(memberName, memberText));
                                            }
                                            else
                                            {
                                                msg.AppendFormat(memberFormaterAttribute.PostFormat(memberName, memberText));
                                            }
                                        }
                                    }
                                }
                            }
                            //msg.AppendNewLine();
                            if (collectErrMsgs)
                            {
                                GetErrorMsgs(target, msg);
                            }

                            return(msg.ToString());
                        }
                    }
                    finally
                    {
                        if (interceptableObject != null)
                        {
                            interceptableObject.Dispose();
                        }
                    }
                }
            }
        }