參考這篇
張小呆的碎碎唸
稍微改成log到資料庫並記錄下當時所處理的資料.
public class LogActionFilterAttribute : ActionFilterAttribute
{
public string ControllerName { get; set; }
public string ActionName { get; set; }
private ApplicationDbContext db = new ApplicationDbContext();
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string uid = filterContext.HttpContext.User.Identity.Name.ToString();
string originController = filterContext.RouteData.Values["controller"].ToString();
string originAction = filterContext.RouteData.Values["action"].ToString();
string originArea = String.Empty;
if (filterContext.RouteData.DataTokens.ContainsKey("area"))
originArea = filterContext.RouteData.DataTokens["area"].ToString();
//這邊利用傳遞參數的名稱來判定要如取出相關資料與以記錄, 所以Action的參數名稱就必須要有可鑑別性
string result = "";
if (filterContext.ActionParameters.ContainsKey("empv"))
{
empEditViewModels viewModel = (empEditViewModels)filterContext.ActionParameters["empv"];
result = viewModel.emp1.eid + viewModel.emp1.cname + " 職稱: " + viewModel.emp1.title + " 部門編號: " + viewModel.emp1.dept;
};
if (filterContext.ActionParameters.ContainsKey("emp"))
{
emp emp1 = (emp)filterContext.ActionParameters["emp"];
result = emp1.eid + emp1.cname + " 職稱: " + emp1.title + " 部門編號: " +emp1.dept;
};
if (filterContext.ActionParameters.ContainsKey("id"))
{
int id1 = (int)filterContext.ActionParameters["id"];
result = " 序號: " + id1.ToString();
};
//紀錄相關資料到資料庫
actlog logmodel = new actlog()
{
App = ControllerName+originController,
Act = ActionName+originAction,
Pepo = String.IsNullOrEmpty(SessionHelper.RealName) ? uid : SessionHelper.RealName,
Ext ="進入網頁 : "+result,
Tm = DateTime.Now
};
db.actlogs.Add(logmodel);
db.SaveChanges();
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
}
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
string uid = filterContext.HttpContext.User.Identity.Name.ToString();
actlog logmodel = new actlog()
{
App = ControllerName,
Act = ActionName,
Pepo = String.IsNullOrEmpty(SessionHelper.RealName) ? uid : SessionHelper.RealName,
Ext = "傳回的動作結果執行完成",
Tm = DateTime.Now
};
db.actlogs.Add(logmodel);
db.SaveChanges();
}
}
在control中使用範例 :
[HttpPost]
[ValidateAntiForgeryToken]
[LogActionFilter(ControllerName = "員工資料管理", ActionName = "新增完成")]
public ActionResult Create([Bind(Include = "id,eid,cname,dept,title")] emp emp, int? page, int? itemsPerPage, string sortOrder, string currentFilter)
{
ViewBag.DropDownList = new SelectList(db.deps, "Id", "title");
ViewBag.CurrentPage = page;
ViewBag.CurrentItemsPerPage = itemsPerPage;
ViewBag.OldSortParm = sortOrder;
ViewBag.CurrentFilter = currentFilter;
if (ModelState.IsValid)
{
db.emps.Add(emp);
db.SaveChanges();
return RedirectToAction("Index", new { page = ViewBag.CurrentPage, itemsPerPage = ViewBag.CurrentItemsPerPage, sortOrder = ViewBag.OldSortParm, searchString = "", CurrentFilter = ViewBag.CurrentFilter });
}
return View(emp);
}
紀錄結果 :
沒有留言:
張貼留言