.NET:如何将异常转换为字符串? [英] .NET: How to convert Exception to string?

查看:102
本文介绍了.NET:如何将异常转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当抛出异常(在IDE中进行调试时),我有机会查看详细信息

When an exception is thrown (while debugging in the IDE), i have the opportunity to view details of the exception:

< img src =https://i.stack.imgur.com/NNcx3.pngalt =在此输入图像描述>

但是在代码中如果我打电话 exception.ToString()我没有看到这些有用的细节:

But in code if i call exception.ToString() i do not get to see those useful details:

System.Data.SqlClient.SqlException (0x80131904): Could not find stored procedure 'FetchActiveUsers'.
  [...snip stack trace...]

但Visual Studio有一些魔法它可以将异常复制到剪贴板:

But Visual Studio has some magic where it can copy the exception to the clipboard:

其中提供了有用的详细信息:

Which gives the useful details:

System.Data.SqlClient.SqlException was unhandled by user code
  Message=Could not find stored procedure 'FetchActiveUsers'.
  Source=.Net SqlClient Data Provider
  ErrorCode=-2146232060
  Class=16
  LineNumber=1
  Number=2812
  Procedure=""
  Server=vader
  State=62
  StackTrace:
       [...snip stack trace...]
  InnerException:

我想要这个!

什么是

String ExceptionToString(Exception ex)
{ 
    //todo: Write useful routine
    return ex.ToString();
}

可以完成相同的魔法。有没有一个.NET功能建在某个地方?

that can accomplish the same magic. Is there a .NET function built in somewhere? Does Exception have a secret method somewhere to convert it to a string?

推荐答案

p> ErrorCode 特定于 ExternalException ,而不是异常 LineNumber 号码特定于 SqlException ,而不是code>异常。因此,从 Exception 中的通用扩展方法获取这些属性的唯一方法是使用反射来遍历所有公共属性。

ErrorCode is specific to ExternalException, not Exception and LineNumber and Number are specific to SqlException, not Exception. Therefore, the only way to get these properties from a general extension method on Exception is to use reflection to iterate over all of the public properties.

所以你必须这样说:

public static string GetExceptionDetails(this Exception exception) {
    var properties = exception.GetType()
                            .GetProperties();
    var fields = properties
                     .Select(property => new { 
                         Name = property.Name,
                         Value = property.GetValue(exception, null)
                     })
                     .Select(x => String.Format(
                         "{0} = {1}",
                         x.Name,
                         x.Value != null ? x.Value.ToString() : String.Empty
                     ));
    return String.Join("\n", fields);
}

(未对不符合问题进行测试。)

(Not tested for compliation issues.)

.NET 2.0兼容答案:

.NET 2.0 compatible answer:

public static string GetExceptionDetails(this Exception exception) 
{
    PropertyInfo[] properties = exception.GetType()
                            .GetProperties();
    List<string> fields = new List<string>();
    foreach(PropertyInfo property in properties) {
        object value = property.GetValue(exception, null);
        fields.Add(String.Format(
                         "{0} = {1}",
                         property.Name,
                         value != null ? value.ToString() : String.Empty
        ));    
    }         
    return String.Join("\n", fields.ToArray());
}

这篇关于.NET:如何将异常转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆