ASP.NET MVC 2.0 中的 OutputCache 和日志记录
我想我可能已经知道答案了,但这里是。我想在调用某些方法时进行记录,但我很困惑,因为我通过将 OutputCache 属性与这些方法一起使用而获得了性能优势。当该方法被多次调用时,ASP.NET MVC 会从之前的调用中返回 HTML,同时缓存尚未过期,而且它又好又快。但该方法中的任何日志记录命令都不会执行。
有什么方法可以让我打开日志记录来记录这些方法被调用的时间,而不必删除 [OutputCache] 并失去我所获得的性能优势?即使 [OutputCache] 属性在先前的输出已被缓存时基本上会短路方法执行,基于属性的日志记录机制是否会以某种方式工作?
谢谢, 杰夫
I think I might already know the answer, but here goes. I want to log when certain methods are called, but I'm torn because I've been gaining performance benefits by using the OutputCache attribute with those methods. When the method is called many times, ASP.NET MVC returns the HTML from prior calls while the cache hasn't expired and it's nice and fast. But any logging commands within that method wouldn't execute.
Is there some way for me to turn on logging to record when those methods are called, without having to remove [OutputCache] and losing the performance benefits I'm getting? Would an attribute-based logging mechanism somehow work even though the [OutputCache] attribute basically short-circuits method execution when prior output has been cached?
Thanks,
Jeff
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
Global.asax
中的Application_BeginRequest
和Application_EndRequest
事件来记录操作执行之前和之后的信息。即使对于使用[OutputCache]
属性修饰的控制器操作,这些事件也会触发。You could use the
Application_BeginRequest
andApplication_EndRequest
events inGlobal.asax
to log information before and after the action is executed. Those events will fire even for controller actions decorated with the[OutputCache]
attribute.Darin Dimitrov 是正确的,请使用
Application_BeginRequest
和Application_EndRequest
方法。您将这样做:
无论版本是否从缓存中提供,这些方法都会被调用。
Darin Dimitrov is correct, use the
Application_BeginRequest
andApplication_EndRequest
methods.This is how you would do it:
Those methods will get called regardless if the version is served from the cache or not.