2016-10-26 5 views
0

アプリケーション内でhttpRequestを傍受して(プロキシを必要としないように)、ログのリクエストとレスポンスの詳細を記録できるクラスを作成する必要があります。 HttpRequestを作成するには、HttpClient、HttpWebRequest、RestSharpなどのHelperライブラリなどの任意の方法で作成できます。つまり、HttpRequestが初期化されたときに発生するイベントを含む動的クラスを作成することができます。私の.Netアプリケーション内のhttpRequestとレスポンスを傍受してください。

私はすでにネットワークのトレースを使用しますが、私はログインすることができ、すべてがそうそこには、要求と応答を傍受する安全な方法は、私はあなたがasp.net Webフォームを使用していると仮定しています代わりに、文字列に

public class DebugTraceListener : System.Diagnostics.TextWriterTraceListener 
{ 
    public override void Write(string message) 
    { 
     using (StreamWriter w = File.AppendText("D:\\Log\\log2.txt")) 
     { 
      Log(message, w); 
     } 
    } 

    public override void WriteLine(string message) 
    { 
     using (StreamWriter w = File.AppendText("D:\\Log\\log2.txt")) 
     { 
      Log(message, w); 
     } 
    } 

    public static void Log(string logMessage, TextWriter w) 
    { 
     w.WriteLine(" :{0}", logMessage); 
     w.WriteLine("-------------------------------"); 
    } 
} 
+0

"ネットワークトレース"とはどういう意味ですか? ''で ''を有効にすると、リクエストとレスポンスの両方が得られます。フォーマットについて:標準の.NETメカニズムを使用してログし、独自のリスナーを書くことができます(標準の 'TextWriterTraceListener'フォーマットが嫌いな場合)。もちろん、それらを解釈したい場合は、ログメッセージを理解する必要があります。 –

+0

私はこのhttps://msdn.microsoft.com/en-us/library/a6sbz1dx(v=vs.110).aspxを使用しました。私は独自のリスナーを書くと出力形式を制御できます。 –

+0

@AdrianoRepettiそして、web.config以外のコードを使ってすべてを行うことはできますか? –

答えて

0

を解析するオブジェクトです、文字列でありますまたはasp.net mvc。次は、以下の行を追加することで、あなたのweb.configファイルにHttpModuleを登録、

using System.Web; 


namespace WebApplication1.App_Start 
{ 
using System; 

public class RequestHandler : HttpApplication,IHttpModule 
{ 
    /// <summary> 
    /// Initializes a module and prepares it to handle requests. 
    /// </summary> 
    /// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application </param> 
    public void Init(HttpApplication context) 
    { 

     context.BeginRequest += context_BeginRequest; 
     context.EndRequest += context_RequestCompleted; 
    } 

    void context_RequestCompleted(object sender, EventArgs e) 
    { 
     var application = (HttpApplication)sender; 
     var context = application.Context; 

     var response = context.Response; 
     context.Response.Write(response.Charset); 
    } 

    void context_BeginRequest(object sender, EventArgs e) 
    { 
     var application = (HttpApplication)sender; 
     var context = application.Context; 

     var url = context.Request.Url; 
     context.Response.Write(url.AbsoluteUri); 
    } 

    /// <summary> 
    /// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>. 
    /// </summary> 
    public void Dispose() 
    { 
     throw new NotImplementedException(); 
    } 



} 
} 

を:あなたは、私は、次のモジュールを作成している

のIHttpModule

クラスのHttpApplicationを拡張し、インタフェースを実装するクラスを作成する必要があります下に:

<system.webServer> 
<modules> 
    <add name="Handler" type="WebApplication1.App_Start.RequestHandler"/> 
    <remove name="FormsAuthenticationModule" /> 
</modules> 
</system.webServer> 

あなたがリクエストするたびに、受信した応答のURLと文字セットが書き込まれます。

すると、あなたのロギング命令によって次の行を置き換える:あなたは、私が上記で定義されている応答URL変数の異なる属性をログに記録することができます

Logger.Log(url.AbsoluteUri) //in context_BeginRequest 
Logger.Log(response.ContentType) //in context_RequestCompleted 

:例えばのための

context.Response.Write(url.AbsoluteUri); 
context.Response.Write(response.Charset); 

希望する

+0

私はHelperクラスを作成する必要があります。これは、.Netアプリケーション呼び出しWebサービス(Web、Windows、Consoleなど)で使用できます。 –

関連する問題