using Ewide.Core; using Furion.DatabaseAccessor; using Furion.DependencyInjection; using RoadFlow.Utility; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RoadFlow.Data { public class Log :RoadFlowRepository,ILog, ITransient { private readonly IRepository _sysUserRep =Furion.App.GetService>(); private readonly IUserManager _userManager=Furion.App.GetService(); /// /// 添加日志 /// /// /// /// /// /// /// /// /// /// /// /// public void Add(string title, string CurrentUserId = "", string contents = "", LogType type = LogType.其他, string oldContents = "", string newContents = "", string others = "", string browseAgent = "", string ipAddress = "", string url = "", string userId = "", string userName = "") { var logModel = new Model.rf_log { Id = GuidExtensions.NewGuid().ToString(), Title = title, Type = type.ToString(), IPAddress = ipAddress.IsNullOrWhiteSpace() ? Tools.GetIP() : ipAddress, URL = url.IsNullOrWhiteSpace() ? Tools.GetAbsoluteURL() : url, WriteTime = DateExtensions.Now, Referer = Tools.GetReferer() }; if (userId.IsGuid(out Guid userGuid)) { logModel.UserId = userGuid.ToString(); } else { try { logModel.UserId = CurrentUserId; } catch { } } if (!userName.IsNullOrWhiteSpace()) { logModel.UserName = userName; } else { try { var userModel = logModel.UserId.IsNullOrWhiteSpace() ?_sysUserRep.DetachedEntities.FirstOrDefault(x=>x.Id==logModel.UserId) : null; if (null != userModel) { logModel.UserName = userModel.Name; } } catch { } } if (!contents.IsNullOrWhiteSpace()) { logModel.Contents = contents; } if (!others.IsNullOrWhiteSpace()) { logModel.Others = others; } if (!oldContents.IsNullOrWhiteSpace()) { logModel.OldContents = oldContents; } if (!newContents.IsNullOrWhiteSpace()) { logModel.NewContents = newContents; } logModel.BrowseAgent = browseAgent.IsNullOrWhiteSpace() ? Tools.GetBrowseAgent() : browseAgent; AddLog(logModel); } /// /// 添加日志 /// /// /// /// /// /// /// /// /// /// /// /// public void Add(string title, string contents = "", LogType type = LogType.其他, string oldContents = "", string newContents = "", string others = "", string browseAgent = "", string ipAddress = "", string url = "", string userId = "", string userName = "") { this.Add(title, _userManager.UserId, contents, type, oldContents, newContents, others, browseAgent, ipAddress, url, userId, userName); } /// /// 添加日志 /// /// private void AddLog(Model.rf_log log) { log.Type = GetLogType(log.Type, Config.Language_Default); this.Add(log); } /// /// 得到日志类型 /// /// 日志类型 /// 语言 /// public string GetLogType(string type, string language) { switch (language) { case "zh-CN": return type; case "zh": return ZH_Type.TryGetValue(type, out string t) ? t : type; case "en-US": return EN_US_Type.TryGetValue(type, out string t2) ? t2 : type; } return type; } private static readonly Dictionary ZH_Type = new Dictionary { {"用户登录" ,"用戶登錄"}, {"系统管理" ,"系統管理"}, {"流程管理" ,"流程管理"}, {"表单管理" ,"表單管理"}, {"流程运行" ,"流程運行"}, {"系统异常" ,"系統異常"}, {"其他" ,"其他"}, }; private static readonly Dictionary EN_US_Type = new Dictionary { {"用户登录" ,"User login"}, {"系统管理" ,"System management"}, {"流程管理" ,"Workflow management"}, {"表单管理" ,"Form management"}, {"流程运行" ,"Workflow run"}, {"系统异常" ,"Exception"}, {"其他" ,"Other"}, }; /// /// 添加异常日志 /// /// 异常类 /// 标题 如果为空用err.Message public void Add(Exception err, string title = "") { Model.rf_log logModel = new Model.rf_log { Id = GuidExtensions.NewGuid().ToString(), Title = title.IsNullOrWhiteSpace() ? err.Message : title, Type = GetLogType("系统异常", Tools.GetCurrentLanguage()), IPAddress = Tools.GetIP(), URL = Tools.GetAbsoluteURL(), WriteTime = DateExtensions.Now, Referer = Tools.GetReferer() }; string uid = _userManager.UserId; /*if (uid.IsNullOrWhiteSpace()) { uid = EnterpriseWeiXin.Common.GetUserId(); }*/ if (uid.IsNotEmptyGuid()) { logModel.UserId = uid; } var userModel = _userManager.User;//logModel.UserId.HasValue ? new RoadFlow.Business.User().Get(logModel.UserId.Value) : null; /*if (null == userModel) { userModel = EnterpriseWeiXin.Common.GetUser(); }*/ if (null != userModel) { logModel.UserName = userModel.Name; } logModel.Contents = err.StackTrace; logModel.Others = err.Source + "(" + err.Message + ")"; logModel.BrowseAgent = Tools.GetBrowseAgent(); AddLog(logModel); } } }