简体   繁体   中英

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. I want to get more details.

Example I want to get a string like below:

"Admin created a Project "ABC""

or

"Admin edited Project "DEF" with changes: "PM = John" to 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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