软测单独项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

439 lines
17 KiB

using FangYar.BLL.TBL;
using FangYar.Common;
using FangYar.Model.TBL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
namespace FangYar.BLL
{
/// <summary>
/// 系统操作记录添加到系统日志
/// </summary>
public class SysOperationLogHelp
{
/// <summary>
/// 管理员账号数据库连接字符串
/// </summary>
private static string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlConnStrLog"].ConnectionString;
/// <summary>
/// 设置是否记录操作日志到Kafka
/// </summary>
public static string IsKafkaLog = System.Configuration.ConfigurationManager.AppSettings["IsKafkaLog"] + "";
/// <summary>
/// 系统日志记录
/// </summary>
private static SysOperationLogBLL syslogBll = new SysOperationLogBLL();
/// <summary>
/// 设置是否记录操作日志:0、记录;其他、不记录
/// </summary>
public static string IsOperationLogDebug = System.Configuration.ConfigurationManager.AppSettings["IsOperationLogDebug"] + "";
/// <summary>
/// 用户操作日志记录
/// </summary>
/// <param name="context">请求信息HttpContext对象</param>
/// <param name="logType">日志类型</param>
/// <param name="Info_Name">信息名称</param>
/// <param name="Info_Msg">信息内容</param>
/// <param name="ClientType">日志平台类型:0、营区平台;1、战训;</param>
public static void AddSysOperationLog(HttpContext context, EnumOperationLogType logType, string Info_Name, string Info_Msg, string ClientType = "0")
{
// 将线程丢进线程池,参数是线程要做的事情
ThreadPool.QueueUserWorkItem(t =>
{
//new Thread(() =>
//{
try
{
using (var ms = new System.IO.MemoryStream())
{
//判断如果信息内容为空则不记录
if (string.IsNullOrWhiteSpace(Info_Msg))
{
return;
}
//判断是否需要记录日志
if (!IsOperationLogDebug.Equals("0"))
{
return;
}
Tbl_Sys_Operation_Log_Model addMo = GetLogMoByHttpContext(context);
//Tbl_Sys_Operation_Log_Model addMo = GetLogMoByHttpContext(HttpContext.Current);
addMo.Info_Type = ((int)logType) + "";
addMo.Info_Msg = Info_Msg;
addMo.Info_Name = Info_Name;
if (string.IsNullOrWhiteSpace(addMo.Url_Str))
{
string a = "";
}
addMo.Headers_Str1 = ClientType;
syslogBll.Add(addMo, connStr);
//添加到ClickHouse数据库
//ClickHouseHelper.AddSysOperationLog(addMo);
if (!IsKafkaLog.Equals("0"))
{
return;
}
//添加到Kafka后添加到Click数据库
KafkaHelper.SendKafKaMsg(addMo);
}
}
catch (Exception ex)
{
string str = "系统日志记录异常:" + ex;
FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo()
{
message = str,
msgType = FangYar.Common.EnumLogMsgTypeEnum.Error,
path = "SysOperationLog"
});
}
//}).Start();
});
}
private static Tbl_Sys_Operation_Log_Model GetLogMoByHttpContext(HttpContext context)
{
Tbl_Sys_Operation_Log_Model retMo = new Tbl_Sys_Operation_Log_Model();
try
{
var Request = context.Request;
var cookUserMo = GetCookUserByHttpContext(context);
retMo = new Tbl_Sys_Operation_Log_Model();
retMo.ID = "";
retMo.Info_Msg = "";
retMo.Info_Name = "";
retMo.createtime = "";
retMo.Info_Type = null;
try
{
retMo.Url_Form = GetFormStr(context);
}
catch (Exception ex)
{
string str = "系统日志记录HttpContext获取Form参数信息异常:" + ex;
FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo() { message = str, msgType = FangYar.Common.EnumLogMsgTypeEnum.Error, path = "SysOperationLog" });
}
try
{
retMo.User_Uid = cookUserMo.usersUid;
}
catch (Exception ex)
{
string str = "系统日志记录HttpContext获取登录用户账号异常:" + ex;
FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo() { message = str, msgType = FangYar.Common.EnumLogMsgTypeEnum.Error, path = "SysOperationLog" });
}
try
{
retMo.Browser = Request.Browser.Browser;
retMo.Browser_Type = Request.Browser.Type;
retMo.System_Type = Request.Browser.Platform;
retMo.Browser_VerSion = Request.Browser.Version;
}
catch (Exception ex)
{
string str = "系统日志记录HttpContext获取客户端浏览器异常:" + ex;
FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo() { message = str, msgType = FangYar.Common.EnumLogMsgTypeEnum.Error, path = "SysOperationLog" });
}
//获取用户IC
retMo.Client_IP = GetClientIp(context);
//获取用户端UUID
retMo.UUID_Str = GetUUID(context);
try
{
retMo.Client_Name = Request.UserHostName;
}
catch (Exception ex)
{
string str = "系统日志记录HttpContext获取用户IP信息异常:" + ex;
FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo() { message = str, msgType = FangYar.Common.EnumLogMsgTypeEnum.Error, path = "SysOperationLog" });
}
try
{
retMo.Url_Str = Request.Url.AbsoluteUri;
}
catch (Exception ex)
{
string str = "系统日志记录HttpContext获取请求路径信息异常:" + ex;
FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo()
{
message = str,
msgType = FangYar.Common.EnumLogMsgTypeEnum.Error,
path = "SysOperationLog"
});
}
try
{
string Headers_Str = Request.ServerVariables["All_Http"];
if (Headers_Str.IndexOf(".SessionCookie=") > 0)
{
string Headers_StrHead = Headers_Str.Substring(0, Headers_Str.IndexOf(".SessionCookie="));
string Headers_StrEnd = Headers_Str.Substring(Headers_Str.IndexOf("HTTP_HOST:"));
Headers_Str = Headers_StrHead + Headers_StrEnd;
retMo.Headers_Str = Headers_Str;
}
else
{
retMo.Headers_Str = Headers_Str;
}
}
catch (Exception ex)
{
string str = "系统日志记录HttpContext获取Header信息异常:" + ex;
FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo()
{
message = str,
msgType = FangYar.Common.EnumLogMsgTypeEnum.Error,
path = "SysOperationLog"
});
}
//string Headers_Str1 = Request.ServerVariables["All_Raw"];
//string Headers_Str1Head = Headers_Str1.Substring(0, Headers_Str1.IndexOf(".SessionCookie"));
//string Headers_Str1End = Headers_Str1.Substring(Headers_Str1.IndexOf("Host:"));
//Headers_Str1 = Headers_Str1Head + Headers_Str1End;
//retMo.Headers_Str1 = Headers_Str1;
retMo.Action_Str = context.Request.Params["Action"] + "";
retMo.Url_Parameter = Request.Url.Query;
retMo.Browser_System_Str = Request.ServerVariables["HTTP_USER_AGENT"];
retMo.Org_ID = cookUserMo.orgId;
retMo.Org_Name = cookUserMo.orgName;
retMo.roles = cookUserMo.roles;
retMo.UserMobile = cookUserMo.userMobile;
retMo.User_Name = cookUserMo.usersName;
retMo.Post_Parameter = GetPostParam(Request);
}
catch (Exception ex)
{
string str = "系统日志记录HttpContext转换异常:" + ex;
FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo()
{
message = str,
msgType = FangYar.Common.EnumLogMsgTypeEnum.Error,
path = "SysOperationLog"
});
}
return retMo;
}
private static string GetPostParam(HttpRequest Request)
{
string postParam = ""; //post的所有参数
try
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(Request.InputStream))
{
postParam = sr.ReadLine();
}
}
catch (Exception ex)
{
string str = "系统日志记录获取Post参数异常:" + ex;
FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo()
{
message = str,
msgType = FangYar.Common.EnumLogMsgTypeEnum.Error,
path = "SysOperationLog"
});
}
return postParam;
}
/// <summary>
/// 返回客户端IP
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
private static string GetClientIp(HttpContext context)
{
string retStr = "";
try
{
string ipStr = context.Request.ServerVariables["All_Http"];
if (ipStr.IndexOf("HTTP_X_ORIGINAL_FORWARDED_FOR:") < 0)
{
if (ipStr.IndexOf("HTTP_X_FORWARDED_FOR:") < 0)
{
if (ipStr.IndexOf("HTTP_X_CLIENT_IP:") < 0)
{
return "未获取";
}
else
{
ipStr = ipStr.Substring(ipStr.IndexOf("HTTP_X_CLIENT_IP:") + 17);
retStr = ipStr.Substring(0, ipStr.IndexOf("HTTP_"));
}
}
else
{
ipStr = ipStr.Substring(ipStr.IndexOf("HTTP_X_FORWARDED_FOR:") + 21);
retStr = ipStr.Substring(0, ipStr.IndexOf("HTTP_"));
}
}
else
{
ipStr = ipStr.Substring(ipStr.IndexOf("HTTP_X_ORIGINAL_FORWARDED_FOR:") + 30);
retStr = ipStr.Substring(0, ipStr.IndexOf("HTTP_"));
}
}
catch (Exception ex)
{
string str = "系统日志记录HttpContext获取用户IP信息异常:" + ex;
FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo() { message = str, msgType = FangYar.Common.EnumLogMsgTypeEnum.Error, path = "SysOperationLog" });
}
return retStr;
}
/// <summary>
/// 返回客户端UUID
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
private static string GetUUID(HttpContext context)
{
string retStr = "";
try
{
string ipStr = context.Request.ServerVariables["All_Http"];
if (ipStr.IndexOf("HTTP_X5_UUID:") < 0)
{
return "未获取";
}
retStr = ipStr.Substring(ipStr.IndexOf("HTTP_X5_UUID:") + 13);
}
catch (Exception ex)
{
string str = "系统日志记录HttpContext获取客户端UUID异常:" + ex;
FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo() { message = str, msgType = FangYar.Common.EnumLogMsgTypeEnum.Error, path = "SysOperationLog" });
}
return retStr;
}
/// <summary>
/// 获取Cook缓存信息
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
private static CookUserMo GetCookUserByHttpContext(HttpContext context)
{
CookUserMo retMo = new CookUserMo();
try
{
//if (context.Request.Cookies["kn_root_cookie"] == null)
//{
// return new CookUserMo() { cityId = "", deptId = "", deptName = "", orgId = "", orgName = "", roles = "", userMobile = "", usersName = "", usersUid = "" };
//}
//string CookStr = context.Request.Cookies["kn_root_cookie"].Value + "";
string CookStr = context.Request.ServerVariables["All_Http"];
if (CookStr.IndexOf("kn_root_cookie=") < 0)
{
return new CookUserMo() { cityId = "", deptId = "", deptName = "", orgId = "", orgName = "", roles = "", userMobile = "", usersName = "", usersUid = "" };
}
CookStr = CookStr.Substring(CookStr.IndexOf("kn_root_cookie=") + 15);
CookStr = CookStr.Substring(0, CookStr.IndexOf("}") + 1);
//CookStr = CookStr.Replace("kn_root_cookie=", "");
retMo = JsonHelper.FromJSON<CookUserMo>(CookStr);
}
catch (Exception ex)
{
retMo = new CookUserMo() { cityId = "", deptId = "", deptName = "", orgId = "", orgName = "", roles = "", userMobile = "", usersName = "", usersUid = "" };
string str = "系统日志记录获取Cook信息异常:" + ex;
FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo()
{
message = str,
msgType = FangYar.Common.EnumLogMsgTypeEnum.Error,
path = "SysOperationLog"
});
}
return retMo;
}
private static string GetFormStr(HttpContext context)
{
string retStr = "";
try
{
foreach (var item in context.Request.Params)
{
try
{
if (item.Equals(".SessionCookie"))
{
continue;
}
if (item.Equals("ALL_HTTP"))
{
continue;
}
if (item.Equals("ALL_RAW"))
{
continue;
}
if (item.Equals("HTTP_COOKIE"))
{
continue;
}
if (item.Equals("ASP.NET_SessionId"))
{
continue;
}
string a = "";
if (!string.IsNullOrWhiteSpace(retStr))
{
retStr += "&";
}
// retStr += item + "=" + GetParams(context.Request.Params[item + ""] + "");
retStr += item + "=" + context.Request.Params[item + ""];
}
catch (Exception ex) { }
}
}
catch (Exception ex)
{
string str = "系统日志记录后去Form参数异常:" + ex;
FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo()
{
message = str,
msgType = FangYar.Common.EnumLogMsgTypeEnum.Error,
path = "SysOperationLog"
});
}
return retStr;
}
}
}