/// <summary> /// 错误日志加入队列 /// </summary> /// <param name="msg"></param> /// <param name="ip"></param> public static void InLogQueue_Error(Exception ex, string ip = "") { LogQueueInfo info = new LogQueueInfo(); info.Exception = ex; info.Ip = ip; LogQueue.Enqueue(info); }
/// <summary> /// 记录日志 /// </summary> /// <param name="msg">日志内容</param> public void WriteLog(string msg) { LogQueueInfo queue = new LogQueueInfo(); queue.Message = msg; queue.Ip = GetIP(); queue.IsSaveDB = false; HWebQueue.LogQueue.Enqueue(queue); // LogHelper.WriteLog(msg + ">>ip:" + GetIP()); }
/// <summary> /// 日志加入队列 /// </summary> /// <param name="msg"></param> /// <param name="ip"></param> public static void InLogQueue(string msg, LogLevel level, string ip = "") { LogQueueInfo info = new LogQueueInfo(); info.Message = msg; info.HappenTime = DateTime.Now; info.Ip = ip; info.Level = (int)level; info.IsSaveDB = false; LogQueue.Enqueue(info); }
/// <summary> /// 绑定设备之前先验证设备是符合绑定的条件 /// </summary> /// <param name="user"></param> /// <param name="imei"></param> /// <returns></returns> public static Device preBindDevice(User user, string ip = "1.1.1.1") { Device _d = DeviceLogic.GetDeviceByUserId(user.Id); //验证设备是否存在 if (!string.IsNullOrWhiteSpace(user.IMEI) && _d == null) { //需先调用设备接口验证 DeviceCheckModel dcm = DeviceData.CheckDevice(user.IMEI, user.APIUserId ?? 0); if (dcm.State != 0 && dcm.State != 1105) { // res.State = State.Falid; string msg = dcm.Message; //res.Message = msg; // SaveUserLog(, LogLevel.Error, user.LoginName, "preBindDevice", "绑定该设备"); LogQueueInfo queue = new LogQueueInfo(); queue.IsSaveDB = true; queue.DbLog = new WebLog { Content = user.LoginName + "绑定设备失败:" + msg, Created = DateTime.Now, UserId = user.Id, UserName = user.LoginName, LogName = msg, LogLevel = (int)LogLevel.Sensitive, ClientIp = ip, Method = "preBindDevice" }; HWebQueue.LogQueue.Enqueue(queue); // return Json(res); return(null); } else { //添加设备表 Device d = new Device { UserId = 0, IconId = 3, Created = DateTime.Now, Imei = user.IMEI, Status = (int)Status.Normal, APIDeviceId = dcm.DeviceId, APIDeviceModel = dcm.Model }; DeviceLogic.SaveDevice(d); // SaveUserLog(AuthUser.LoginName + "为" + user.LoginName + "绑定设备:" + user.IMEI, LogLevel.Error, AuthUser.LoginName, "AjaxEditUser", "绑定设备"); return(d); } } return(null); }
/// <summary> /// 开启日志队列 /// </summary> public static void StartLogQueue() { LogQueue = new Queue <LogQueueInfo>(); //通过线程池开启线程,不停的的取队列 ThreadPool.QueueUserWorkItem(p => { while (true) { if (LogQueue.Count > 0) { LogHelper.WriteLog("剩余队列数量:" + LogQueue.Count() + "-----" + DateTime.Now); try { //出队列 LogQueueInfo info = LogQueue.Dequeue(); //错误日志 if (info.Exception != null) { LogHelper.ErrorLog(info.Exception); } //需要存数据的 else if (info.IsSaveDB && info.DbLog != null) { //保存到数据库 int id = WebLogLogic.SaveWebLog(info.DbLog); LogHelper.WriteLog(info.DbLog.UserName + "操作记录id=" + id + "内容:" + info.DbLog.Content + ",IP:" + info.DbLog.ClientIp); } else { LogHelper.WriteLog(info.Message + ">>ip:" + info.Ip); } } catch { throw; } } else { //队列中没有内容,线程休眠1s Thread.Sleep(2000); } } }); }
/// <summary> /// 保存用户操作记录 /// </summary> /// <param name="content">操作内容</param> /// <param name="logLevel">日志等级</param> /// <param name="userName">用户名</param> /// <param name="method">方法</param> protected void SaveUserLog(string content, LogLevel logLevel, string userName, string method, string msg = "") { int userid = AuthUser == null ? 0 : AuthUser.Id; string ip = GetIP(); LogQueueInfo queue = new LogQueueInfo(); queue.IsSaveDB = true; queue.DbLog = new WebLog { Content = content, Created = DateTime.Now, UserId = userid, UserName = userName, LogName = msg, LogLevel = (int)logLevel, ClientIp = ip, Method = method }; HWebQueue.LogQueue.Enqueue(queue); ////保存到数据库 //int id = WebLogLogic.SaveWebLog(log); ////记录日志 //WriteLog(userName + "操作记录id=" + id + "内容:" + content); }