2017-11-27 68 views
0

kestrelのホスティングでクライアントのipsを記録する方法はありますか?私はアプリの設定でDEBUGログレベルを指定しましたが、依然として、誰からではなく、要求されているサーバーのパスだけを示しています。要求ごとにクライアントのIPを記録する - kestrelのアクセスログ

私はクライアントのipが各api内でHttpContextの構造体から取得できることは知っていますが、私が望むのは、フレームワークによってログに記録して、各APIに同じ機能を追加することができます。

dbug: Microsoft.AspNetCore.Server.Kestrel[1] 
     Connection id "0HL9L7IJ7JLIV" started. 
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] 
     Request starting HTTP/1.1 GET http://192.168.1.110:2180/ <====== it logs the server path 
dbug: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[9] 
     AuthenticationScheme: Bearer was not authenticated. 
dbug: Microsoft.AspNetCore.Routing.RouteBase[1] 
     Request successfully matched the route with name 'default' and template '{controller}/{action}'. 
dbug: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1] 
     Executing action CH.Api.Controllers.HomeController.Index (CH) 
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1] 
     Executing action method CH.Controllers.HomeController.Index (CH) with arguments ((null)) - ModelState is Valid 
dbug: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2] 
     ... 
dbug: Microsoft.AspNetCore.Server.Kestrel[9] 
     Connection id "0HL9L7IJ7JLIV" completed keep alive response. 
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] 
     Request finished in 160.2879ms 200 text/plain; charset=utf-8 
dbug: Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv[6] 
     Connection id "0HL9L7IJ7JLIV" received FIN. 
dbug: Microsoft.AspNetCore.Server.Kestrel[10] 
     Connection id "0HL9L7IJ7JLIV" disconnecting. 
dbug: Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv[7] 
     Connection id "0HL9L7IJ7JLIV" sending FIN. 
dbug: Microsoft.AspNetCore.Server.Kestrel[2] 
     Connection id "0HL9L7IJ7JLIV" stopped. 

答えて

1

たぶん、あなただけの4線で各要求をログに記録するカスタムミドルウェアを作成することができます

app.Use(async (context, next) => 
{ 
    Console.WriteLine($"[{context.Connection.RemoteIpAddress}] | {context.Request.Path}"); 
    await next.Invoke(); 
}); 

出力:

[127.0.0.1] |/
[127.0.0.1] | /About 
[127.0.0.1] | /Post 

または完全なURLを参照してくださいcontext.Request.GetDisplayUrl()context.Request.Pathを置き換える:

[127.0.0.1] | http://127.0.0.1:5000/Post?id=1 
+0

設定ファイルでそれを有効にするだけの方法がない場合、私はミドルウェアのパスに行きます。私は、nginxのように、あなたは設定ファイルによってaccess.logを行うことができます。 – fluter

関連する問題