CK.Core.ActivityMonitorTextWriterClient.DumpException C# (CSharp) Method

DumpException() static public method

Recursively dumps an Exception as readable text.
static public DumpException ( StringBuilder w, string prefix, bool displayMessage, Exception ex ) : void
w StringBuilder The TextWriter to write to.
prefix string Prefix that will start all lines.
displayMessage bool Whether the exception message must be displayed or skip.
ex System.Exception The exception to display.
return void
        static public void DumpException( StringBuilder w, string prefix, bool displayMessage, Exception ex )
        {
            CKException ckEx = ex as CKException;
            if( ckEx != null && ckEx.ExceptionData != null )
            {
                ckEx.ExceptionData.ToStringBuilder( w, prefix );
                return;
            }

            string header = String.Format( " ┌──────────────────────────■ Exception : {0} ■──────────────────────────", ex.GetType().Name );

            string p;
            w.AppendLine( prefix + header );
            string localPrefix = prefix + " | ";
            if( displayMessage && ex.Message != null )
            {
                w.Append( localPrefix + "Message: " );
                w.AppendMultiLine( localPrefix + "         ", ex.Message, false );
                w.AppendLine();
            }
            if( ex.StackTrace != null )
            {
                w.Append( localPrefix + "Stack: " );
                w.AppendMultiLine( localPrefix + "       ", ex.StackTrace, false );
                w.AppendLine();
            }
            var fileNFEx = ex as System.IO.FileNotFoundException;
            if( fileNFEx != null )
            {
                if( !String.IsNullOrEmpty( fileNFEx.FileName ) ) w.AppendLine( localPrefix + "FileName: " + fileNFEx.FileName );
                #if NET451 || NET46
                if( fileNFEx.FusionLog != null )
                {
                    w.Append( localPrefix + "FusionLog: " );
                    w.AppendMultiLine( localPrefix + "         ", fileNFEx.FusionLog, false );
                    w.AppendLine();
                }
                #endif
            }
            else
            {
                var loadFileEx = ex as System.IO.FileLoadException;
                if( loadFileEx != null )
                {
                    if( !String.IsNullOrEmpty( loadFileEx.FileName ) ) w.AppendLine( localPrefix + "FileName: " + loadFileEx.FileName );
                    #if NET451 || NET46
                    if( loadFileEx.FusionLog != null )
                    {
                        w.Append( localPrefix + "FusionLog: " );
                        w.AppendMultiLine( localPrefix + "         ", loadFileEx.FusionLog, false );
                        w.AppendLine();
                    }
                    #endif
                }
                else
                {
                    var typeLoadEx = ex as ReflectionTypeLoadException;
                    if( typeLoadEx != null )
                    {
                        w.AppendLine( localPrefix + " ┌──────────────────────────■ [Loader Exceptions] ■──────────────────────────" );
                        p = localPrefix + " | ";
                        foreach( var item in typeLoadEx.LoaderExceptions )
                        {
                            DumpException( w, p, true, item );
                        }
                        w.AppendLine( localPrefix + " └─────────────────────────────────────────────────────────────────────────" );
                    }
                    #if NET451 || NET46
                    else
                    {
                        var configEx = ex as System.Configuration.ConfigurationException;
                        if( configEx != null )
                        {
                            if( !String.IsNullOrEmpty( configEx.Filename ) ) w.AppendLine( localPrefix + "FileName: " + configEx.Filename );
                        }
                    }
                    #endif
                }
            }
            // The InnerException of an aggregated exception is the same as the first of it InnerExceptionS.
            // (The InnerExceptionS are the contained/aggregated exceptions of the AggregatedException object.)
            // This is why, if we are on an AggregatedException we do not follow its InnerException.
            var aggrex = ex as AggregateException;
            if( aggrex != null && aggrex.InnerExceptions.Count > 0 )
            {
                w.AppendLine( localPrefix + " ┌──────────────────────────■ [Aggregated Exceptions] ■──────────────────────────" );
                p = localPrefix + " | ";
                foreach( var item in aggrex.InnerExceptions )
                {
                    DumpException( w, p, true, item );
                }
                w.AppendLine( localPrefix + " └─────────────────────────────────────────────────────────────────────────" );
            }
            else if( ex.InnerException != null )
            {
                w.AppendLine( localPrefix + " ┌──────────────────────────■ [Inner Exception] ■──────────────────────────" );
                p = localPrefix + " | ";
                DumpException( w, p, true, ex.InnerException );
                w.AppendLine( localPrefix + " └─────────────────────────────────────────────────────────────────────────" );
            }
            w.AppendLine( prefix + " └" + new string( '─', header.Length - 2 ) );
        }

Usage Example

        static string DumpErrorText(DateTimeStamp logTime, string text, LogLevel level, Exception ex, CKTrait tags)
        {
            StringBuilder buffer = CreateHeader(logTime, text, level, tags);

            if (ex != null)
            {
                ActivityMonitorTextWriterClient.DumpException(buffer, String.Empty, !ReferenceEquals(text, ex.Message), ex);
            }
            WriteFooter(level, buffer);
            return(buffer.ToString());
        }