Files
number_zj/20220330_Vote/Ewide.RoadFlow/Data/Log/Log.cs
2022-03-30 17:54:33 +08:00

209 lines
7.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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<RoadFlow.Model.rf_log>,ILog, ITransient
{
private readonly IRepository<SysUser> _sysUserRep =Furion.App.GetService<IRepository<SysUser>>();
private readonly IUserManager _userManager=Furion.App.GetService<IUserManager>();
/// <summary>
/// 添加日志
/// </summary>
/// <param name="title"></param>
/// <param name="contents"></param>
/// <param name="type"></param>
/// <param name="oldContents"></param>
/// <param name="newContents"></param>
/// <param name="others"></param>
/// <param name="browseAgent"></param>
/// <param name="ipAddress"></param>
/// <param name="url"></param>
/// <param name="userId"></param>
/// <param name="userName"></param>
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);
}
/// <summary>
/// 添加日志
/// </summary>
/// <param name="title"></param>
/// <param name="contents"></param>
/// <param name="type"></param>
/// <param name="oldContents"></param>
/// <param name="newContents"></param>
/// <param name="others"></param>
/// <param name="browseAgent"></param>
/// <param name="ipAddress"></param>
/// <param name="url"></param>
/// <param name="userId"></param>
/// <param name="userName"></param>
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);
}
/// <summary>
/// 添加日志
/// </summary>
/// <param name="log"></param>
private void AddLog(Model.rf_log log)
{
log.Type = GetLogType(log.Type, Config.Language_Default);
this.Add(log);
}
/// <summary>
/// 得到日志类型
/// </summary>
/// <param name="type">日志类型</param>
/// <param name="language">语言</param>
/// <returns></returns>
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<string, string> ZH_Type = new Dictionary<string, string>
{
{"用户登录" ,"用戶登錄"},
{"系统管理" ,"系統管理"},
{"流程管理" ,"流程管理"},
{"表单管理" ,"表單管理"},
{"流程运行" ,"流程運行"},
{"系统异常" ,"系統異常"},
{"其他" ,"其他"},
};
private static readonly Dictionary<string, string> EN_US_Type = new Dictionary<string, string>
{
{"用户登录" ,"User login"},
{"系统管理" ,"System management"},
{"流程管理" ,"Workflow management"},
{"表单管理" ,"Form management"},
{"流程运行" ,"Workflow run"},
{"系统异常" ,"Exception"},
{"其他" ,"Other"},
};
/// <summary>
/// 添加异常日志
/// </summary>
/// <param name="err">异常类</param>
/// <param name="title">标题 如果为空用err.Message</param>
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);
}
}
}