asp.net-mvc 在 ASP.NET MVC 应用程序上记录用户活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1822754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:58:13  来源:igfitidea点击:

Log User Activity on ASP.NET MVC Application

asp.net-mvcloggingaudit-trailaudit-logginguser-activity

提问by JOBG

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 StackOverflow "Seen 23 mins ago"), and even what Pages and Controllers were used, and pushing even further what buttons or links were clicked.

类似于上次用户活动(就像 StackOverflow“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:谷歌分析不是一种选择。

采纳答案by Rory

You could try using a PostSharpaspect 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方面为您执行日志记录,它的多播功能可能会派上用场。如果它不是一个选项,那么模块可能是最容易实现的(假设您可以在管道中的那个点获得所需的用户信息)。

回答by Jay

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

警告:这是 2009 年为 MVC .NET RC1 编写的,在语法上可能不再正确。

Action Filter Attributesare 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).

Action Filter Attributes非常适合这个,只需[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]在它之前以相同的方式调用来仅在一个动作上指定它。

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

要获得您需要的日志记录功能,您很可能希望覆盖OnResultExecutingOnResultExecuted,这两者都是不言自明的。

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

回答by Leniel Maccaferri

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

我偶然发现了Rion Williams 的这 2 个帖子,它们描述了一种非常简单的方法:

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.

我现在将在我的应用程序中实现这一点。

回答by stimms

@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 结合使用