using SqlSugar; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using WMS.BZModels.Dto.UserCenterManager.UserDeptDtos; using WMS.BZModels; using WMS.BZModels.Models.UserCenterManager; using WMS.Info; using WMS.BZSqlSugar; using WMS.Util; using WMS.BZModels.Dto.UserCenterManager.SysLogDtos; namespace WMS.BZServices.UserCenterManager { public class SysLogService { private static readonly Repository _sysLogRepository=new Repository (); //public SysLogService(Repository sysLogRepository) //{ // _sysLogRepository = sysLogRepository; //} public static void WriteLog(BZBLLExecInfo BLLExec, ELogType eLogType) { try { SysLog log = new SysLog(); log.RunMs = (int)CommonUtil.TimerEnd(BLLExec.RunWatch); log.LogTime = DateTime.Now; log.AppTypeNum = 0; log.TypeNum = (int)eLogType; if (BLLExec.Ex == null) { log.ContentText = string.IsNullOrWhiteSpace(BLLExec.BLLObj.SuccessMsg) ? "操作成功。" : BLLExec.BLLObj.SuccessMsg; log.IsSucc = 1; } else { log.ContentText = BLLExec.Ex.Message; log.IsSucc = 0; } if (BLLExec.BLLObj != null) { log.AppTypeNum = (int)BLLExec.BLLObj.LoginUser.AppType; log.Method = BZBLLCore.GetBLLClassNo(BLLExec.BLLObj); log.MethodText = BLLExec.BLLObj.BLLDesc; StringBuilder sb = new StringBuilder(); sb.Append("---------------------------------------请求数据-----------------------------------------------------------------"); sb.AppendLine(BLLExec.BLLObj.InJsonData); if (BLLExec.Ex != null) { sb.AppendLine("----------------------------------------异常数据----------------------------------------------------------------"); sb.AppendLine(BLLExec.Ex.ToString()); sb.AppendLine("----------------------------------------异常内部数据----------------------------------------------------------------"); if (BLLExec.Ex.InnerException == null) { sb.AppendLine(string.Format("Message:{0} Source:{1} StackTrace:{2} ", BLLExec.Ex.Message, BLLExec.Ex.Source, BLLExec.Ex.StackTrace)); } else { sb.AppendLine(string.Format("Message:{0} Source:{1} StackTrace:{2} ", BLLExec.Ex.InnerException.Message, BLLExec.Ex.InnerException.Source, BLLExec.Ex.InnerException.StackTrace)); } } log.ParamJson = sb.ToString(); log.Ip = BLLExec.BLLObj.LoginUser.IPAddress; log.LogWho = BLLExec.BLLObj.LoginUser.UserNo; //log. = BLLExec.BLLObj.LoginUser.AppDeviceNo; } _sysLogRepository.Insert(log); } catch (Exception ex) { LogFactory.GetLogger("WMS.Core.SysLogCore.WriteLog").Error(ex.ToString()); } } public static void WriteLog(LoginUserInfo LoginUser, string ModuleNo, string ModuleName, string UrlAddress) { try { SysLog log = new SysLog(); log.RunMs = 0; log.LogTime = DateTime.Now; log.AppTypeNum = (int)LoginUser.AppType; log.TypeNum = (int)ELogType.Visit; log.ContentText = string.Format("{0}[{1},{2}]", ModuleNo, ModuleName, UrlAddress); log.IsSucc = 1; log.Ip = LoginUser.IPAddress; log.Method = "VisitModule"; log.MethodText = "模块访问"; //log.F_APPDEVICENO = LoginUser.AppDeviceNo; log.LogWho = LoginUser.UserNo; var d = new { ModuleNo, ModuleName, UrlAddress, }; log.ParamJson = d.ToJson(); _sysLogRepository.Insert(log); } catch (Exception ex) { LogFactory.GetLogger("WMS.Core.SysLogCore.WriteLog").Error(ex.ToString()); } } public static void WriteLog(LoginUserInfo LoginUser, dynamic JsonData, string ClassNo, string ClassDesc, Stopwatch ts, ELogType eLogType, Exception ex) { try { string injson = JsonData == null ? "" : JsonData.ToJson(); SysLog log = new SysLog(); log.RunMs = (int)CommonUtil.TimerEnd(ts); log.LogTime = DateTime.Now; log.AppTypeNum = (int)LoginUser.AppType; log.TypeNum = (int)eLogType; log.ContentText = ex == null ? "操作成功。" : ex.Message; log.IsSucc = ex == null ? 1 : 0; log.Ip = LoginUser.IPAddress; log.Method = ClassNo; log.MethodText = ClassDesc; //log.F_APPDEVICENO = LoginUser.AppDeviceNo; log.LogWho = LoginUser.UserNo; log.ParamJson = injson; _sysLogRepository.Insert(log); } catch (Exception e) { CommonUtil.TimerEnd(ts); LogFactory.GetLogger("WMS.Core.SysLogCore.WriteLog").Error(e.ToString()); } } public static void WriteLog(LoginUserInfo LoginUser, string ClassDesc, dynamic JsonData, Action action) { Stopwatch ts = CommonUtil.TimerStart(); StackTrace ss = new StackTrace(true); //index:0为本身的方法;1为调用方法;2为其上上层,依次类推 MethodBase mb = ss.GetFrame(1).GetMethod(); string ClassNo = mb.ReflectedType.FullName + "." + mb.Name; try { action.Invoke(); WriteLog(LoginUser, JsonData, ClassNo, ClassDesc, ts, ELogType.Work, null); } catch (Exception ex) { WriteLog(LoginUser, JsonData, ClassNo, ClassDesc, ts, ELogType.Work, ex); throw ex; } } /// /// 清空日志 /// /// 日志分类Id /// 保留时间段内 public static void RemoveLog(int LogType, string keepTime) { try { DateTime operateTime = DateTime.Now; if (keepTime == "7")//保留近一周 { operateTime = DateTime.Now.AddDays(-7); } else if (keepTime == "1")//保留近一个月 { operateTime = DateTime.Now.AddMonths(-1); } else if (keepTime == "3")//保留近三个月 { operateTime = DateTime.Now.AddMonths(-3); } _sysLogRepository.Deleteable().Where(it => it.LogTime <= operateTime && it.TypeNum == LogType).ExecuteCommand(); } catch (Exception ex) { throw ex; } } public PagedInfo GetPageList(Pagination pagination, SysLogQueryDto sysLogQueryDto) { var predicate = Expressionable.Create(); // predicate = predicate.AndIF(sysLogQueryDto != null && !string.IsNullOrEmpty(billDocsinfoQueryDto?.KeyWord), m => m.Code.Contains(billDocsinfoQueryDto.KeyWord) || m.Name.Contains(billDocsinfoQueryDto.KeyWord)); var list = _sysLogRepository.Queryable().Where(predicate.ToExpression()) .ToPage(pagination); return list; } } }