Advertisement
EmilySamantha80

Format exceptions as detailed strings

Mar 7th, 2017
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.58 KB | None | 0 0
  1. // Title:  Format exceptions as detailed strings
  2. // Author: Emily Heiner
  3. // This code is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported license.
  4. // You are free to share and adapt this code for any purposes, commerical and non-commercial,
  5. // as long as appropriate credit is given, you indicate if changes were made, and you share your
  6. // modified code. License details can be found at https://creativecommons.org/licenses/by-sa/3.0/
  7.  
  8. using System;
  9. using System.Text;
  10.  
  11. namespace ESH.Utility
  12. {
  13.     public static class ExceptionFormatter
  14.     {
  15.         public static string ExceptionAsString(Exception ex, bool useHtmlNewline = false)
  16.         {
  17.             StringBuilder errorDetail = new StringBuilder();
  18.             if (ex != null)
  19.             {
  20.                 Exception orgEx = ex;
  21.                 errorDetail.Append("[Exceptions]: ");
  22.                 errorDetail.Append(Environment.NewLine);
  23.                 int i = 1;
  24.                 while (orgEx != null)
  25.                 {
  26.                     errorDetail.Append("   #" + i++.ToString() + ": ");
  27.                     errorDetail.Append("(" + orgEx.GetType().ToString() + ") " + orgEx.Message);
  28.                     errorDetail.Append(Environment.NewLine);
  29.                     orgEx = orgEx.InnerException;
  30.                     if (orgEx != null && orgEx.GetType() == typeof(AggregateException))
  31.                     {
  32.                         orgEx = orgEx.InnerException;
  33.                     }
  34.                 }
  35.                 while (ex != null)
  36.                 {
  37.                     errorDetail.Append(Environment.NewLine);
  38.                     if (ex.GetType() == typeof(AggregateException))
  39.                     {
  40.                         ex = ((AggregateException)ex).Flatten().InnerException;
  41.                     }
  42.                     errorDetail.Append(ToLogString(ex: ex));
  43.                     ex = ex.InnerException;
  44.                 }
  45.             }
  46.             else
  47.             {
  48.                 errorDetail.Append(ToLogString(new Exception()));
  49.             }
  50.  
  51.             if (useHtmlNewline)
  52.                 errorDetail.Replace(Environment.NewLine, "<br />");
  53.  
  54.             return errorDetail.ToString();
  55.         }
  56.  
  57.         /// <summary>
  58.         /// <para>Creates a log-string from the Exception.</para>
  59.         /// <para>The result includes the stacktrace, innerexception et cetera, separated by <seealso cref="Environment.NewLine"/>.</para>
  60.         /// </summary>
  61.         /// <param name="ex">The exception to create the string from.</param>
  62.         /// <param name="additionalMessage">Additional message to place at the top of the string, maybe be empty or null.</param>
  63.         /// <returns></returns>
  64.         private static string ToLogString(Exception ex, string additionalMessage = "")
  65.         {
  66.             StringBuilder msg = new StringBuilder();
  67.  
  68.             if (!string.IsNullOrEmpty(additionalMessage))
  69.             {
  70.                 msg.Append(additionalMessage);
  71.                 msg.Append(Environment.NewLine);
  72.             }
  73.  
  74.             if (ex != null)
  75.             {
  76.                 try
  77.                 {
  78.                     Exception orgEx = ex;
  79.  
  80.                     msg.Append("[Excpetion]: (");
  81.                     msg.Append(ex.GetType().ToString());
  82.                     msg.Append(") " + ex.Message);
  83.                     msg.Append(Environment.NewLine);
  84.  
  85.                     if (ex.Source != null)
  86.                     {
  87.                         msg.Append("[Source]: ");
  88.                         msg.Append(ex.Source);
  89.                         msg.Append(Environment.NewLine);
  90.                     }
  91.  
  92.                     if (ex.TargetSite != null)
  93.                     {
  94.                         msg.Append("[TargetSite]: ");
  95.                         msg.Append(ex.TargetSite.ToString());
  96.                         msg.Append(Environment.NewLine);
  97.                     }
  98.  
  99.                     //if (ex.GetType() == typeof(WebApiException)) {
  100.                     //    WebApiException apiEx = (WebApiException)ex;
  101.                     //    msg.Append("[WebApiInfo]");
  102.                     //    msg.Append(Environment.NewLine);
  103.                     //    msg.AppendLine("   BaseAddress: " + apiEx.ConnectionInfo.BaseAddress.ToString());
  104.                     //    msg.AppendLine("   ApiPath: " + apiEx.ApiPath.ToSafeString());
  105.                     //    msg.AppendLine("   Username: " + apiEx.ConnectionInfo.Username.ToSafeString());
  106.                     //    if (apiEx.ResponseMessage != null) {
  107.                     //        msg.AppendLine("   ResponseCode: " + apiEx.ResponseMessage.StatusCode.ValueString() + " (" + apiEx.ResponseMessage.ReasonPhrase + ")");
  108.                     //    }
  109.  
  110.                     //    if (apiEx.ApiParameters != null) {
  111.                     //        msg.AppendLine("   ApiParameters: " + apiEx.ApiParameters.Explode());
  112.                     //   }
  113.                     //}
  114.  
  115.                     if (ex.Data != null)
  116.                     {
  117.                         foreach (DictionaryEntry entry in ex.Data)
  118.                         {
  119.                             msg.Append("[Data]: ");
  120.                             msg.Append(entry.Key.ToString());
  121.                             msg.Append(" = ");
  122.                             if (entry.Value != null)
  123.                             {
  124.                                 msg.Append(entry.Value.ToString());
  125.                             }
  126.                             else
  127.                             {
  128.                                 msg.Append("<null>");
  129.                             }
  130.                             msg.Append(Environment.NewLine);
  131.                         }
  132.                     }
  133.  
  134.                     if (ex.StackTrace != null)
  135.                     {
  136.                         msg.Append("[StackTrace]: ");
  137.                         msg.Append(Environment.NewLine);
  138.                         msg.Append(ex.StackTrace.ToString());
  139.                         msg.Append(Environment.NewLine);
  140.                     }
  141.  
  142.                     //                    Exception baseException = ex.GetBaseException();
  143.                     //                    if (baseException != null && ex.GetBaseException() != ex) {
  144.                     //                        msg.Append("[BaseException]: ");
  145.                     //                        msg.Append(Environment.NewLine);
  146.                     //                        msg.Append(ex.GetBaseException());
  147.                     //                    }
  148.                 }
  149.                 finally
  150.                 {
  151.                 }
  152.             }
  153.             return msg.ToString();
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement