0

我正在构建一个 MVC4 Web 应用程序。我的要求是使用 log4net 记录控制器操作方法的 http 请求的执行持续时间。

我听说这样做的首选方法是创建一个自定义操作过滤器 - 在 onExecuting 方法中启动秒表 - 然后在 onExecuted 中记录持续时间。

我有一个自定义模型绑定器对我的存储库进行查询以构造复杂的对象。因为模型绑定器在过滤器之前执行,所以我没有准确的持续时间读数。

我可以使用自定义 HTTPModule,但我怎么知道它是否路由到操作方法...

编辑:

我的操作将客户域对象作为参数。我使用自定义模型绑定器在 url 中查找密钥并从我的存储库中获取客户对象。

有任何想法吗?

4

1 回答 1

0

You question is a bit confusing. You want to track action/result execution or model binder execution. Why do you have model binder contacting your repository to construct an object? Model binder should construct an object to a type that you specify in your HttpPost action. As for action/result tracking here is a little reference that might help you figure out what you want to track (if you want to track using action filters).

In MVC you can track when you action started to execute and when it finished, and you can also track when the result began processing and when the result ended.

The first two options are implemented via interface IActionFilter. This filter requires you to implement two methods: OnActionExecuting and OnActionExecuted. The first method is invoked just before the action started and the last method is invoked just after the action ended.

The second interface is concerning the result being generated. This is implemented via IResultFilter interface. It also contains two methods to be implemented. These are OnResultExecuting and OnResultExecuted. The first method, OnResultExecuting is invoked after OnActionExecuted completed. This is the point at which you result, let's say ViewResult has started processing the output that will return to a user. Once the view is ready and returned to a user, OnResultExecuted kicks in. This informs you that the processing is done.

Now, what you want to log is up to you. If you need to log when you action starts, makes a call to the underlying data sources and ends, then implement IActionFilter. If you need to track how long it takes for a result to process then implement IResultFilter.

You can also implement both filters at once by inheriting from ActionFilterAttribute.

于 2013-06-24T20:23:49.140 回答