如何进行此调用以记录我的异常递归?

时间:2012-05-24 04:33:55

标签: c# exception inner-exception

我有以下代码:

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

我是否有某种方法可以递归地执行此操作,或者将消息格式放在另一个函数中并调用它会更好吗?

6 个答案:

答案 0 :(得分:2)

尝试使用类似

的内容
Exception ex = e;
while (ex != null)
{
   string s = ex.Message;
   ex = ex.InnerException;
}

答案 1 :(得分:1)

这就是我要做的事情:

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();
}

答案 2 :(得分:1)

我会这样做:

        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>";
    }

答案 3 :(得分:1)

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

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是其中一个工具。

为了获得更好的效果,请考虑改为使用StringBuilder

答案 4 :(得分: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;    
}

答案 5 :(得分: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();

在您的情况下,传递您自己的例外......