Exemplo n.º 1
0
        /// <summary>
        /// Reads the log item fields from the specified log file reader.
        /// </summary>
        /// <param name="reader">The log file reader to read from.</param>
        /// <returns>The read log item.</returns>
        internal static FieldLogDataItem Read(FieldLogFileReader reader)
        {
            FieldLogDataItem item = new FieldLogDataItem();

            item.ReadBaseData(reader);
            item.Name  = reader.ReadString();
            item.Value = reader.ReadString();
            return(item);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reads a log item from the log file.
        /// </summary>
        /// <param name="reader">Log file reader.</param>
        /// <param name="type">The type of log item to read.</param>
        /// <returns>The read log item.</returns>
        internal static FieldLogItem Read(FieldLogFileReader reader, FieldLogItemType type)
        {
            switch (type)
            {
            case FieldLogItemType.Text:
                return(FieldLogTextItem.Read(reader));

            case FieldLogItemType.Data:
                return(FieldLogDataItem.Read(reader));

            case FieldLogItemType.Exception:
                return(FieldLogExceptionItem.Read(reader));

            case FieldLogItemType.Scope:
            case FieldLogItemType.RepeatedScope:
                return(FieldLogScopeItem.Read(reader, type == FieldLogItemType.RepeatedScope));
            }
            throw new ArgumentException("Unsupported log item type (" + (int)type + ")");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initialises a new instance of the FieldLogException class.
        /// </summary>
        /// <param name="ex">The Exception instance.</param>
        /// <param name="customStackTrace">A StackTrace that shall be logged instead of the StackTrace from the Exception instance.</param>
        public FieldLogException(Exception ex, StackTrace customStackTrace)
        {
            Exception = ex;
            Type exType = ex.GetType();

            Type = exType.FullName;
            if (!exType.Module.Name.Contains("<"))
            {
                TypeModule = exType.Module.FullyQualifiedName;
            }
            else
            {
                TypeModule = exType.Module.Name;
            }
            Token   = exType.MetadataToken;
            Message = ex.Message.TrimEnd();
            StackTrace stackTrace = customStackTrace;

            if (stackTrace == null)
            {
                stackTrace = new StackTrace(ex, true);
            }
            StackFrames = new FieldLogStackFrame[stackTrace.FrameCount];
            for (int i = 0; i < stackTrace.FrameCount; i++)
            {
                StackFrames[i] = new FieldLogStackFrame(stackTrace.GetFrame(i));
            }

            StringBuilder dataSb = new StringBuilder();

            if (ex.Data != null)
            {
                foreach (DictionaryEntry x in ex.Data)
                {
                    dataSb.Append("Data[").Append(x.Key).Append("]");
                    if (x.Value != null)
                    {
                        dataSb.Append(" (")
                        .Append(x.Value.GetType().Name)
                        .Append("): ")
                        .Append(Convert.ToString(x.Value, CultureInfo.InvariantCulture));
                    }
                    else
                    {
                        dataSb.Append(": null");
                    }
                    dataSb.Append("\n");
                }
            }

            // Find more properties through reflection
            PropertyInfo[] props = exType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo prop in props)
            {
                // Known properties, already handled
                if (prop.Name == "Message")
                {
                    continue;
                }
                if (prop.Name == "StackTrace")
                {
                    continue;
                }
                if (prop.Name == "ErrorCode")
                {
                    continue;
                }
                if (prop.Name == "Data")
                {
                    continue;
                }
                if (prop.Name == "InnerException")
                {
                    continue;
                }
                if (prop.Name == "InnerExceptions")
                {
                    continue;
                }
                if (prop.Name == "TargetSite")
                {
                    continue;
                }
                if (prop.Name == "HelpLink")
                {
                    continue;
                }
                if (prop.Name == "Source")
                {
                    continue;
                }

                try
                {
                    object value = prop.GetValue(ex, null);                       // Indexed properties are not supported here!
                    dataSb.Append(prop.Name);
                    if (value != null)
                    {
                        dataSb.Append(" (").Append(value.GetType().Name).Append("): ").Append(Convert.ToString(value, CultureInfo.InvariantCulture));
                        if (value is byte)
                        {
                            dataSb.Append(" (0x").Append(((byte)value).ToString("X2")).Append(")");
                        }
                        if (value is sbyte)
                        {
                            dataSb.Append(" (0x").Append(((sbyte)value).ToString("X2")).Append(")");
                        }
                        if (value is ushort)
                        {
                            dataSb.Append(" (0x").Append(((ushort)value).ToString("X4")).Append(")");
                        }
                        if (value is short)
                        {
                            dataSb.Append(" (0x").Append(((short)value).ToString("X")).Append(")");
                        }
                        if (value is uint)
                        {
                            dataSb.Append(" (0x").Append(((uint)value).ToString("X8")).Append(")");
                        }
                        if (value is int)
                        {
                            dataSb.Append(" (0x").Append(((int)value).ToString("X8")).Append(")");
                        }
                        if (value is ulong)
                        {
                            dataSb.Append(" (0x").Append(((ulong)value).ToString("X16")).Append(")");
                        }
                        if (value is long)
                        {
                            dataSb.Append(" (0x").Append(((long)value).ToString("X16")).Append(")");
                        }

                        if (exType.FullName == "System.Data.Entity.Validation.DbEntityValidationException" &&
                            prop.Name == "EntityValidationErrors")
                        {
                            dataSb.Append("\n");
                            dataSb.Append(FieldLogDataItem.FormatValues(value));
                        }
                    }
                    else
                    {
                        dataSb.Append(": null");
                    }
                    dataSb.Append("\n");
                }
                catch (Exception ex2)
                {
                    dataSb.Append("Exception property \"")
                    .Append(prop.Name)
                    .Append("\" cannot be retrieved. (")
                    .Append(ex2.GetType().Name)
                    .Append(": ")
                    .Append(ex2.Message)
                    .Append(")\n");
                }
            }
            Data = dataSb.ToString().TrimEnd();

#if !NET20
            AggregateException aex = ex as AggregateException;
            if (aex != null)
            {
                InnerExceptions = new FieldLogException[aex.InnerExceptions.Count];
                for (int i = 0; i < aex.InnerExceptions.Count; i++)
                {
                    InnerExceptions[i] = new FieldLogException(aex.InnerExceptions[i]);
                }
            }
            else
#endif
            if (ex.InnerException != null)
            {
                InnerExceptions    = new FieldLogException[1];
                InnerExceptions[0] = new FieldLogException(ex.InnerException);
            }

            ExternalException eex = ex as ExternalException;               // e.g. COMException
            if (eex != null)
            {
                Code = eex.ErrorCode;
            }

            Size = (Type != null ? Type.Length * 2 : 0) +
                   4 +
                   (Message != null ? Message.Length * 2 : 0) +
                   4 +
                   (Data != null ? Data.Length * 2 : 0);
            foreach (FieldLogStackFrame sf in StackFrames)
            {
                Size += sf.Size;
            }
            if (InnerExceptions != null)
            {
                foreach (FieldLogException ex2 in InnerExceptions)
                {
                    Size += ex2.Size;
                }
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Reads the log item fields from the specified log file reader.
 /// </summary>
 /// <param name="reader">The log file reader to read from.</param>
 /// <returns>The read log item.</returns>
 internal static FieldLogDataItem Read(FieldLogFileReader reader)
 {
     FieldLogDataItem item = new FieldLogDataItem();
     item.ReadBaseData(reader);
     item.Name = reader.ReadString();
     item.Value = reader.ReadString();
     return item;
 }
 public FieldLogDataItemViewModel(FieldLogDataItem item)
 {
     this.Item = item;
     base.Item = item;
 }