flu*_*ter 3 asp.net kestrel-http-server asp.net-core
有没有办法用 Kestrel 托管来记录客户端 ip?我在应用程序设置中指定了 DEBUG 日志级别,但它仍然只显示正在请求的服务器路径,而不显示来自谁。
我知道客户端 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.
Run Code Online (Sandbox Code Playgroud)
也许您可以创建一个自定义中间件,仅用四行记录每个请求:
app.Use(async (context, next) =>
{
Console.WriteLine($"[{context.Connection.RemoteIpAddress}] | {context.Request.Path}");
await next.Invoke();
});
Run Code Online (Sandbox Code Playgroud)
输出:
[127.0.0.1] | /
[127.0.0.1] | /About
[127.0.0.1] | /Post
Run Code Online (Sandbox Code Playgroud)
或者替换context.Request.Path为context.Request.GetDisplayUrl()以查看完整的 URL:
[127.0.0.1] | http://127.0.0.1:5000/Post?id=1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4576 次 |
| 最近记录: |