OutputCache and Logging in ASP.NET MVC 2.0

I think I already know the answer, but here. I want to log when certain methods are called, but I am torn because I get performance benefits using the OutputCache attribute with these methods. When a method is called many times, ASP.NET MVC returns HTML from previous calls until the cache has expired, which is nice and fast. But any logging commands in this method will not be executed.

Is there any way to enable logging for writing when these methods are called without having to remove [OutputCache] and the performance loss that I get? Will the attribute-based logging mechanism not work correctly even if the [OutputCache] attribute mainly performs the short circuit method when the previous output was cached?

Thanks Jeff

+3
source share
2 answers

Darin Dimitrov is right, use the Application_BeginRequestand methods Application_EndRequest.

Here's how you 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");
}

These methods will be called whether the version is loaded from the cache or not.

+1
source

Application_BeginRequest Application_EndRequest Global.asax . , [OutputCache].

+2

Source: https://habr.com/ru/post/1779800/


All Articles