gpt4 book ai didi

c# - 确定哪些中间件在 ASP.NET Core 网站上响应

转载 作者:行者123 更新时间:2023-11-30 17:26:48 26 4
gpt4 key购买 nike

有没有办法确定(返回途中)哪个中间件响应了请求?某处是否有堆栈跟踪或类似的东西?

我寻求的可能是直到调用的最后一个中间件的堆栈跟踪,可在 httpcontext 对象中访问。

我可以添加一些假的中间件只是设置标志,但我更愿意有一些不需要添加代码的东西。

例如,区分由静态文件提供的请求和由 MVC 提供的请求(这是一个示例,但并不详尽,我有几个中间件提供响应,我希望能够确定哪个一个)。

最佳答案

ASP.NET Core Logging API 已经提供了你所需要的。您可以获得以下输出:

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting 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.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 GET https://localhost:44301/css/bootstrap.css

info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2] Sending file. Request path: '/css/bootstrap.css'. Physical path: XXX

实现此目的的最简单方法是创建一个 appsettings.Development.json 文件,您可以在其中设置以下配置:

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

默认情况下,您将能够在控制台中看到日志。您可以使用不同的 ILoggerProvider 更改日志的显示位置。

我建议您阅读 Logging chapter on MSDN

关于c# - 确定哪些中间件在 ASP.NET Core 网站上响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55851621/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com