MVC3 is the best way to register a request

I want to keep a log of all requests to my MVC 3 application, including the requested URL, ip user address, user agent, etc. Where is the best place for this

1) use a basic controller?

2) use an action filter?

3) others?

+4
source share
3 answers

I am doing this inside my BaseController. Something like that:

protected override void OnActionExecuted(ActionExecutedContext filterContext) { // If in Debug mode... if (filterContext.HttpContext.IsDebuggingEnabled) { var message = string.Format(CultureInfo.InvariantCulture, "Leaving {0}.{1} => {2}", filterContext.Controller.GetType().Name, filterContext.ActionDescriptor.ActionName.Trim(), filterContext.Result); Logger.Debug(message); } // Logs error no matter what if (filterContext.Exception != null) { var message = string.Format(CultureInfo.InvariantCulture, "Exception occured {0}.{1} => {2}", filterContext.Controller.GetType().Name, filterContext.ActionDescriptor.ActionName.Trim(), filterContext.Exception.Message); Logger.Error(message); } base.OnActionExecuted(filterContext); } 

Hope you get this idea.

You can also register before the action is completed using:

 protected override void OnActionExecuting(ActionExecutingContext filterContext) 
+6
source

HttpModule seems best if you ask me.

All the data that you talk about registration is available long before any particular controller is called. Therefore, if you perform registration outside the controller, you can even capture requests that do not correspond to the actual actions of the controller. And there is no need to clutter up your controller code with what really is a cross-cutting issue.

+6
source

When rendering a single request, you can invoke several action methods. Consider using RenderAction on the layout page as follows:

 Html.RenderAction("Navigation", "Menu") 

It is worth noting that you have two journal entries with the same information if you decide to use an action filter for logging.

-1
source

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


All Articles