相关疑难解决方法(0)

如何在操作过滤器中记录所有查询参数?

我正在实现一个记录器,它应该通过操作过滤器捕获详细信息,以便记录用户活动和查找故障。

   public interface ISessionLogger
    {
        void LogUserActionSummary(int sessionId, string userActionType);    
        void LogUserActionDetail(int session, string userActionType, DateTime userActionStartedUtc, long actionDurationInMilliseconds, string queryParams, string referredFrom);
    }
Run Code Online (Sandbox Code Playgroud)

我想queryParams从表单集合、路由值、操作参数、json 调用和查询字符串中捕获所有键和值,这听起来像是我需要的

filterContext.Controller.ValueProvider

但似乎不可能循环遍历这个。所以在我的 FilterAttribute 中我有

public void OnActionExecuting(ActionExecutingContext filterContext)
{
    var paramList = new List<string>();
    if(filterContext.ActionParameters != null)
    {
        paramList.AddRange(filterContext.ActionParameters.Select(param => String.Format("{0}:{1}", param.Key, param.Value.ToString())));
    }
    if(filterContext.Controller.ValueProvider != null)
    {
        //loop through the valueprovider and save the key value pairs to paramList
    }

    _queryParams = string.Join(",", paramList);
}
Run Code Online (Sandbox Code Playgroud)

是否有另一种方法可以在操作过滤器的 OnActionExecuting 方法中实现此目的?

c# logging action-filter asp.net-mvc-3

4
推荐指数
1
解决办法
6807
查看次数

标签 统计

action-filter ×1

asp.net-mvc-3 ×1

c# ×1

logging ×1