public ActionResult Create(SysLog entity) { if (entity != null && ModelState.IsValid) { string currentPerson = GetCurrentPerson(); entity.CreateTime = DateTime.Now; entity.CreatePerson = currentPerson; entity.Id = Result.GetNewId(); string returnValue = string.Empty; if (m_BLL.Create(ref validationErrors, entity)) { LogClassModels.WriteServiceLog(Suggestion.InsertSucceed + ",日志的信息的Id为" + entity.Id,"日志" );//写入日志 return Json(Suggestion.InsertSucceed); } else { if (validationErrors != null && validationErrors.Count > 0) { validationErrors.All(a => { returnValue += a.ErrorMessage; return true; }); } LogClassModels.WriteServiceLog(Suggestion.InsertFail + ",日志的信息," + returnValue,"日志" );//写入日志 return Json(Suggestion.InsertFail + returnValue); //提示插入失败 } } return Json(Suggestion.InsertFail + ",请核对输入的数据的格式"); //提示输入的数据的格式不对 }
/// <summary> /// 创建一个日志 /// </summary> /// <param name="validationErrors">返回的错误信息</param> /// <param name="db">数据库上下文</param> /// <param name="entity">一个日志</param> /// <returns></returns> public bool Create(ref ValidationErrors validationErrors, SysLog entity) { try { repository.Create(entity); return true; } catch (Exception ex) { validationErrors.Add(ex.Message); ExceptionsHander.WriteExceptions(ex); } return false; }
public static void WriteServiceLog(string message, string logType, LogOpration logOpration = LogOpration.Default) { try { //logOpration设置优先级高于配置节logEnabled bool logEnabled = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["LogEnabled"]); if (logOpration == LogOpration.Fobid || (logOpration == LogOpration.Default && !logEnabled)) { return; } else if (logOpration == LogOpration.Start || (logOpration == LogOpration.Default && logEnabled)) { SysLog sysLog = new SysLog(); sysLog.Id = Result.GetNewId(); sysLog.CreateTime = DateTime.Now; sysLog.Ip = GetIP(); sysLog.Message = message; sysLog.CreatePerson = AccountModel.GetCurrentPerson(); sysLog.MenuId = logType;//哪个模块生成的日志 ISysLogBLL sysLogRepository = new SysLogBLL(); ValidationErrors validationErrors = new ValidationErrors(); sysLogRepository.Create(ref validationErrors, sysLog); return; } } catch (Exception ep) { try { string path = @"mylog.txt"; string txtPath = System.Web.HttpContext.Current.Server.MapPath(path);//获取绝对路径 using (StreamWriter sw = new StreamWriter(txtPath, true, Encoding.Default)) { sw.WriteLine((ep.Message + "|" + message + "|" + GetIP() + DateTime.Now.ToString()).ToString()); sw.Close(); } return; } catch { return; } } }
public ActionResult Edit(string id, SysLog entity) { if (entity != null && ModelState.IsValid) { //数据校验 string currentPerson = GetCurrentPerson(); //entity.UpdateTime = DateTime.Now; //entity.UpdatePerson = currentPerson; string returnValue = string.Empty; if (m_BLL.Edit(ref validationErrors, entity)) { LogClassModels.WriteServiceLog(Suggestion.UpdateSucceed + ",日志信息的Id为" + id,"日志" );//写入日志 return Json(Suggestion.UpdateSucceed); //提示更新成功 } else { if (validationErrors != null && validationErrors.Count > 0) { validationErrors.All(a => { returnValue += a.ErrorMessage; return true; }); } LogClassModels.WriteServiceLog(Suggestion.UpdateFail + ",日志信息的Id为" + id + "," + returnValue, "日志" );//写入日志 return Json(Suggestion.UpdateFail + returnValue); //提示更新失败 } } return Json(Suggestion.UpdateFail + "请核对输入的数据的格式"); //提示输入的数据的格式不对 }