zoukankan      html  css  js  c++  java
  • C#异常信息格式化

    private static string ExceptionFormat(Exception ex, string customMessage = "")
            {
                var sb = new StringBuilder();
                sb.AppendLine($"【错误信息】{customMessage}:{ex.Message}");
                sb.AppendLine($"【堆栈跟踪】:{ex.StackTrace}");
    
                if (ex.InnerException != null)
                {
                    sb.AppendLine($"【内部异常】:{ex.InnerException.Message}");
                    sb.AppendLine($"【内部异常堆栈跟踪】:{ex.InnerException.StackTrace}");
                }
    
                if (ex is AggregateException)
                {
                    var exs = (ex as AggregateException).InnerExceptions;
                    for (int i = 0; i < exs.Count; i++)
                    {
                        var item = exs[i];
    
                        sb.AppendLine($"【第{i + 1}个错误信息】{item.Message}");
    
                        if (ex.InnerException != null)
                        {
                            sb.AppendLine($"{item.InnerException.Message}");
                            if (item.InnerException.InnerException != null)
                            {
                                sb.AppendLine($"{item.InnerException.InnerException.Message}");
                            }
                        }
    
                        sb.AppendLine($"【第{i + 1}个错误堆栈跟踪】:{item.StackTrace}");
    
                        if (ex.InnerException != null)
                        {
                            sb.AppendLine($"{item.InnerException.StackTrace}");
                            if (item.InnerException.InnerException != null)
                            {
                                sb.AppendLine($"{item.InnerException.InnerException.StackTrace}");
                            }
                        }
                    }
                }
    
                return sb.ToString();
            }

    结果:

    Newd

    版权声明

    作者:扶我起来我还要敲

    地址:https://www.cnblogs.com/Newd/p/13099373.html

    © Newd 尊重知识产权,引用请注出处

    广告位

    (虚位以待,如有需要请私信)

  • 相关阅读:
    洛谷P5281 [ZJOI2019] Minimax搜索
    势函数
    Comet OJ [Contest #5] 迫真大游戏
    洛谷P3307 [SDOI2013] 项链
    洛谷P5985 [PA2019] Muzyka pop
    CF1205E Expected Value Again
    review
    CF891E Lust
    线性代数
    洛谷P4607 [SDOI2018] 反回文串
  • 原文地址:https://www.cnblogs.com/Newd/p/13099373.html
Copyright © 2011-2022 走看看