首页 > 解决方案 > 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?

标签: c#asp.net-coreasp.net-core-webapi

解决方案


Q3:更精细的解决方案是将指标放入 ElasticSearch 或外部系统。但是如何确保我们有一个连接池(或等效的)连接起来并准备好在中间件入口点使用。它不会遭受与 DBContext 相同的问题吗?

我建议使用带有适当接收器的 Serilog(例如ElasticSearch)。您可以在应用程序启动时对其进行配置,将其集成到主机中UseSerilog,之后您可以使用标准ILogger接口(或显式使用 Serilog,例如Log.ForContext<YourMiddleware>())轻松地将其注入中间件(或过滤器)


推荐阅读