如何包装所有操作过滤器以进行性能监控?
我正在将越来越多的面向方面的关注点转移到我的 ASP.NET MVC 站点上的 ActionFilters 中。因此,我怀疑我的一些性能瓶颈可能已经转移到 ActionFilters 中,而无法了解它们的性能。我想挂钩 MVC 的 ActionFilter 创建过程来包装每个 ActionFilter,以便我可以记录该 ActionFilter 的执行时间。
到目前为止,我发现了以下网络链接,它们暗示了如何开始,但不确定如何实际包装每个 ActionFilter。
- http://haacked. com/archive/2011/08/10/writing-an-asp-net-mvc-controller-inspector.aspx
- http://msdn.microsoft.com/en-us/magazine/gg309182.aspx< /一>
- <一href="https://stackoverflow.com/questions/5896929/property-injection-without-using-attribute-inject-with-ninject">不使用属性 [Inject] 和 Ninject 进行属性注入
I am moving more and more of my aspect-oriented concerns into ActionFilters on my ASP.NET MVC site. As a result, I suspect some of my performance bottlenecks may have moved into ActionFilters with no visibility of what their performance is. I would like to hook into ActionFilter creation process of MVC to wrap each ActionFilter so that I can log how long the execution of that ActionFilter takes.
So far, I have found the following web links which hint at how to start down the road but am not sure how to actually wrap each ActionFilter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该从 ControllerActionInvoker 派生新类并重写 GetFilters 方法。在此方法中,您应该创建一个诊断包装类的新实例,该类实现包装真实过滤器的相同接口 (IActionFilter)。 GetFilters 作为 InvokeAction 方法的一部分调用(也可以被重写),但 GetFilters 可能是最干净的。
另外,您也可以尝试覆盖 InvokeActionMethodWithFilters,这是实际调用操作过滤器的地方,但这可能会很混乱,但如果您只是添加了一些内容,则可能没问题
例如:
要实现 ControllerActionInvoker 类,请参阅:
要实际指定调用您的自定义 ControllerActionInvoker,您需要创建一个 DefaultControllerFactory 实现,例如:
并且通过在应用程序代码中进行设置,如下所示:
You should derive and new class from ControllerActionInvoker and override the GetFilters method. In this method you should create a new instance of a diagnostic wrapper class implementing the same interface (IActionFilter) that wrap the real filter. GetFilters is called as part of the InvokeAction method (also can be overridden) but GetFilters is probably the cleanest.
Also you could also try overriding InvokeActionMethodWithFilters which is where the action filters are actually invoked however again this could be messy but may be ok if you just added something
like:
To implement an ControllerActionInvoker class see:
To actually specify that your custom ControllerActionInvoker is called you need to create a DefaultControllerFactory implementation like:
And like by setting is in the a your application code like so: