EnsureExceptionData() public method

If ExceptionData is null, this method creates the CKExceptionData with the details from this exception.
public EnsureExceptionData ( ) : CKExceptionData
return CKExceptionData
Esempio n. 1
0
        /// <summary>
        /// Creates a <see cref="CKExceptionData"/> from any <see cref="Exception"/>.
        /// </summary>
        /// <param name="ex">Exception for which data must be created. Can be null: null is returned.</param>
        /// <returns>The data that describes the exception.</returns>
        static public CKExceptionData CreateFrom(Exception ex)
        {
            if (ex == null)
            {
                return(null);
            }
            CKException ckEx = ex as CKException;

            if (ckEx != null)
            {
                return(ckEx.EnsureExceptionData());
            }
            Type   t = ex.GetType();
            string exceptionTypeName = t.Name;
            string exceptionTypeAssemblyQualifiedName = t.AssemblyQualifiedName;

            CKExceptionData innerException;

            CKExceptionData[] aggregatedExceptions = null;
            var aggEx = ex as AggregateException;

            if (aggEx != null)
            {
                CKExceptionData[] a = new CKExceptionData[aggEx.InnerExceptions.Count];
                for (int i = 0; i < a.Length; ++i)
                {
                    a[i] = CreateFrom(aggEx.InnerExceptions[i]);
                }
                innerException       = a[0];
                aggregatedExceptions = a;
            }
            else
            {
                innerException = CreateFrom(ex.InnerException);
            }

            string fileName     = null;
            string detailedInfo = null;

            CKExceptionData[] loaderExceptions = null;
            var typeLoadEx = ex as ReflectionTypeLoadException;

            if (typeLoadEx != null)
            {
                CKExceptionData[] a = new CKExceptionData[typeLoadEx.LoaderExceptions.Length];
                for (int i = 0; i < a.Length; ++i)
                {
                    a[i] = CreateFrom(typeLoadEx.LoaderExceptions[i]);
                }
                loaderExceptions = a;
            }
            else
            {
                var fileNFEx = ex as System.IO.FileNotFoundException;
                if (fileNFEx != null)
                {
                    fileName = fileNFEx.FileName;
                    #if NET451 || NET46
                    detailedInfo = fileNFEx.FusionLog.NormalizeEOL();
                    #endif
                }
                else
                {
                    var loadFileEx = ex as System.IO.FileLoadException;
                    if (loadFileEx != null)
                    {
                        fileName = loadFileEx.FileName;
                        #if NET451 || NET46
                        detailedInfo = loadFileEx.FusionLog.NormalizeEOL();
                        #endif
                    }
                    else
                    {
                        #if NET451 || NET46
                        var configEx = ex as System.Configuration.ConfigurationException;
                        if (configEx != null)
                        {
                            fileName = configEx.Filename;
                        }
                        #endif
                    }
                }
            }
            return(new CKExceptionData(ex.Message, exceptionTypeName, exceptionTypeAssemblyQualifiedName, ex.StackTrace, innerException, fileName, detailedInfo, loaderExceptions, aggregatedExceptions));
        }