使用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();
}
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();
}
相关文章推荐
- 学术诚信与职业道德
- 继承+super+final的例子
- 在Mac的terminal下连接 SMB 共享的三种方法
- ubuntu 默认防火墙安装、启用、查看状态
- 如何在代码中减少if else语句的使用
- oc 中代码块中局部变量与全局变量、全局静态变量
- 强化学习基本方法(二)
- HTTP Status 500 - Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.J
- join命令
- 【krpano】高德地图导航插件(源码+介绍+预览)
- 1、CommonUtils.uuid()和CommonUtils.toBean(map, Person.class)
- 强化学习基本方法(一)
- openstack测试工具使用二 --- tempest 的部署和使用
- 项目-排序函数模板将数组a中的前size个元素按从小到大顺序排列。试设计这个函数模板。
- static linked list(静态链表,结合数组实现)
- CSS——盒子模型
- php迭代器的基本例子
- 最大流之Ford-Fulkerson方法详解及实现
- 关于pgsql 几个操作符的效率测试比较
- Excel Sheet Column Number