代码之家  ›  专栏  ›  技术社区  ›  JOBG

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

  •  21
  • JOBG  · 技术社区  · 16 年前

    在ASP MVC应用程序上记录用户活动是否有好的策略?(操作过滤器/httpmodules)。

    类似于上一个用户活动(就像23分钟前看到的stackoverflow),甚至使用了什么页面和控制器,并进一步推送单击了什么按钮或链接。

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

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

    4 回复  |  直到 10 年前
        1
  •  6
  •   Jon Rory    10 年前

    你可以尝试使用 PostSharp 为了为您执行日志记录,它的多播功能可能对类似的事情很有用。如果它不是一个选项,那么模块可能是最容易实现的(假设您可以在管道中的那个点获得所需的用户信息)。

        2
  •  43
  •   Jay Tauqeer Ahmad    10 年前

    Action Filter Attributes 很适合这个,打个电话给 [YourAttributeName] 在控制器的顶部(或者如果您有其他控制器继承的应用程序控制器,则在应用程序中只需要它一次)。

    例如:

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

    从这一点开始,此属性将在控制器中运行每个操作之前被调用,您也可以通过调用 [Log] 以同样的方式。

    要获得所需的日志记录功能,您很可能希望重写 OnResultExecuting OnResultExecuted 这两个都是不言而喻的。

    例如:

    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
  •  21
  •   Community Mohan Dere    9 年前

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

    Implementing Audit Trails using ASP.NET MVC ActionFilters

    Creating Advanced Audit Trails using ActionFilters in ASP.NET MVC

    高级日志非常棒,因为您可以存储请求的数据并进一步过滤这些数据。

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

        4
  •  2
  •   stimms    16 年前

    @罗里的主意很好。Postshap正是您需要的,您可以考虑将它与 ASP.net Health Monitoring

    推荐文章