[翻译] 使用ASP.NET MVC操作过滤器记录日志

[翻译] 使用ASP.NET MVC操作过滤器记录日志

摘要:日志记录是一种常见的交错关注点(Cross-Cutting Concern),很多ASP.NET开发者会在Global.asax文件中处理它。由于MVC是构建在ASP.NET之上的,所以你可以使用同样的解决方式,但还有更好的方法。这篇文章向你展示了使用ASP.NET MVC的操作过滤器来向Web应用程序中添加日志是多么简单。

Logging is a common Cross-Cutting Concern that many ASP.NET developers solve in the Global.asax file. Because MVC is built on top of ASP.NET you could tap 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.

日志记录是一种常见的交错关注点,很多ASP.NET开发者会在Global.asax文件中处理它。由于MVC是构建在ASP.NET之上的,所以你可以使用同样的解决方式,但还有更好的方法。这篇文章向你展示了使用ASP.NET MVC的操作过滤器来向Web应用程序中添加日志是多么简单。

Action Filters give you the ability to run custom 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).


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.

为了看到这有多简单,我们使用了开箱即用的ASP.NET MVC模板,对其进行少许调整就可以开始记录日志了。我们将会添加一个类(自定义的操作过滤器),并用这个新的"LogRequest"特性来“调制”现有的页面。

Creating a Custom Action 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.

要创建自己的操作过滤器,只需要简单地继承ActionFilterAttribute基类,该类是MVC框架的一部分。我为了更方便一些,还实现了IActionFilter接口,这样Visual Studio就会自动为我生成两个方法。

At this point, my custom action filter looks like this:


The ActionFilterAttribute and IActionFilter interface comes 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 should remain the same.

要记得本文(和文中的示例程序)是针对ASP.NET MVC Preview 4编写的。当beta和release版发布后,文章的细节可能不是100%有效的。不过,这一功能还是相同的。

Applying Our Custom Action 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 would 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.

现在我们已经创建好操作过滤器了,我们需要将其应用到控制器上,或者有选择地应用在操作上。因为你可能希望对所有的“页面”进行日志记录,我们简单地将其添加到控制器级别上。在这里我们将其添加到ASP.NET MVC模板提供的两个控制器上。

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.

就是这样!当一个请求进入这两个控制器时,ASP.NET MVC框架会自动调用我们的方法(先是OnActionExecuting,然后是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.

此时,我们必须要真正实现日志记录代码了。由于我希望能简单地报告站点的活动,所以我将日志记录在SQL数据库中。要注意,操作过滤器是同步执行的(原因很明显),但我可不想让用户在访问我的牛逼的站点之前还要等着记录日志。因此,我将使用fire and forget设计模式。

When we're all done, this is what our logger will look like:



There are a lot of features in MVC that could 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.


This article isn't here to show you the best way to do logging, but rather how and why you would use ASP.NET MVC Action Filters. Here's the source code, play around with it: MVC_CustomActionFilter_Logging.zip

这篇文章并没有介绍记录日志最好的方法,而是给出了如何以及为什么使用ASP.NET MVC操作过滤器。这里是源代码,玩得愉快:MVC_CustomActionFilter_Logging.zip

posted @ 2008-08-26 15:20  Anders Liu  阅读(4808)  评论(1编辑  收藏  举报