2016-06-01 7 views
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; 
} 
+1

最も内側の例外は、多くの場合、意味がありません。外部のものは、しばしば情報を追加または解釈します。おそらく、aggexを取り除き、すべての例外(ToStringなど)を表示する必要があります。 – usr

答えて

0

アンAggregatedExceptionは、一の以上の例外を表します。つまり、複数の例外を処理し、それぞれの根本的な問題を探す必要があるため、複数の「ルート例外」を持つことができます。

あなたは各例外にハンドラを呼び出すAggregatedExceptionのハンドル方法を使用してそれを行うことができます。

catch (AggregateException ae) { 
     ae.Handle((x) => 
     { 
      // look for the route cause of exception x 
     }); 
    } 

See the documentation

関連する問題