Esempio n. 1
0
        public static void WriteLog(ControllerContext controllerContext, ViewDataDictionary viewData)
        {
            var cc = controllerContext.Controller.ControllerContext;

            var category = GetCategory(controllerContext);
            var title = GetTitle(controllerContext);

            var log = new ActionLog
            {
                Controller = cc.RouteData.GetRequiredString("controller"),
                Action = cc.RouteData.GetRequiredString("action"),
                Url = controllerContext.HttpContext.Request.Path,
                CreatedAt = DateTime.Now,
                CreatedBy = "SYSTEM",
                Address = controllerContext.HttpContext.Request.UserHostAddress,
                Actor = TryGetUserName(controllerContext),
                Method = controllerContext.HttpContext.Request.HttpMethod,
                Category = GetCategory(controllerContext),
                Title = title.Equals(category) ? "查询" : title,
                Result = TryGetResult(controllerContext),
                ModelId = cc.RouteData.Values.ContainsKey("id") ? cc.RouteData.GetRequiredString("id") : null,
                Description = viewData.ContainsKey(Const.ToLogMessage) ? viewData[Const.ToLogMessage].ToString() : null
            };

            if (log.Description == null && viewData.ContainsKey(Const.ToLogModel))
            {
                // 取得描述
                var model = viewData.ContainsKey(Const.ToLogModel) ? viewData[Const.ToLogModel] : viewData.Model;
                if (model != null)
                {
                    if (!model.GetType().IsGenericType)
                    {
                        log.Description = model.Print();
                    }
                    else
                    {
                        var objs = (IEnumerable<object>)model;
                        log.Description = string.Join(";" + Environment.NewLine, objs.Select(m => m.Print()));
                    }

                }
            }

            using (var ds = new SessionFactory().OpenSession())
            {
                if (!ds.Create(log))
                {
                    Log.Error("记录日志失败:{0}", log.ToString());
                }
            }
        }
 //#region 通用控制器模块
 //public ActionResult CommonCreate<TModel>(FormCollection collection,
 //    Func<Session, bool> onTouch = null,
 //    Func<TModel, Session, bool> beforePersistance = null,
 //    Func<TModel, Session, bool> afterPersistance = null
 //    ) where TModel : class, new()
 //{
 //    using (var session = new SessionFactory().OpenSession())
 //    {
 //        try
 //        {
 //            session.BeginTransaction();
 //            if (onTouch != null && !onTouch(session))
 //            {
 //                return Close();
 //            }
 //            var model = new TModel();
 //            TryUpdateModel(model, collection);
 //            if (!ModelState.IsValid)
 //            {
 //                // ReSharper disable Mvc.ViewNotResolved
 //                return View(model);
 //                // ReSharper restore Mvc.ViewNotResolved
 //            }
 //            if (beforePersistance != null && !beforePersistance(model, session))
 //            {
 //                return Close();
 //            }
 //            if (typeof(TModel).GetInterface("IDboFollow") == typeof(IDboFollow))
 //            {
 //                model.SetPropertyValue("CreatedAt", DateTime.Now);
 //                model.SetPropertyValue("CreatedBy", CurrentAccountNo);
 //            }
 //            if (session.Create(model))
 //            {
 //                if (afterPersistance == null || afterPersistance(model, session))
 //                {
 //                    session.Commit();
 //                    FlashSuccess("{0}创建成功", SchemaHelper.GetObjectName<TModel>());
 //                    return Close();
 //                }
 //            }
 //            session.Rollback();
 //            FlashFailure("创建{0}失败!", SchemaHelper.GetObjectName<TModel>());
 //            return View(model);
 //        }
 //        catch (Exception ex)
 //        {
 //            session.Rollback();
 //            FlashError("发生错误:{0}", ex.Message);
 //            return View();
 //        }
 //    }
 //}
 //#endregion
 protected void CreateLog(string result = null, string description = null)
 {
     using (var session = new SessionFactory().OpenSession())
     {
         var log = new ActionLog
                       {
                           Controller = ControllerName,
                           Action = ActionName,
                           ModelId = ModelId,
                           UserId = CurrentAccountId,
                           Url = Url,
                           CreatedAt = DateTime.Now,
                           CreatedBy = "SYSTEM",
                           Address = Request.UserHostAddress,
                           Actor = CurrentAccountNo,
                           Category = GetCategory(),
                           Title = GetTitle(),
                           Result = result,
                           Description = description
                       };
         if (!session.Create(log))
         {
             Log.Error("记录日志失败:{0}", log.ToString());
         }
     }
 }