DTLib.Demystifier/StringBuilderExtentions.cs
2024-09-20 02:23:38 +05:00

36 lines
1.2 KiB
C#

// Copyright (c) Ben A Adams. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using DTLib.Demystifier.Enumerable;
namespace DTLib.Demystifier;
public static class StringBuilderExtentions
{
public static StringBuilder AppendDemystified(this StringBuilder builder, Exception exception)
{
var stackTrace = new EnhancedStackTrace(exception);
builder.Append(exception.GetType());
if (!string.IsNullOrEmpty(exception.Message)) builder.Append(": ").Append(exception.Message);
builder.Append(Environment.NewLine);
stackTrace.AppendTo(builder);
if (exception is AggregateException aggEx)
foreach (var ex in EnumerableIList.Create(aggEx.InnerExceptions))
builder.AppendInnerException(ex);
if (exception.InnerException is not null) builder.AppendInnerException(exception.InnerException);
return builder;
}
private static void AppendInnerException(this StringBuilder builder, Exception exception)
{
builder.Append(" ---> ")
.AppendDemystified(exception)
.AppendLine()
.Append(" --- End of inner exception stack trace ---");
}
}