/// <summary> /// Summarizes an exception object into the specified stream /// </summary> /// <param name="errorObject">The error object</param> /// <param name="stream">The object will be added to this stream.</param> public static void GetErrorObjectStream(ErrorObject errorObject, out Stream stream) { try { stream = new MemoryStream(); IFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, ErrorVersion); formatter.Serialize(stream, errorObject); } catch { stream = null; } }
/// <summary> /// Stores the error object in the database /// </summary> /// <param name="errorObject">Error object to store</param> public static void AddException(ErrorObject errorObject) { try { var dc = new ManagementContext(); var databaseError = new DataModels.Exception.Exception { AdditionalInformation = errorObject.AdditionalInformation, AssemblyName = errorObject.AssemblyName, AssemblyVersion = errorObject.AssemblyVersion, Created = errorObject.Created, ErrorNameSpace = errorObject.ErrorNameSpace, MethodName = errorObject.MethodName, NameSpace = errorObject.NameSpace, Group = errorObject.ErrorGroup.HasValue ? (byte?)errorObject.ErrorGroup.Value : null, Severity = errorObject.ErrorSeverity.HasValue ? (byte?)errorObject.ErrorSeverity.Value : null }; if (errorObject.ErrorData != null && errorObject.ErrorData.Count > 0) { foreach (var errorDataDetail in errorObject.ErrorData) { var databaseErrorData = new DataModels.Exception.ExceptionData { DataType = (byte)errorDataDetail.DataType, Name = errorDataDetail.Key, Data = errorDataDetail.Value }; databaseError.ExceptionData.Add(databaseErrorData); } } if (errorObject.ErrorExceptions != null) foreach (var exception in errorObject.ErrorExceptions) { var exceptionErrorDetail = new DataModels.Exception.ExceptionDetail(); exceptionErrorDetail.Depth = exception.Depth; exceptionErrorDetail.Message = exception.Message; exceptionErrorDetail.MethodName = exception.MethodName; exceptionErrorDetail.StackTrace = exception.StackTrace; databaseError.ExceptionDetails.Add(exceptionErrorDetail); } dc.ErrorExceptions.Add(databaseError); dc.SaveChanges(); } catch (Exception except) { AddException(except, except.GetType()); } }
protected static ErrorObject GenerateErrorObject(Exception e, Type type = null, ErrorGroupEnum? errorGroup = null, ErrorSeverityEnum? errorSeverity = null, IDictionary<string, string> sessionData = null, IDictionary<string, string> cookieData = null, IDictionary<string, string> userData = null, IDictionary<string, string> serverVariablesData = null, IList<Expression<Func<object>>> parameters = null, string additionalInformation = null) { var output = new ErrorObject(e) { ErrorGroup = errorGroup, ErrorSeverity = errorSeverity, AdditionalInformation = additionalInformation }; #region Parameter data // Do we have any parameter or variable ? if (parameters != null && parameters.Count > 0) { var parameterValue = new StringBuilder(); foreach (var parameter in parameters) { parameterValue.Clear(); // Clear old values var detail = new ErrorDataDetail { Key = string.Empty, Value = string.Empty, DataType = ErrorDataDetail.ErrorDataType.Parameter }; // Get the name and value seperatly in case one of them fails. try { // Get the parameter name var expression = parameter.Body as UnaryExpression; if (expression != null) { var expression2 = (MemberExpression)expression.Operand; detail.Key = expression2.Member.Name; } else { var expression2 = (MemberExpression)parameter.Body; detail.Key = expression2.Member.Name; } } catch (Exception) { detail.Key = "Unknown (error)"; } try { // Get the parameter value by compiling it and retrieving the value var invokedData = parameter.Compile().Invoke(); // Check if its a dictionary var collection = invokedData as IDictionary; if (collection != null) { // The keys in the collection var keys = collection.Keys; // Loop through the keys and write key/value foreach (var key in keys) parameterValue.Append(string.Format("{0}={1}, ", key, collection[key])); // If we have added something to the value, then remove the ,{space} at the end of the string detail.Value = parameterValue.Length > 0 ? parameterValue.ToString().Substring(0, parameterValue.Length - 2) : parameterValue.ToString(); output.ErrorData.Add(detail); continue; } // Check if its a list var list = invokedData as IList; if (list != null) { // Loop through the data and write it foreach (var colData in list) parameterValue.Append(string.Format("{0}, ", colData)); // If we have added something to the value, then remove the ,{space} at the end of the string detail.Value = parameterValue.Length > 0 ? parameterValue.ToString().Substring(0, parameterValue.Length - 2) : parameterValue.ToString(); output.ErrorData.Add(detail); continue; } if (invokedData == null || String.IsNullOrEmpty(invokedData.ToString())) { // Save the value detail.Value = string.Empty; output.ErrorData.Add(detail); } else { // Save the value detail.Value = invokedData.ToString(); output.ErrorData.Add(detail); } } catch (Exception) { detail.Value = "Unknown (error)"; output.ErrorData.Add(detail); } } } #endregion if (sessionData != null && sessionData.Count > 0) { foreach (var session in sessionData) { output.ErrorData.Add(new ErrorDataDetail { Key = session.Key, Value = session.Value, DataType = ErrorDataDetail.ErrorDataType.Session }); } } if (userData != null && userData.Count > 0) { foreach (var user in userData) { output.ErrorData.Add(new ErrorDataDetail { Key = user.Key, Value = user.Value, DataType = ErrorDataDetail.ErrorDataType.UserData }); } } if (cookieData != null && cookieData.Count > 0) { foreach (var cookie in cookieData) { output.ErrorData.Add(new ErrorDataDetail { Key = cookie.Key, Value = cookie.Value, DataType = ErrorDataDetail.ErrorDataType.Cookie }); } } if (serverVariablesData != null && serverVariablesData.Count > 0) { foreach (var serverVariable in serverVariablesData) { output.ErrorData.Add(new ErrorDataDetail { Key = serverVariable.Key, Value = serverVariable.Value, DataType = ErrorDataDetail.ErrorDataType.ServerVariable }); } } var assembly = type.Assembly; var assemblyInfo = assembly.GetName(); output.AssemblyName = assemblyInfo.Name; output.AssemblyVersion = assemblyInfo.Version.ToString(); output.Created = DateTime.UtcNow; output.NameSpace = type.Namespace; output.ErrorNameSpace = e.Source; output.MethodName = type.Name.IndexOf('`') > 0 ? type.Name.Substring(0, type.Name.IndexOf('`')) : type.Name; return output; }
protected static Stream SaveErrorObject(ErrorObject errorObject) { BinaryFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); formatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple; formatter.Serialize(stream, ErrorVersion); formatter.Serialize(stream, errorObject); stream.Seek(0, SeekOrigin.Begin); return stream; }
protected static void SaveErrorObject(ErrorObject errorObject, string path) { IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); formatter.Serialize(stream, ErrorVersion); formatter.Serialize(stream, errorObject); stream.Seek(0, SeekOrigin.Begin); using (var fileStream = File.Create(path)) { stream.CopyTo(fileStream); } }