3

我有以下代码:

protected string formatException(Exception e)
{
    var exError = "<form>";
        if (e == null)
        {
            throw new ArgumentNullException("e");
        }
        exError += "<fieldset><legend><a href='#'>" +
                  "<span class='show-expanded'>collapse message</span>" +
                  "<span class='show-collapsed'>expand message</span>" +
                  "</a></legend><p>" + e.Message + "</p></fieldset>";
        exError += "<fieldset><legend><a href='#'>" +
                  "<span class='show-expanded'>collapse trace</span>" +
                  "<span class='show-collapsed'>expand trace</span>" +
                  "</a></legend><p>" + e.StackTrace + "</p></fieldset>";

        if (e.InnerException != null)
        {
            // same functionality but for the inner exception and the InnerException.InnerException
        }
    return exError + "</form>";
}

调用时,它会格式化异常消息。但是我想让它包括InnerExceptionInnerException.InnerException

有什么方法可以递归地执行此操作,还是将消息格式放在另一个函数中并调用它会更好?

4

6 回答 6

5

我知道这是一个老问题,并且已经有一个明确的答案。但是,我就是这样做的,我会在这里发布它以防它帮助某人:

public static class ExceptionExtension
{
    public static string GetFullTrace(this Exception ex, bool recursive = true)
    {
        string trace = "";

        trace += "Name: " + ex.GetType().Name + "\n";
        trace += "Message: " + ex.Message + "\n";
        trace += "Stack Trace: " + (ex.StackTrace ?? "null") + "\n";

        if (recursive)
        {
            while (ex.InnerException != null)
            {
                ex = ex.InnerException;

                trace += "\n-------------------- Caused by: --------------------\n";
                trace += "Name: " + ex.GetType().Name + "\n";
                trace += "Message: " + ex.Message + "\n";
                trace += "Stack Trace: " + (ex.StackTrace ?? "null") + "\n";
            }
        }
        return trace;
    }
}

与以前的答案略有不同的是使用扩展方法。这样,人们可以简单地将其称为:

try 
{
    SomeOperationWhichMayThrow();
}
catch(Exception ex) 
{
    LogError(ex.GetFullTrace());
}

我有一个项目,其中包含许多可重用的实用程序和扩展方法,我的大部分项目中都包含这些。ExceptionExtension是这些实用程序之一。

为了获得更好的性能,请考虑使用 aStringBuilder代替。

于 2016-06-22T15:22:16.330 回答
3

尝试使用类似的东西

Exception ex = e;
while (ex != null)
{
   string s = ex.Message;
   ex = ex.InnerException;
}
于 2012-05-24T04:40:07.280 回答
2

这是我要做的:

protected string formatException(Exception e)
{
    Func<string, string> createFieldSet =
        t =>
            "<fieldset><legend><a href='#'>" +
            "<span class='show-expanded'>collapse message</span>" +
            "<span class='show-collapsed'>expand message</span>" +
            "</a></legend><p>" + t + "</p></fieldset>";

    var exError = new StringBuilder("<form>");
    if (e == null)
    {
        throw new ArgumentNullException("e");
    }
    while (e != null)
    {
        exError.AppendLine(createFieldSet(e.Message));
        exError.AppendLine(createFieldSet(e.StackTrace));
        e = e.InnerException;
    }
    exError.AppendLine("</form>");
    return exError.ToString();
}
于 2012-05-24T04:53:00.020 回答
2

我会这样做:

        private string privateFormatException(Exception e)
        {
            var exError = String.Empty;
            if (e == null)
            {
                return exError;
            }
            exError += "<fieldset><legend><a href='#'>" +
                          "<span class='show-expanded'>collapse message</span>" +
                          "<span class='show-collapsed'>expand message</span>" +
                          "</a></legend><p>" + e.Message + "</p></fieldset>";
            exError += "<fieldset><legend><a href='#'>" +
                          "<span class='show-expanded'>collapse trace</span>" +
                          "<span class='show-collapsed'>expand trace</span>" +
                          "</a></legend><p>" + e.StackTrace + "</p></fieldset>";

            return exError + privateFormatException(e.InnerException);
        }



    protected string formatException(Exception e)
    {
        var exError = "<form>";
        if (e == null)
        {
            throw new ArgumentNullException("e");
        }
        exError += privateFormatException(e);
        return exError + "</form>";
    }
于 2012-05-24T04:55:18.453 回答
0

我不确定下面的代码是否可以解决它。它可能需要某种格式。

protected string formatException(Exception e)
{
    var exError = "<form>";
        if (e == null)
        {
            throw new ArgumentNullException("e");
        }
        exError += "<fieldset><legend><a href='#'>" +
                  "<span class='show-expanded'>collapse message</span>" +
                  "<span class='show-collapsed'>expand message</span>" +
                  "</a></legend><p>" + e.Message + "</p></fieldset>";
        exError += "<fieldset><legend><a href='#'>" +
                  "<span class='show-expanded'>collapse trace</span>" +
                  "<span class='show-collapsed'>expand trace</span>" +
                  "</a></legend><p>" + e.StackTrace + "</p></fieldset>";

        if (e.InnerException != null)
        {
            exError += formatException(e.InnerException);
        }
    return exError + "</form>";

}

编辑

protected string FormatException(Exception e)
{
    if (e == null)
    {
        throw new ArgumentNullException("e");
    }
    var exError = "<form>";
    exError += FormatExceptionInternal(e);
    return exError + "</form>";
}

private string FormatExceptionInternal(Exception e)
{
    var exError = "";        
    exError += "<fieldset><legend><a href='#'>" +
              "<span class='show-expanded'>collapse message</span>" +
              "<span class='show-collapsed'>expand message</span>" +
              "</a></legend><p>" + e.Message + "</p></fieldset>";
    exError += "<fieldset><legend><a href='#'>" +
              "<span class='show-expanded'>collapse trace</span>" +
              "<span class='show-collapsed'>expand trace</span>" +
              "</a></legend><p>" + e.StackTrace + "</p></fieldset>";

    if (e.InnerException != null)
    {
        exError += FormatExceptionInternal(e.InnerException);
    }
    return exError;    
}
于 2012-05-24T04:42:20.180 回答
0

您的FormatException方法应该是这样的,不需要将 exError 变量声明为类级别,在函数中声明它并返回..

        protected string formatException(Exception e)
        {
            var exError = string.Empty;
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }
            exError += "<fieldset><legend><a href='#'>" +
                      "<span class='show-expanded'>collapse message</span>" +
                      "<span class='show-collapsed'>expand message</span>" +
                      "</a></legend><p>" + e.Message + "</p></fieldset>";
            exError += "<fieldset><legend><a href='#'>" +
                      "<span class='show-expanded'>collapse trace</span>" +
                      "<span class='show-collapsed'>expand trace</span>" +
                      "</a></legend><p>" + e.StackTrace + "</p></fieldset>";

            if (e.InnerException != null)
            {
                exError += formatException(e.InnerException);
            }
            return exError;

        }

你可以这样调用这个函数,我希望它也可以用于递归。使用上述方法是

   StringBuilder sb = new StringBuilder();
   sb.Append("<form>");
   sb.Append(formatException(new Exception()));// pass your own exception
   sb.Append("</form>");
   string strException = sb.ToString();

在你的情况下,通过你自己的例外......

于 2012-05-24T04:59:13.480 回答