English 中文(简体)
我怎样才能打电话来记录我的例外 重现?
原标题:How can I make this call to log my exceptions recursive?

我有以下代码:

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

当调用它来格式化例外消息时。 但我希望它包括 < code> innerexpeption 和 innerexpeption. innerexpeption

是否有办法我可以反复地这样做,或者最好把信息格式放在另一个函数中,称之为“信息格式”?

最佳回答

这里我要做的是:

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();
}
问题回答

我知道这是一个古老的问题,而且已经有一个明确的答案。然而,这是我如何做到的,我会把这个张贴在这里,以防它帮助别人:

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

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

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

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

与先前的回答相比,稍有变化的是使用扩展方法。 这样可以简单地将其称为:

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

我有一个包含许多可再利用的公用设施和推广方法的项目,我在我的大多数项目中都包含了这些方法。 ExpendionExtention 就是其中之一。

为了提高性能,请考虑使用 < code> StringBuilder 代替 < code>。

尝试使用类似

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

我愿意这样做:

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

我不知道接下来的代码能否解答它。 它可能需要某种格式 。

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

您的“ 强度” 格式例外 < / 强度” 方法应该如此, “ 强度” 不需要将Exeror 变量声明为等级级别 < / 强度”, 在函数中声明, 然后再返回 。

        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;

        }

You can call this function like that and i hope so it will work for recursive as well.. Using of above method is

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

万一你破例了...





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签