简体   繁体   English

确定在ASP.NET Core网站上回答的中间件

[英]Determine which middleware answered on ASP.NET Core websites

Is there a way to determine (on way back) which middleware answered the request? 有没有办法确定(回来)哪个中间件回答了请求? Is there a stacktrace somewhere or something similar? 是否有某个堆栈跟踪或类似的东西?

What I seek could be a stacktrace up to the last middleware called, accessible in the httpcontext object. 我所寻求的可能是一个堆栈跟踪,直到被调用的最后一个中间件,可以在httpcontext对象中访问。

I could add some fake middlewares just setting flags, but I would prefer to have something that does not need adding code. 我可以添加一些假的中间件只是设置标志,但我更喜欢有一些不需要添加代码的东西。

It would be for example to differenciate requests that have been served by static files and those served by MVC (this is an example, but not exhaustive, I have several middlewares serving response, and I would like to be able to identify which one). 例如,区分静态文件和MVC服务的请求(这是一个例子,但并非详尽无遗,我有几个中间件提供响应,我希望能够识别哪一个)。

ASP.NET Core Logging API already provide what you need. ASP.NET Core Logging API已经提供了您所需的功能。 You can obtain the following output : 您可以获得以下输出:

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] info:Microsoft.AspNetCore.Hosting.Internal.WebHost [1]
Request starting HTTP/1.1 GET http://localhost:5000/api/todo/0 请求启动HTTP / 1.1 GET http:// localhost:5000 / api / todo / 0

info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1] Executing action method TodoApi.Controllers.TodoController.GetById (TodoApi) with arguments (0) - ModelState is Valid info:Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker [1]使用参数(0)执行操作方法TodoApi.Controllers.TodoController.GetById(TodoApi) - ModelState有效

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 GET https://localhost:44301/css/bootstrap.css info:Microsoft.AspNetCore.Hosting.Internal.WebHost [1]请求启动HTTP / 1.1 GET https:// localhost:44301 / css / bootstrap.css

info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2] Sending file. info:Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware [2]发送文件。 Request path: '/css/bootstrap.css'. 请求路径:'/ css / bootstrap.css'。 Physical path: XXX 物理路径:XXX

The simpliest way of achieving this would be to create an appsettings.Development.json file where you can set the following configuration : 实现此目的的最简单方法是创建一个appsettings.Development.json文件,您可以在其中设置以下配置:

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    },
    "Console":
    {
      "IncludeScopes": true
    }
  }
}

By default, you will be able to see the logs in the console. 默认情况下,您将能够在控制台中查看日志。 You could change where the logs are displayed by using differents ILoggerProvider . 您可以使用不同的ILoggerProvider更改日志的显示ILoggerProvider

I recommend you to read the Logging chapter on MSDN 我建议您阅读MSDN上日志记录章节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM