c# - ASP.Net Core 2 Web API performance metrics in database
问题描述
I want to store each request's in-time, out-time, api in a database context managed by Entity Framework.
Best approach seemed to add a middleware at the entry point. I could get up to the point of collecting the metrics and api, but could not put in database as EF DBContext seems to be tied to Mvc layer. It was giving me a ObjectDisposedException.
Q1: Is this something we can put to work?
So, I was willing to put up with the next best option of moving it to Filter. The first point of entry into Mvc seems to be Authorization filter. But I don't see how to get OnExecuting and OnExecuted events in this filter. I am able to store everything in the database in the filter, though.
Q2: How do I get the in-time and out-time in AuthorizationFilter?
Q3: A more elaborate solution would be to put the metrics into ElasticSearch or an external system. But how to ensure we have a connection pool (or equivalent) wired up and ready for use at the middleware entry point. Would it not suffer the same issue as DBContext?
解决方案
Q3:更精细的解决方案是将指标放入 ElasticSearch 或外部系统。但是如何确保我们有一个连接池(或等效的)连接起来并准备好在中间件入口点使用。它不会遭受与 DBContext 相同的问题吗?
我建议使用带有适当接收器的 Serilog(例如ElasticSearch)。您可以在应用程序启动时对其进行配置,将其集成到主机中UseSerilog
,之后您可以使用标准ILogger
接口(或显式使用 Serilog,例如Log.ForContext<YourMiddleware>()
)轻松地将其注入中间件(或过滤器)
推荐阅读
- laravel - Laravel Blade:与 css url 属性不同,html src 属性指向的图像不在公共目录中编译。为什么?
- header - 将 xlsx 读入 R 并创建新标头
- reactjs - 如何使用 Forge Viewer 查看本地存储的 SVF 文件?
- python - 多个公会欢迎机器人 - discord.py
- c++ - GDB 不会在 Qt 项目中的断点处停止
- python - Matplotlib:TypeError:float()参数必须是字符串或数字,而不是“NoneType”
- minizinc - Minizinc 在求解过程中跟踪数组
- selenium - 有没有办法直接在步骤定义中传递数据而不传递特征文件?
- hy - if 和 cond 有什么区别?
- git - 是否可以不显示来自特定文件夹的 git status 更改?