/// <summary> /// Reads the exception fields from the specified log file reader. /// </summary> /// <param name="reader">The log file reader to read from.</param> /// <returns>The exception data.</returns> internal static FieldLogException Read(FieldLogFileReader reader) { FieldLogException ex = new FieldLogException(); ex.Type = reader.ReadString(); if (reader.FormatVersion >= 2) { ex.TypeModule = reader.ReadString(); ex.Token = reader.ReadInt32(); } ex.Message = reader.ReadString(); ex.Code = reader.ReadInt32(); ex.Data = reader.ReadString(); int frameCount = reader.ReadInt32(); ex.StackFrames = new FieldLogStackFrame[frameCount]; for (int i = 0; i < frameCount; i++) { ex.StackFrames[i] = FieldLogStackFrame.Read(reader); } int innerCount = reader.ReadInt32(); ex.InnerExceptions = new FieldLogException[innerCount]; for (int i = 0; i < innerCount; i++) { ex.InnerExceptions[i] = FieldLogException.Read(reader); } return(ex); }
/// <summary> /// Initialises a new instance of the FieldLogExceptionItem class. /// </summary> /// <param name="priority">The priority of the new log item.</param> /// <param name="ex">The exception instance.</param> /// <param name="context">The context in which the exception has been thrown. Can be an /// arbitrary string that is useful for the logging purpose.</param> /// <param name="customStackTrace">A StackTrace that shall be logged instead of the StackTrace from the Exception instance.</param> public FieldLogExceptionItem(FieldLogPriority priority, Exception ex, string context, StackTrace customStackTrace) : base(priority) { Exception = new FieldLogException(ex, customStackTrace); Context = context; bool includeEnvironment = true; if (context == "AppDomain.FirstChanceException" || context == FL.StackTraceOnlyExceptionContext) { // First-chance exception logging with environment may lead to crashes at WMI // requests, so it's disabled for now. // Testcase: Inspect FieldLogViewer with Snoop while debugging. includeEnvironment = false; } if (includeEnvironment) { EnvironmentData = FieldLogEventEnvironment.Current(); } else { EnvironmentData = FieldLogEventEnvironment.Empty; } Size += Exception.Size + (Context != null ? Context.Length * 2 : 0) + EnvironmentData.Size; }
/// <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 FieldLogExceptionItem Read(FieldLogFileReader reader) { FieldLogExceptionItem item = new FieldLogExceptionItem(); item.ReadBaseData(reader); item.Exception = FieldLogException.Read(reader); item.Context = reader.ReadString(); item.EnvironmentData = FieldLogEventEnvironment.Read(reader); return(item); }
public FieldLogExceptionViewModel(FieldLogException exception) { this.Exception = exception; if (this.Exception.StackFrames != null) { this.StackFrameVMs = this.Exception.StackFrames.Select(sf => new FieldLogStackFrameViewModel(sf)).ToList(); UpdateStackFrames(); } if (this.Exception.InnerExceptions != null) { this.InnerExceptionVMs = this.Exception.InnerExceptions.Select(ie => new FieldLogExceptionViewModel(ie)).ToList(); } }
private bool CompareExceptionTypeRecursive(FieldLogException ex) { bool result = CompareString(ex.Type); if (result) return true; foreach (var ie in ex.InnerExceptions) { result = CompareExceptionTypeRecursive(ie); if (result) return true; } return false; }
/// <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; } } }
/// <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; TypeModule = exType.Module.FullyQualifiedName; 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(")"); } } 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; } } }
/// <summary> /// Reads the exception fields from the specified log file reader. /// </summary> /// <param name="reader">The log file reader to read from.</param> /// <returns>The exception data.</returns> internal static FieldLogException Read(FieldLogFileReader reader) { FieldLogException ex = new FieldLogException(); ex.Type = reader.ReadString(); if (reader.FormatVersion >= 2) { ex.TypeModule = reader.ReadString(); ex.Token = reader.ReadInt32(); } ex.Message = reader.ReadString(); ex.Code = reader.ReadInt32(); ex.Data = reader.ReadString(); int frameCount = reader.ReadInt32(); ex.StackFrames = new FieldLogStackFrame[frameCount]; for (int i = 0; i < frameCount; i++) { ex.StackFrames[i] = FieldLogStackFrame.Read(reader); } int innerCount = reader.ReadInt32(); ex.InnerExceptions = new FieldLogException[innerCount]; for (int i = 0; i < innerCount; i++) { ex.InnerExceptions[i] = FieldLogException.Read(reader); } return ex; }
private string MakeSimpleExMessage(FieldLogException ex) { // Resolve the single inner exception of an AggregateException if (ex.Type == "System.AggregateException" && ex.InnerExceptions != null && ex.InnerExceptions.Length == 1) { return "AggEx: " + MakeSimpleExMessage(ex.InnerExceptions[0]); } // Remove namespace, abbreviate "Exception" to "Ex", make common names even shorter string exType = ex.Type; if (exType == "System.ArgumentException") { exType = "ArgEx"; } else if (exType == "System.ArgumentOutOfRangeException") { exType = "ArgOutOfRangeEx"; } else if (exType == "System.ApplicationException") { exType = "ApplEx"; } else if (exType == "System.InvalidOperationException") { exType = "InvalidOpEx"; } else if (exType == "System.IO.DirectoryNotFoundException") { exType = "DirNotFoundEx"; } else if (exType == "System.NotImplementedException") { exType = "NotImplEx"; } else if (exType == "System.NullReferenceException") { exType = "NullRefEx"; } else if (exType == "System.OperationCanceledException") { exType = "OpCanceledEx"; } else { int dotIndex = exType.LastIndexOf('.'); if (dotIndex > -1) exType = exType.Substring(dotIndex + 1); if (exType.EndsWith("Exception", StringComparison.Ordinal)) exType = exType.Substring(0, exType.Length - "Exception".Length + 2); } // Return shortened exception type and exception message in a single line return exType + ": " + (ex.Message != null ? ex.Message.Trim().Replace("\r", "").Replace("\n", "↲") : ""); }
/// <summary> /// Initialises a new instance of the FieldLogExceptionItem class. /// </summary> /// <param name="priority">The priority of the new log item.</param> /// <param name="ex">The exception instance.</param> /// <param name="context">The context in which the exception has been thrown. Can be an /// arbitrary string that is useful for the logging purpose.</param> /// <param name="customStackTrace">A StackTrace that shall be logged instead of the StackTrace from the Exception instance.</param> public FieldLogExceptionItem(FieldLogPriority priority, Exception ex, string context, StackTrace customStackTrace) : base(priority) { Exception = new FieldLogException(ex, customStackTrace); Context = context; bool includeEnvironment = true; if (context == "AppDomain.FirstChanceException" || context == FL.StackTraceOnlyExceptionContext) { // First-chance exception logging with environment may lead to crashes at WMI // requests, so it's disabled for now. // Testcase: Inspect FieldLogViewer with Snoop while debugging. includeEnvironment = false; } if (includeEnvironment) { EnvironmentData = FieldLogEventEnvironment.Current(); } else { EnvironmentData = FieldLogEventEnvironment.Empty; } Size += Exception.Size + (Context != null ? Context.Length * 2 : 0) + EnvironmentData.Size; }