Use ASP. net mvc action filters to record logs
Address: http://www.singingeels.com/Articles/Logging_with_ASPNET_MVC_Action_Filters.aspx
Translation: Anders Liu
Abstract: log recording is a common Cross-Cutting Concern. Many ASP. NET developers process it in the Global. asax file. Because MVC is built on ASP. NET, you can use the same solution, but there are better methods. This article shows you how easy it is to add logs to Web applications using ASP. net mvc action filters.
Logging is a common Cross-Cutting Concern that implements ASP. NET developers solve in the Global. asax file. Because MVC is built on top of ASP. NET youCocouldTap into the same solution, but there is a better way. This article will show how easy it is to add logging to your web app using ASP. net mvc Action Filters.
Log recording is a common cross-cutting concern. Many ASP. NET developers process it in the Global. asax file. Because MVC is built on ASP. NETYesUse the same solution, but there is a better solution. This article shows you how easy it is to add logs to Web applications using ASP. net mvc action filters.
Action Filters give you the ability to run M code before or after an action (or page) is hit. applying action filters to your MVC app is simple because they are implemented as attributes that can be placed on a method (an individual Action), or a class (the entire Controller ).
The operation filter allows you to run custom code before and after the operation (or page) is executed. Using action filters in MVC applications is simple because they are implemented by features and can be placed before methods (a separate action) or classes (the entire Controller.
To show how easy this is, we're going to take the out-of-the-box ASP. net mvc template, and very slightly tweak it to begin logging. we'll add one class (our custom Action Filter), and salt the existing pages with our new "LogRequest" attribute.
To see how simple this is, we use the out-of-the-box ASP. net mvc template to record logs with a slight adjustment. We will add a class (custom operation filter) and use this new "LogRequest" feature to "modulated" existing pages.
Creating a Custom Action Filter
Create a custom operation Filter
To create your own action filter, you simply have to inherit from the base "ActionFilterAttribute" class that's already a part of the MVC framework. to make this easier on myself, I 've also implemented the IActionFilter interface so that Visual Studio can auto-generate my two methods for me.
To create your own operation filters, simply inherit the ActionFilterAttribute base class, which is part of the MVC framework. For convenience, I also implemented the IActionFilter interface, so that Visual Studio will automatically generate two methods for me.
At this point, my custom action filter looks like this:
In this case, the custom operation filter looks like this:
Public class logsrequestsattriattribute: ActionFilterAttribute, IActionFilter <br/>{< br/> void IActionFilter. onActionExecuted (ActionExecutedContext filterContext) <br/>{< br/> // I need to do something here... <br/>}< br/> void IActionFilter. onActionExecuting (ActionExecutingContext filterContext) <br/>{< br/> // I need to do something here... <br/>}< br/>}
The ActionFilterAttribute and IActionFilter interface comes from the System. Web. Mvc namespace.
The ActionFilterAttribute and IActionFilter interfaces are from the System. Web. Mvc namespace.
It's important to remember that this article was written (and the sample app was compiled) for ASP. net mvc Preview 4. when the beta and release is eventually out, specifics of the article may not be 100% relevant. however, this capability shocould remain the same.
Remember that this article (the sample program in the text) was written for ASP. net mvc Preview 4. After beta and release are released, the details of the article may not be 100% valid. However, this function is the same.
Applying Our Custom Action Filter
Application custom operation Filter
Now that we have created our action filter, we need to apply it to our Controllers or optionally, to our Actions. because logging is something that you wowould likely want on all of your "pages", we'll simply add it at the controller level. here is what we 've added to the two existing controllers that were supplied in the ASP. net mvc template.
Now that we have created an operation filter, we need to apply it to the Controller or selectively apply it to the operation. Because you may want to record all the "pages", we simply add them to the Controller level. Here we add it to the two controllers provided by the ASP. net mvc template.
// HandleError was already there... <br/> [HandleError] <br/> [LogRequest] <br/> public class HomeController: Controller <br/>{< br/>... <br/>}< br/> // HandleError was already there... <br/> [HandleError] <br/> [LogRequest] <br/> public class AccountController: Controller <br/>{< br/>... <br/>}
That's it! The ASP. net mvc framework will automatically call our methods (OnActionExecuting and then OnActionExecuted) when a request comes in to any of those two controllers.
That's it! When a request enters the two controllers, the ASP. net mvc Framework automatically calls our method (OnActionExecuting and OnActionExecuted ).
At this point, all we have to do is actually implement our logging code. because I want to be able to easily report against my site's activity, I'm going to log to a SQL database. now, it's important to note that action filters are executed synchronously (for obvious reasons ), but I don't want the user to have to wait for my logging to happen before he can enjoy my great site. so, I'm going to use the fire and forget design pattern.
At this point, we must truly implement the logging code. Because I want to easily report site activities, I recorded the logs in the SQL database. Note that the Operation filter is executed synchronously (for obvious reasons), but I don't want users to wait to record logs before accessing my awesome site. Therefore, I will use the fire and forget design mode.
When we're all done, this is what our logger will look like:
After all this is done, our log record looks like this:
// By the way, I'm using the Entity Framework for fun. <br/> public class logrequestattrities: ActionFilterAttribute, IActionFilter <br/>{< br/> private static LoggerDataStoreEntities DataStore = new LoggerDataStoreEntities (); <br/> void IActionFilter. onActionExecuting (ActionExecutingContext filterContext) <br/>{< br/> ThreadPool. queueUserWorkItem (delegate <br/>{< br/> DataStore. addToSiteLog (new SiteLog <br/>{< br/> Action = filterContext. actionMethod. name, <br/> Controller = filterContext. controller. toString (), <br/> TimeStamp = filterContext. httpContext. timestamp, <br/> IPAddress = filterContext. httpContext. request. userHostAddress, <br/>}); <br/> DataStore. saveChanges (); <br/>}); <br/>}< br/>}
Conclusion
Summary
There are a lot of features in MVC that cocould each merrit their own articles, but Action Filters are definately one feature that shows off the advantages of the MVC design pattern. action Filters make perfect sense for cross-cutting concerns like logging, but you can get creative with how and why you use them.
MVC has many features, each of which can be written separately, but the operation filter can best show off the features of the MVC design pattern. Operational filters have common similarities and differences for staggered concerns, but you need to be creative in how and why you use them.
This article isn't here to show you the best way to do logging, but rather how and why you wocould use ASP. net mvc Action Filters. here's the source code, play around with it: MVC_CustomActionFilter_Logging.zip
This article does not introduce the best way to record logs, but shows how and why to use ASP. net mvc to operate filters. Here is the source code. Have a good time: MVC_CustomActionFilter_Logging.zip.