如何在动作过滤器中记录所有查询参数? [英] How can I log all query parameters in an action filter?

查看:128
本文介绍了如何在动作过滤器中记录所有查询参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am implementing a logger that should capture details through an action filter for the purposes of user activity recording and fault finding.

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

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

I would like queryParams to capture all keys and values from the form collection, route values, action params, json calls and the querystring, which sounds like I would need

filterContext.Controller.ValueProvider

,但似乎无法遍历.所以在我的FilterAttribute中,我有

but it doesn't seem possible to loop through this. So in my FilterAttribute I have

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);
}

在动作过滤器的OnActionExecuting方法中还有另一种方法可以实现此目的吗?

Is there another way to achieve this within the OnActionExecuting method of an action filter?

推荐答案

为什么不简单地记录整个请求正文?这样,您将拥有重构用户请求所需的一切.

Why don't you simply log the entire Request body? This way you will have everything you need to reconstruct the user request.

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var stream = filterContext.HttpContext.Request.InputStream;
    var data = new byte[stream.Length];
    stream.Read(data, 0, data.Length);
    Log(Encoding.UTF8.GetString(data));
}

作为替代方案,您可以尝试记录所有filterContext.HttpContext.Request.Params值以及filterContext.RouteData值:

As an alternative you could try logging all filterContext.HttpContext.Request.Params values as well as filterContext.RouteData values:

这篇关于如何在动作过滤器中记录所有查询参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆