123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- 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<SysLog> _sysLogRepository=new Repository<SysLog> ();
- //public SysLogService(Repository<SysLog> 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;
- }
- }
- /// <summary>
- /// 清空日志
- /// </summary>
- /// <param name="categoryId">日志分类Id</param>
- /// <param name="keepTime">保留时间段内</param>
- 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<SysLogDto> GetPageList(Pagination pagination, SysLogQueryDto sysLogQueryDto)
- {
- var predicate = Expressionable.Create<SysLog>();
- // 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<SysLog, SysLogDto>(pagination);
- return list;
- }
- }
- }
|