Log User Activity on ASP.NET MVC Application

十年热恋 提交于 2019-12-03 02:02:05

问题


Is there A good strategy to Log the User activity on an ASP MVC App? (ActionFilters/ HTTPModules).

Something like last user activity (just like StackOverflow "Seen 23 mins ago"), and even what Pages and Controllers were used, and pushing even further what buttons or links were clicked.

I have ELMAH installed but as far as i know its just for Error Logging.

PD: Google Analytics is not an option.


回答1:


You could try using a PostSharp aspect to perform the logging for you, it's multi-cast functionality might come in handy for something like that. If it's not an option, then a Module would probably be the easiest to implement (assuming you can get the required user information at that point in the pipeline).




回答2:


Warning: This was written in 2009 for MVC .NET RC1 and may not be syntactically correct anymore.

Action Filter Attributes are perfect for this, just put a call to [YourAttributeName] at the top of your controller (or if you have an Application Controller which your other controllers inherit from, you only need it once in your application).

For example:

namespace the_name_space
{
    [Log]
    public class ApplicationController : Controller
    {

From this point onwards this attribute will be called before each action is run in your controller(s), you can also specify this on just an action by calling [Log] just before it in the same fashion.

To get the logging functionality which you require, you will most likely want to override OnResultExecuting and OnResultExecuted, both of which are pretty self explanatory.

For example:

public class LogAttribute : ActionFilterAttribute
{
    protected DateTime start_time;

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        start_time = DateTime.Now;
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        RouteData route_data = filterContext.RouteData;
        TimeSpan duration = (DateTime.Now - start_time);
        string controller = (string)route_data.Values["controller"];
        string action = (string)route_data.Values["action"];
        DateTime created_at = DateTime.Now;
        //Save all your required values, including user id and whatnot here.
        //The duration variable will allow you to see expensive page loads on the controller, this can be useful when clients complain about something being slow.
    }
}



回答3:


I just stumbled on these 2 posts by Rion Williams that describe a very straightforward way of doing this:

Implementing Audit Trails using ASP.NET MVC ActionFilters

Creating Advanced Audit Trails using ActionFilters in ASP.NET MVC

The advanced post is really great as you can store the request's data and further filter that data.

I'll implement this right now in my app.




回答4:


@Rory's idea is excelent. PostSharp is just what you need for this, you might consider coupling it with ASP.net Health Monitoring



来源:https://stackoverflow.com/questions/1822754/log-user-activity-on-asp-net-mvc-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!