2016-05-27 16 views
3

ASP.NET Core Web APIでロギング目的でHTTPリクエストを取得するのが難しいです。私は助けているここでは例ASP.Net Core Web APIロギング用HTTPリクエストの取得

http://dotnetliberty.com/index.php/2016/01/07/logging-asp-net-5-requests-using-middleware/

を見つけることができました。基本的には、ミドルウェア機能を使用してHTTPリクエストパイプラインに追加されるロギングクラスです。問題は、クラスメソッドがアプリケーションの起動時にのみ呼び出されることです。私はhttpリクエストを取得したり投稿したりするときに呼び出すことができません。私がデバッグすることができ、コンソールアプリケーション内でFiddlerとhttpクライアントを使って試した応答がOKであるため、httpリクエストの取得またはポストが機能しています。私が使用してHTTP要求にピギーバックところ、最後の行である。ここ

は私のロギングクラスは

using Microsoft.AspNetCore.Http; 
using Microsoft.Extensions.Logging; 
using System; 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.Linq; 
using System.Threading.Tasks; 


namespace CoreWebAPI.Models 
{ 
public class RequestLoggingMiddleware 
{ 
    private readonly RequestDelegate _next; 
    private readonly ILogger<RequestLoggingMiddleware> _logger; 


    public RequestLoggingMiddleware(RequestDelegate next, ILogger<RequestLoggingMiddleware> logger) 
    { 
     _next = next; 
     _logger = logger; 
    } 


    public async Task Invoke(HttpContext context) 
    { 
     var startTime = DateTime.UtcNow; 


     var watch = Stopwatch.StartNew(); 
     await _next.Invoke(context); 
     watch.Stop(); 


     var logTemplate = @" 
Client IP: {clientIP} 
Request path: {requestPath} 
Request content type: {requestContentType} 
Request content length: {requestContentLength} 
Start time: {startTime} 
Duration: {duration}"; 


     _logger.LogInformation(logTemplate, 
      context.Connection.RemoteIpAddress.ToString(), 
      context.Request.Path, 
      context.Request.ContentType, 
      context.Request.ContentLength, 
      startTime, 
      watch.ElapsedMilliseconds); 
    } 
} 
} 

されており、ここで私のStartup.Configure方法は、私のStartup.csクラスで

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
     loggerFactory.AddDebug(); 




     app.UseApplicationInsightsRequestTelemetry(); 


     app.UseApplicationInsightsExceptionTelemetry(); 


     app.UseMvc(); 
     app.UseCors(builder => builder.AllowAnyOrigin()); 


     app.UseMiddleware<RequestLoggingMiddleware>(); 

    } 

ですapp.UseMiddleware。私はこれがうまくいくはずだと思うが、なぜ誰かがそれがなぜそうでないか、あるいはそれが偉大になる代わりのものであるかという手がかりを持っている。追加情報を提供する必要がある場合はお知らせください。

+0

の初めにミドルウェアを追加する必要がスタートアップで

public async Task Invoke(HttpContext context) { var timer = new Stopwatch(); timer.Start(); await _next(context); //Need to stop the timer after the pipeline finish timer.Stop(); _logger.LogDebug("The request took '{0}' ms", timerer.ElapsedMilliseconds); } 

-for-asp-net-core / – IbrarMumtaz

答えて

4

ミドルウェアは、登録された順序でパイプラインの一種で呼び出され、次のものを呼び出すかどうかを決定します(独自のログミドルウェア:await _next.Invoke(context);参照)。

Mvcフレームワークによって処理されるリクエストは、次のミドルウェアに渡されません。

すべての要求をログに記録するには、Mvcなどのパイプラインを終了するミドルウェアの前にログの登録を行う必要があります。

2

これは正常に動作します。 https://blog.getseq.net/smart-logging-middleware:あなたはこのブログには何が必要カバーパイプライン

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
    loggerFactory.AddDebug(); 


    app.UseMiddleware<RequestLoggingMiddleware>(); 

    // rest of the middlewares 
     .................. 

    app.UseMvc(); 





} 
関連する問題