2017-05-03 5 views
4

リクエストが失敗したときにHTTPリクエスト本文を記録する最も良い方法は何ですか?Application Insightで失敗した要求に対して要求本文を記録するベストプラクティスは何ですか?

私は例外ロガーオーバーライドすることで、未処理の例外をログに記録しています:上記のコードで

public class AiExceptionLogger : ExceptionLogger 
{ 
    public override void Log(ExceptionLoggerContext context) 
    { 
     if (context != null && context.Exception != null) 
     { 
      ExceptionTelemetry telemetry = new ExceptionTelemetry(context.Exception); 

      // the requestBody is always empty because the stream is non-rewinadable? 
      string requestBody = context.Request.Content.ReadAsStringAsync().Result; 
      telemetry.Properties.Add("Request Body", requestBody); 

      Logger.LogException(telemetry); 
     } 
     base.Log(context); 
    } 
} 

を、要求内容は常に空です。私もthisを試しましたが、GetBufferlessInputStreamを呼び出すため、サポートされていないメソッドの例外がスローされます。だからそれはどちらもうまくいかない。

DelegatingHandlerを使用してすべてのリクエストコンテンツをログに記録できましたが、処理されない例外によって発生したリクエストのリクエストのみを記録したいと思います。

アイデア?

答えて

2

、要求内容は常に空です。

ReadAsStreamAsyncメソッドを使用してリクエストストリームを取得し、このストリームの位置をリセットすることができます。その後、StreamReaderを使用してこの蒸気からコンテンツを読み取ることができます。下記のコードは参照用です。私はそれをテストし、それは私の側で正常に働いた。

ExceptionTelemetry telemetry = new ExceptionTelemetry(context.Exception); 

//Get request stream and reset the position of this stream 
Stream requestBodyStream = context.Request.Content.ReadAsStreamAsync().Result; 
requestBodyStream.Position = 0; 
string requestBody = string.Empty; 
using (StreamReader sr = new StreamReader(requestBodyStream)) 
{ 
    requestBody = sr.ReadToEnd(); 
} 
telemetry.Properties.Add("Request Body", requestBody); 
+0

私はMitch StewartのCopyToアプローチを使用してもあなたの答えを受け入れました。しかし、彼のコードが不完全で修正が必要なので、正しい答えとしてマークすることはできませんでした。 –

+0

@ErtayShashkoこれはうまくいきませんなぜこれが受け入れられた答えなのか分かりません。あなたは位置をリセットすることはできません。 – Phill

+0

@Phill、ご使用の環境でrequestBodyStreamの位置をリセットできない場合は、MemoryStreamにコピーしてMemoryStreamの位置をリセットします。その後、MemoryStreamからデータを読み取ることができます。 MemoryStream ms =新しいMemoryStream(); requestBodyStream.CopyTo(ms); ms.Position = 0; – Amor

0

あなたは正しいです。 Streamsの場合と同じように、Positionプロパティをリセットすることはできません。代わりに、コンテンツをコピーしてコピーを読んでください。上記のコードで

if (context != null && context.Exception != null) 
{ 
    HttpContent requestContent = new HttpContent(); 
    request.Content.CopyToAsync(requestContent); 
    ExceptionTelemetry telemetry = new ExceptionTelemetry(context.Exception); 

    // the requestBody is always empty because the stream is non-rewinadable? 
    string requestBody = requestContent.ReadAsStringAsync().Result; 
    telemetry.Properties.Add("Request Body", requestBody); 

    Logger.LogException(context.Exception); 
} 
+0

HttpContentを使用しMSFTの答えは抽象クラスであり、また、CopyToのは、ストリームを取り込んで:)あなたは、あなたの答えを編集する必要があります。私はそれが動作するかどうかを確認しようとします。 –

0

ここアモールの代替だ - CopyTo

public class AiExceptionLogger : ExceptionLogger 
{ 
    public override async void Log(ExceptionLoggerContext context) 
    { 
     if (context != null && context.Exception != null) 
     { 
      ExceptionTelemetry telemetry = new ExceptionTelemetry(context.Exception); 

      using (var ms = new MemoryStream()) 
      { 
       await context.Request.Content.CopyToAsync(ms); 
       var requestBody = Encoding.UTF8.GetString(ms.ToArray()); 
       telemetry.Properties.Add("Request Body", requestBody); 
      } 


      Logger.LogException(telemetry); 
     } 
     base.Log(context); 
    } 
} 
関連する問題