ASP.NET MVC 2.0 中的 OutputCache 和日志记录

发布于 2024-10-07 01:32:56 字数 310 浏览 1 评论 0原文

我想我可能已经知道答案了,但这里是。我想在调用某些方法时进行记录,但我很困惑,因为我通过将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那小子欠揍 2024-10-14 01:32:56

您可以使用 Global.asax 中的 Application_BeginRequestApplication_EndRequest 事件来记录操作执行之前和之后的信息。即使对于使用 [OutputCache] 属性修饰的控制器操作,这些事件也会触发。

You could use the Application_BeginRequest and Application_EndRequest events in Global.asax to log information before and after the action is executed. Those events will fire even for controller actions decorated with the [OutputCache] attribute.

淤浪 2024-10-14 01:32:56

Darin Dimitrov 是正确的,请使用 Application_BeginRequestApplication_EndRequest 方法。

您将这样做:

protected void Application_BeginRequest()
{
    var routeCollection = RouteTable.Routes;
    var routeData = routeCollection.GetRouteData(new HttpContextWrapper(HttpContext.Current));

    // Log
    Debug.WriteLine("Invoking " + routeData.Values["Controller"] + "::" + routeData.Values["Action"]);
}

protected void Application_EndRequest()
{
    // Log
    Debug.WriteLine("EndRequest");
}

无论版本是否从缓存中提供,这些方法都会被调用。

Darin Dimitrov is correct, use the Application_BeginRequest and Application_EndRequest methods.

This is how you would do it:

protected void Application_BeginRequest()
{
    var routeCollection = RouteTable.Routes;
    var routeData = routeCollection.GetRouteData(new HttpContextWrapper(HttpContext.Current));

    // Log
    Debug.WriteLine("Invoking " + routeData.Values["Controller"] + "::" + routeData.Values["Action"]);
}

protected void Application_EndRequest()
{
    // Log
    Debug.WriteLine("EndRequest");
}

Those methods will get called regardless if the version is served from the cache or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文