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

 

//定义过滤器

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

 

posted @ 2016-06-03 19:38  zslm___  阅读(452)  评论(0编辑  收藏  举报