/// <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();
         Serializer.Serialize(stream, errorObject);
     }
     catch
     {
         stream = null;
     }
 }
 protected static Stream SaveErrorObject(ErrorObject errorObject)
 {
     Stream stream = new MemoryStream();
     Serializer.Serialize(stream, errorObject);
     stream.Seek(0, SeekOrigin.Begin);
     return stream;
 }
        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 void SaveErrorObject(ErrorObject errorObject, string path)
        //{

        //    Stream stream = new MemoryStream();
        //    Serializer.Serialize(stream, ErrorVersion);
        //    Serializer.Serialize(stream, errorObject);

        //    stream.Seek(0, SeekOrigin.Begin);
        //    using (var fileStream = File.Create(path))
        //    {
        //        stream.CopyTo(fileStream);
        //    }


        //}
        public static Stream SaveErrorObject(ErrorObject errorObject)
        {
            Stream stream = new MemoryStream();
            //Serializer.Serialize(stream, ErrorVersion);
            Serializer.Serialize(stream, errorObject);
            stream.Seek(0, SeekOrigin.Begin);
            return stream;
        }