public static SimpleError ToSimpleErrorModel(this Exception exception) { Type type = exception.GetType(); var error = new SimpleError { Message = GetMessage(exception), Type = type.FullName, StackTrace = exception.StackTrace }; // TODO: Test adding non-serializable objects to ExtendedData and see what happens try { Dictionary <string, object> extraProperties = type.GetPublicProperties().Where(p => !_exceptionExclusions.Contains(p.Name)).ToDictionary(p => p.Name, p => { try { return(p.GetValue(exception, null)); } catch {} return(null); }); extraProperties = extraProperties.Where(kvp => !ValueIsEmpty(kvp.Value)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value); if (extraProperties.Count > 0 && !error.Data.ContainsKey(SimpleError.KnownDataKeys.ExtraProperties)) { error.AddObject(new ExtendedDataInfo { Data = extraProperties, Name = SimpleError.KnownDataKeys.ExtraProperties, IgnoreSerializationErrors = true, MaxDepthToSerialize = 5 }); } } catch {} if (exception.InnerException != null) { error.Inner = exception.InnerException.ToSimpleErrorModel(); } return(error); }
/// <summary> /// Sets the properties from an exception. /// </summary> /// <param name="exception">The exception to populate properties from.</param> /// <param name="client"> /// The ExceptionlessClient instance used for configuration. If a client is not specified, it will use /// ExceptionlessClient.Default. /// </param> public static SimpleError ToSimpleErrorModel(this Exception exception, ExceptionlessClient client = null) { if (client == null) { client = ExceptionlessClient.Default; } var log = client.Configuration.Resolver.GetLog(); Type type = exception.GetType(); var error = new SimpleError { Message = GetMessage(exception), Type = type.FullName, StackTrace = exception.StackTrace }; var exclusions = _exceptionExclusions.Union(client.Configuration.DataExclusions).ToList(); try { if (exception.Data != null) { foreach (object k in exception.Data.Keys) { string key = k != null?k.ToString() : null; if (String.IsNullOrEmpty(key) || key.AnyWildcardMatches(exclusions, true)) { continue; } error.Data[key] = exception.Data[k]; } } } catch (Exception ex) { log.Error(typeof(ExceptionlessClient), ex, "Error populating Data: " + ex.Message); } try { var extraProperties = type.GetPublicProperties().Where(p => !p.Name.AnyWildcardMatches(exclusions, true)).ToDictionary(p => p.Name, p => { try { return(p.GetValue(exception, null)); } catch {} return(null); }); extraProperties = extraProperties.Where(kvp => !ValueIsEmpty(kvp.Value)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value); if (extraProperties.Count > 0 && !error.Data.ContainsKey(SimpleError.KnownDataKeys.ExtraProperties)) { error.AddObject(new ExtendedDataInfo { Data = extraProperties, Name = SimpleError.KnownDataKeys.ExtraProperties, IgnoreSerializationErrors = true, MaxDepthToSerialize = 5 }, client); } } catch {} if (exception.InnerException != null) { error.Inner = exception.InnerException.ToSimpleErrorModel(client); } return(error); }