/// <summary> /// 新增 /// </summary> /// <param name="logs">日志实体</param> public static void Add(IEnumerable <LoggingModel> logs) { var now = DateTime.Now; //当前缓存的日志 foreach (var log in logs) { CurrentCacheLog.Enqueue(log); } //满足缓存条数写入 或 满足写入时间间隔 if (CurrentCacheLog.Count > CacheWriteCount || (now - CurrentCacheWriteTime).TotalSeconds > CacheWriteSecond) { //写入日志 System.Threading.ThreadPool.QueueUserWorkItem(_ => { var listMo = new List <LoggingModel>(); while (CurrentCacheLog.Count > 0) { listMo.Add(CurrentCacheLog.Dequeue()); } AddNow(listMo); CurrentCacheWriteTime = now; }); } }
public override void OnActionExecuting(ActionExecutingContext context) { var hc = context.HttpContext; string controller = context.RouteData.Values["controller"].ToString().ToLower(); string action = context.RouteData.Values["action"].ToString().ToLower(); var ca = "/" + controller + "/" + action; string url = hc.Request.Path.ToString() + hc.Request.QueryString.Value; //用户信息 var userinfo = Application.CommonService.GetLoginUserInfo(hc); //角色有权限访问配置的菜单 if (!Application.CommonService.QueryMenuIsAuth(userinfo.RoleId, ca)) { context.Result = new ContentResult() { Content = "unauthorized", StatusCode = 401 }; } //日志记录,设置“__nolog”参数可忽略日志记录,为压力测试等环境考虑(即一些不需要记录请求日志的需求) if (GlobalTo.GetValue <bool>("logs:enable") && string.IsNullOrWhiteSpace(hc.Request.Query["__nolog"].ToString())) { try { //客户端信息 var ct = new ClientTo(hc); //日志保存 var mo = new Domain.SysLog() { SuName = userinfo.UserName, SuNickname = userinfo.Nickname, LogAction = ca, LogUrl = url, LogIp = ct.IPv4, LogUserAgent = ct.UserAgent, LogCreateTime = DateTime.Now, LogGroup = 1, LogLevel = "I" }; if (DicDescription.ContainsKey(ca)) { mo.LogContent = DicDescription[ca]; } #region 分批写入日志 CurrentCacheLog.Enqueue(mo); //分批写入满足的条件:缓存的日志数量 int cacheLogCount = GlobalTo.GetValue <int>("logs:CacheWriteCount"); //分批写入满足的条件:缓存的时长,单位秒 int cacheLogTime = GlobalTo.GetValue <int>("logs:CacheWriteSecond"); //上次写入的时间 var cacheLogWriteKey = "Global_Logs_Write"; var cacheLogWrite = Core.CacheTo.Get(cacheLogWriteKey) as DateTime?; if (!cacheLogWrite.HasValue) { Core.CacheTo.Set(cacheLogWriteKey, cacheLogWrite = DateTime.Now); } if (CurrentCacheLog.Count > cacheLogCount || DateTime.Now.ToTimestamp() - cacheLogWrite.Value.ToTimestamp() > cacheLogTime) { //异步写入日志 System.Threading.ThreadPool.QueueUserWorkItem(_ => { try { var ipto = new IPAreaTo(); //写入日志前 var listMo = new List <Domain.SysLog>(); while (CurrentCacheLog.Count > 0) { var log = CurrentCacheLog.Dequeue(); log.LogId = Core.UniqueTo.LongId().ToString(); log.LogArea = ipto.Parse(log.LogIp); var uato = new UserAgentTo(log.LogUserAgent); log.LogBrowserName = uato.BrowserName + " " + uato.BrowserVersion; log.LogSystemName = uato.SystemName + " " + uato.SystemVersion; if (uato.IsBot) { log.LogGroup = 2; } listMo.Add(log); } using var db = Data.ContextBaseFactory.CreateDbContext(); db.SysLog.AddRange(listMo); db.SaveChanges(); //更新时间 Core.CacheTo.Set(cacheLogWriteKey, cacheLogWrite = DateTime.Now); } catch (Exception ex) { Console.WriteLine("写入日志出错:" + ex.Message); } }); } #endregion } catch (Exception ex) { Console.WriteLine("写入日志出错:" + ex.Message); } } base.OnActionExecuting(context); }