//定义过滤器
public class LogAttribute : ActionFilterAttribute
{
/// <summary>
/// 以逗号间隔
/// </summary>
private readonly string _parameterNames;
public BizActivityLogAttribute( string parameters)
{
_parameterNames = parameters;
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var _logTypeName = string.Format("{0}/{1}", filterContext.ActionDescriptor.ControllerDescriptor.ControllerName, filterContext.ActionDescriptor.ActionName);
Dictionary<string, string> paramDic = new Dictionary<string, string>();
foreach (var param in _parameterNames.Split(‘,‘))
{
var paramValue = filterContext.Controller.ValueProvider.GetValue(param);
if (!paramDic.ContainsKey(param))
{
paramDic.Add(param, paramValue.AttemptedValue);
}
}
//然后就可以使用Log4Net之类的存储参数了...
}
}
//使用过滤器
[LogAttribute( "msg,msgList")]
public ActionResult GetMessage( [ModelBinder(typeof(JsonBinder<CMessage>))]CMessage msg,
[ModelBinder(typeof(JsonBinder<List<CMessage>>))]List<CMessage> msgList
)
{
return View();
}