【问题标题】:Event logging using log4Net or Enterprise Library使用 log4Net 或 Enterprise Library 记录事件
【发布时间】:2018-07-29 10:54:05
【问题描述】:

我有一个旧版 MVC 应用程序。我不想打扰所有操作方法,但想记录用户活动,以及调用的一系列方法和传递的输入参数值,以及此过程中是否有任何异常。有没有一种方法可以使用 .net 反射或任何 mvc 过滤器进行最小的更改。

【问题讨论】:

    标签: .net events logging


    【解决方案1】:

    您可以使用 ActionFilterAttribute (https://docs.microsoft.com/fr-fr/aspnet/mvc/overview/older-versions-1/controllers-and-routing/understanding-action-filters-cs)

    如果你在 .NET Core 中:

    public class UserActivityFilter : ActionFilterAttribute
    {
        public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutingDelegate next)
        {
            await next();
            // here get your logger service (example )
            var logger = context.HttpConcet.RequestServices.GetService(typeof(ILogger)) as ILogDataOperations;
            await logger.LogUserActivity(context.HttpContext.User.Name, context.HttpContext.Request.Path);
        }
    
    }
    

    然后戴上你的控制器:

    [Authorize]
    [UserActivityFilter]
    public class BaseController : Controller
    {
    ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-25
      • 1970-01-01
      • 2012-09-06
      • 1970-01-01
      • 2011-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多