2016-04-03 12 views
1

これはおそらく奇妙に聞こえるかもしれませんが、例外に関するデバッグ情報がMVC Web APIから送信されている場所を特定しようとしています。MVC Web API例外ハンドラOWIN/Kotanaを使用したJSONデバッグ情報

  • メッセージ

  • ExceptionMessage

  • ExceptionType

  • のStackTrace:例外が発生するたびに、それは次の属性を持つオブジェクトを(私はXMLフォーマッタを取り外すためJSONフォーマット)を返します

私は、私は例外が発生したときに新しいハンドラは、(デバッグブレークポイントを経由して)呼び出されていることを確認することができるよにもかかわらず、しかしWebApiConfig.Register()

config.Services.Replace(typeof(IExceptionHandler), new CustomExceptionHandler()); 

に次の行でIExceptionHandlerの既存の実装を置き換えますエラーに関するデバッグ情報を持つ同じJSONオブジェクトが返されます。

私の質問は次のとおりです。この情報の生成と送信を担当するストックシステムまたはメカニズムはありますか?

答えて

0

exceptionHandlerの

Handle方法でExceptionHandlerContextクラスが未処理の例外処理が

ExceptionHandlerContext.Result

を発生し、その中のコンテキストを表し Global Error Handling in ASP.NET Web API 2

を見てみましょう

例外が処理されたときに応答メッセージを提供する結果を取得または設定します。

カスタムエラーメッセージ例外ハンドラ

以下の次は、サポートに連絡するための電子メールアドレスなどのクライアントへのカスタムエラー応答を生成します。

class OopsExceptionHandler : ExceptionHandler 
{ 
    public override void HandleCore(ExceptionHandlerContext context) 
    { 
     context.Result = new TextPlainErrorResult 
     { 
      Request = context.ExceptionContext.Request, 
      Content = "Oops! Sorry! Something went wrong." + 
         "Please contact [email protected] so we can try to fix it." 
     }; 
    } 

    private class TextPlainErrorResult : IHttpActionResult 
    { 
     public HttpRequestMessage Request { get; set; } 

     public string Content { get; set; } 

     public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) 
     { 
      HttpResponseMessage response = 
          new HttpResponseMessage(HttpStatusCode.InternalServerError); 
      response.Content = new StringContent(Content); 
      response.RequestMessage = Request; 
      return Task.FromResult(response); 
     } 
    } 
} 

あなたが望む任意の応答本体を作成するためのWeb APIを拡張するためにIHttpActionResultを使用することができます。

関連する問題