在ASP.NET MVC应用程序上记录用户活动

时间:2021-11-12 12:45:23

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

是否有一个很好的策略来记录ASP MVC应用程序上的用户活动? (ActionFilters / HTTPModules)。

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

像上次用户活动的东西(就像*“23分钟前看到的那样”),甚至是使用的页面和控制器,并进一步推动了点击的按钮或链接。

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

我安装了ELMAH,但据我所知它只是用于错误记录。

PD: Google Analytics is not an option.

PD:谷歌分析不是一个选项。

4 个解决方案

#1


6  

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).

您可以尝试使用PostSharp方面为您执行日志记录,它的多播功能可能会派上用场。如果它不是一个选项,那么模块可能是最容易实现的(假设您可以在管道中的那一点获得所需的用户信息)。

#2


41  

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).

操作过滤器属性非常适用于此,只需在控制器顶部调用[YourAttributeName](或者如果您有其他控制器继承的应用程序控制器,则只需在应用程序中使用一次)。

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.

从此时起,将在控制器中运行每个操作之前调用此属性,您也可以通过以相同的方式调用[Log]之前的[Log]来仅在操作上指定此属性。

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.

要获得所需的日志记录功能,您很可能希望覆盖OnResultExecuting和OnResultExecuted,这两者都是非常自我解释的。

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


19  

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

我只是偶然发现了Rion Williams的这两篇帖子,这些帖子描述了一种非常简单的方法:

Implementing Audit Trails using ASP.NET MVC ActionFilters

使用ASP.NET MVC ActionFilters实现审计跟踪

Creating Advanced Audit Trails using ActionFilters in ASP.NET MVC

使用ASP.NET MVC中的ActionFilters创建高级审计跟踪

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


2  

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

@Rory的想法很棒。 PostSharp正是您需要的,您可以考虑将其与ASP.net Health Monitoring相结合

#1


6  

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).

您可以尝试使用PostSharp方面为您执行日志记录,它的多播功能可能会派上用场。如果它不是一个选项,那么模块可能是最容易实现的(假设您可以在管道中的那一点获得所需的用户信息)。

#2


41  

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).

操作过滤器属性非常适用于此,只需在控制器顶部调用[YourAttributeName](或者如果您有其他控制器继承的应用程序控制器,则只需在应用程序中使用一次)。

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.

从此时起,将在控制器中运行每个操作之前调用此属性,您也可以通过以相同的方式调用[Log]之前的[Log]来仅在操作上指定此属性。

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.

要获得所需的日志记录功能,您很可能希望覆盖OnResultExecuting和OnResultExecuted,这两者都是非常自我解释的。

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


19  

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

我只是偶然发现了Rion Williams的这两篇帖子,这些帖子描述了一种非常简单的方法:

Implementing Audit Trails using ASP.NET MVC ActionFilters

使用ASP.NET MVC ActionFilters实现审计跟踪

Creating Advanced Audit Trails using ActionFilters in ASP.NET MVC

使用ASP.NET MVC中的ActionFilters创建高级审计跟踪

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


2  

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

@Rory的想法很棒。 PostSharp正是您需要的,您可以考虑将其与ASP.net Health Monitoring相结合