您的位置:首页 > 其它

使用MVC过滤器保存操作日志

2016-06-03 19:38 357 查看
//定义过滤器

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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: