简体   繁体   English

在ASP.NET MVC中按ActionFilterAttribute记录活动的详细信息

[英]Recording details of activity by ActionFilterAttribute in ASP.NET MVC

I'm using a Custom Action Filter inherit ActionFilterAttribute to record activity logs, but I only could get current username, datetime, controller and action. 我正在使用自定义操作过滤器继承ActionFilterAttribute来记录活动日志,但是我只能获取当前的用户名,日期时间,控制器和操作。 I want to get more details. 我想获得更多细节。

Example I want to get a string like below: 示例我想获取如下字符串:

"Admin created a Project "ABC"" “管理员创建了一个项目“ ABC””

or 要么

"Admin edited Project "DEF" with changes: "PM = John" to Harry" “管理员编辑的项目“ DEF”,更改为:“ PM = John”更改为Harry”

Project is a table of my DB and Create, Edit can be a Action, PM is a field of Project table! 项目是我的数据库的表,创建,编辑可以是动作,项目表是项目表的字段!

I'm using ASP.NET MVC 5 with Visual Studio 2013. 我正在将ASP.NET MVC 5与Visual Studio 2013一起使用。

Thanks! 谢谢!

I don't think action filters are the right fit for what you are trying to achieve. 我认为动作过滤器不适合您要实现的目标。 If you do the logging before the action has executed, you will be logging the creation of the project even if the data turn out to be invalid. 如果在执行操作之前进行了日志记录,那么即使数据无效,也将记录项目的创建。 You could probably do the logging after the action has executed, but you would have to look out for exceptions and inferring what had actually been created would be awkward. 您可以在执行动作后进行日志记录,但是您必须注意异常情况,并推断实际创建的内容会很尴尬。

You should write to your activity log around the same time that you write the new project to the underlying data store, maybe even in the same transaction. 您应该在将新项目写入基础数据存储的同时,甚至在同一事务中,将活动日志写入活动日志。 One simple approach if you have a repository would be to decorate it with logging functionality. 如果您拥有存储库,一种简单的方法是使用日志记录功能对其进行装饰

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM