我想在Entity Framework管理的数据库上下文中存储每个请求的及时,out-time,api。
最好的方法似乎是在入口点添加一个中间件。我可以达到收集指标和api的程度,但由于EF DBContext似乎与Mvc层绑定,因此无法放入数据库。它给了我一个ObjectDisposedException。
Q1: Is this something we can put to work?
因此,我愿意忍受将其移至Filter的下一个最佳选择。进入Mvc的第一个点似乎是授权过滤器。但我不知道如何在此过滤器中获取OnExecuting和OnExecuted事件。但是,我能够将所有内容存储在过滤器中的数据库中。
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?
问题3:更精细的解决方案是将指标放入ElasticSearch或外部系统。但是如何确保我们有一个连接池(或等效的)连线并准备好在中间件入口点使用。它不会遇到与DBContext相同的问题吗?
我建议使用Serilog和适当的水槽(例如ElasticSearch)。您可以在应用启动时配置它,使用UseSerilog
将其集成到主机,之后您可以使用标准ILogger
接口(或明确使用Serilog,例如Log.ForContext<YourMiddleware>()
)将其注入中间件(或过滤器)
您将流量指标提到外部系统,因此值得建议将Application Insights添加到您的应用程序中。结合门户网站,它是一个非常强大的工具,尤其是analytics功能。还有一个免费的层级iirc。
我不为MS等工作,所以我不是推销产品,只是一个建议。