1
Task
を使用してデータをロードしています。障害が発生した場合は、ユーザーに障害の根本原因を示したいと思います。AggregatedExceptionからルートを引き起こす例外を取得する方法
Task
の実行中に発生した例外は、AggregatedException
に集約されます。それで、ルートから例外を引き起こす最善の方法は何でしょうか?
私の現在のアプローチは、次のとおりです。
public static Exception GetRootCausingException(this AggregateException aggregatedException)
{
if (aggregatedException == null)
return null;
// get the AggregateException that is the root cause of this exception.
var exception = aggregatedException.GetBaseException();
while (exception.InnerException != null) exception = exception.InnerException;
return exception;
}
最も内側の例外は、多くの場合、意味がありません。外部のものは、しばしば情報を追加または解釈します。おそらく、aggexを取り除き、すべての例外(ToStringなど)を表示する必要があります。 – usr