Wil*_*lem 147 c# exception inner-exception
什么是展示我的全部的正确方法InnerException.
我发现我的一些InnerExceptions有另一个InnerException,而且非常深入.
会InnerException.ToString()做的工作,我还是我通过需要循环InnerExceptions,建立一个String有StringBuilder?
Jon*_*Jon 221
您可以简单地打印exception.ToString()- 这也将包括所有嵌套InnerExceptions的全文.
Rob*_* P. 44
只是用 exception.ToString()
http://msdn.microsoft.com/en-us/library/system.exception.tostring.aspx
ToString的默认实现获取抛出当前异常的类的名称,消息,在内部异常上调用ToString的结果,以及调用Environment.StackTrace的结果.如果这些成员中的任何一个为null,则其值不包含在返回的字符串中.
如果没有错误消息或者它是空字符串(""),则不返回任何错误消息.仅当内部异常和堆栈跟踪不为空时,才会返回它们的名称.
exception.ToString()也会在该异常的内部异常上调用.ToString(),依此类推......
adr*_*anm 41
我通常喜欢这样做以消除大部分噪音:
void LogException(Exception error) {
Exception realerror = error;
while (realerror.InnerException != null)
realerror = realerror.InnerException;
Console.WriteLine(realerror.ToString())
}
Run Code Online (Sandbox Code Playgroud)
编辑:我忘记了这个答案而且很惊讶没有人指出你可以做到
void LogException(Exception error) {
Console.WriteLine(error.GetBaseException().ToString())
}
Run Code Online (Sandbox Code Playgroud)
小智 34
当你需要完整的细节(所有的消息和堆栈跟踪)和推荐的细节时,@ Jon的答案是最好的解决方案.
但是,可能存在您只想要内部消息的情况,对于这些情况,我使用以下扩展方法:
public static class ExceptionExtensions
{
public static string GetFullMessage(this Exception ex)
{
return ex.InnerException == null
? ex.Message
: ex.Message + " --> " + ex.InnerException.GetFullMessage();
}
}
Run Code Online (Sandbox Code Playgroud)
当我有不同的用于跟踪和记录的侦听器并希望对它们有不同的视图时,我经常使用此方法.这样我就可以有一个监听器,它通过电子邮件将整个错误与堆栈跟踪一起发送给开发团队,以便使用该.ToString()方法进行调试,并使用该方法编写一个日志文件,其中包含每天发生的所有错误的历史记录而没有堆栈跟踪的.GetFullMessage()方法.
要仅打印Message深度异常的一部分,您可以执行以下操作:
public static string ToFormattedString(this Exception exception)
{
IEnumerable<string> messages = exception
.GetAllExceptions()
.Where(e => !String.IsNullOrWhiteSpace(e.Message))
.Select(e => e.Message.Trim());
string flattened = String.Join(Environment.NewLine, messages); // <-- the separator here
return flattened;
}
public static IEnumerable<Exception> GetAllExceptions(this Exception exception)
{
yield return exception;
if (exception is AggregateException aggrEx)
{
foreach (Exception innerEx in aggrEx.InnerExceptions.SelectMany(e => e.GetAllExceptions()))
{
yield return innerEx;
}
}
else if (exception.InnerException != null)
{
foreach (Exception innerEx in exception.InnerException.GetAllExceptions())
{
yield return innerEx;
}
}
}
Run Code Online (Sandbox Code Playgroud)
递归地遍历所有内部异常(包括AggregateExceptions 的情况)以打印Message包含在其中的所有属性,并以换行符分隔。
例如
var outerAggrEx = new AggregateException(
"Outer aggr ex occurred.",
new AggregateException("Inner aggr ex.", new FormatException("Number isn't in correct format.")),
new IOException("Unauthorized file access.", new SecurityException("Not administrator.")));
Console.WriteLine(outerAggrEx.ToFormattedString());
Run Code Online (Sandbox Code Playgroud)
发生外部aggr ex。
内部aggr前。
数字格式不正确。
未经授权的文件访问。
不是管理员。
您将需要收听其他Exception属性以获取更多详细信息。例如Data将有一些信息。您可以这样做:
foreach (DictionaryEntry kvp in exception.Data)
Run Code Online (Sandbox Code Playgroud)
要获取所有派生的属性(不在基Exception类上),可以执行以下操作:
exception
.GetType()
.GetProperties()
.Where(p => p.CanRead)
.Where(p => p.GetMethod.GetBaseDefinition().DeclaringType != typeof(Exception));
Run Code Online (Sandbox Code Playgroud)
如果您使用实体框架,exception.ToString()将不会向您提供DbEntityValidationException异常的详细信息。您可能希望使用相同的方法来处理所有异常,例如:
catch (Exception ex)
{
Log.Error(GetExceptionDetails(ex));
}
Run Code Online (Sandbox Code Playgroud)
其中GetExceptionDetails包含这样的内容:
public static string GetExceptionDetails(Exception ex)
{
var stringBuilder = new StringBuilder();
while (ex != null)
{
switch (ex)
{
case DbEntityValidationException dbEx:
var errorMessages = dbEx.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage);
var fullErrorMessage = string.Join("; ", errorMessages);
var message = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
stringBuilder.Insert(0, dbEx.StackTrace);
stringBuilder.Insert(0, message);
break;
default:
stringBuilder.Insert(0, ex.StackTrace);
stringBuilder.Insert(0, ex.Message);
break;
}
ex = ex.InnerException;
}
return stringBuilder.ToString();
}
Run Code Online (Sandbox Code Playgroud)
我愿意:
namespace System {
public static class ExtensionMethods {
public static string FullMessage(this Exception ex) {
if (ex is AggregateException aex) return aex.InnerExceptions.Aggregate("[ ", (total, next) => $"{total}[{next.FullMessage()}] ") + "]";
var msg = ex.Message.Replace(", see inner exception.", "").Trim();
var innerMsg = ex.InnerException?.FullMessage();
if (innerMsg is object && innerMsg!=msg) msg = $"{msg} [ {innerMsg} ]";
return msg;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这“漂亮地打印”了所有内部异常,还处理了 AggregateExceptions 和 InnerException.Message 与 Message 相同的情况
| 归档时间: |
|
| 查看次数: |
79137 次 |
| 最近记录: |