CK.Core.CKExceptionData.CreateFrom C# (CSharp) Method

CreateFrom() static public method

Creates a CKExceptionData from any Exception.
static public CreateFrom ( Exception ex ) : CKExceptionData
ex System.Exception Exception for which data must be created. Can be null: null is returned.
return CKExceptionData
        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 );
        }

Usage Example

Example #1
0
 /// <summary>
 /// If <see cref="ExceptionData"/> is null, this method creates the <see cref="CKExceptionData"/>
 /// with the details from this exception.
 /// </summary>
 /// <returns>The <see cref="CKExceptionData"/> that describes this exception.</returns>
 public CKExceptionData EnsureExceptionData()
 {
     if (_exceptionData == null)
     {
         var inner = CKExceptionData.CreateFrom(InnerException);
         _exceptionData = new CKExceptionData(Message, "CKException", GetType().AssemblyQualifiedName, StackTrace, inner, null, null, null, null);
     }
     return(_exceptionData);
 }
All Usage Examples Of CK.Core.CKExceptionData::CreateFrom