246 lines
12 KiB
C#
246 lines
12 KiB
C#
using Lskj.Core;
|
|
using Lskj.Util;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Xml.Linq;
|
|
|
|
namespace Lskj.Business.Impl
|
|
{
|
|
public sealed class ErrorMessage
|
|
{
|
|
public static string StackTrace = string.Empty;
|
|
public static string Connection1 = "QkYyNjE3QTcxQjc4RDFFMDlERjlFMDc1QkIyMDVGRjdDRUIyMEVEOUM5NjBCQjc5NTA5NzdEQTk0MkNEQTE1RTFGMEIwMThFNUJDQTE0RjE1NjQwM0U5QTYxMThCMDY2MTA4NkE2OTIxNTNBQjQ0MjRDMDIwM0Q5OEVDQjkwQTVCN0RENjJBODkxNUNGOUM5OEI5RTEzN0E3ODBERjg2OURCNEQ5QTlCMzhCQUZGQ0FBNUREMjY4M0FCODg2RTkxMDRERTQ3MjRGQTE1MERDOTJGRDFDRjY2REQ1ODYyREUzN0RGM0ZBQzc4NzJENjIwNDg4RUUyNkI3ODE1RjlDM0I0MjNCQzcyOENDN0VDMTg0QUU0MjgwMDVDMENBOUZF";
|
|
/// <summary>
|
|
/// 远程数据库名
|
|
/// </summary>
|
|
public static string DataBase = "lserp";
|
|
/// <summary>
|
|
/// 远程服务器名
|
|
/// </summary>
|
|
private static string serverName = "114.116.135.188,16678";
|
|
|
|
/// <summary>
|
|
/// <para>说明:返回报错的提示信息</para>
|
|
/// <para>创建人:曹屹峰</para>
|
|
/// <para>创建日期:2021-8-10 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <returns>DataTable.</returns>
|
|
public static string PromptErrorMessage(Exception ex, string ModlecCode = "", string PromptMessage = "")
|
|
{
|
|
string ReturnInformation = "";//返回信息
|
|
//让数据库可以跨数据库查询,添加
|
|
// BaseImpl.ExecSqlValue("exec sp_configure 'show advanced options',1 reconfigure exec sp_configure 'Ad Hoc Distributed Queries',1 reconfigure");
|
|
string connStr = GetConnection();
|
|
string theDatabase = "p_ErrMessageCpt";
|
|
ReturnInformation = "配置错误,请联系管理员";
|
|
|
|
string Message = string.IsNullOrWhiteSpace(PromptMessage) ? ex.Message : PromptMessage + "\r\n" + ex.Message;
|
|
|
|
DataTable dataTable = new DataTable();
|
|
try
|
|
{
|
|
//dataTable= SqlHelper.ExecuteDataTable(string.Format("select * from {0}", string.Format("OPENDATASOURCE( 'SQLOLEDB ', '{0}').lserp.dbo.p_cpeMethod", connStr)));
|
|
dataTable = null;
|
|
}
|
|
catch
|
|
{
|
|
dataTable = null;
|
|
}
|
|
|
|
var stackFrames = new StackTrace(ex).GetFrames() ?? new StackFrame[0];
|
|
string Fdll = "";
|
|
string ErrorInfo = "";
|
|
string stackTrace = "";//堆栈跟踪
|
|
if (stackFrames.Select(x => x.GetMethod()).Where(m => m.Module.Name.Contains("Lskj")).Count() > 0)
|
|
{
|
|
for (int i = stackFrames.Select(x => x.GetMethod()).Count(); i > 0; i--)
|
|
{
|
|
string MethodExplainStr = "";
|
|
if (stackFrames[i - 1].GetMethod().Module.Name.Contains("Lskj"))
|
|
{
|
|
string Fname = stackFrames[i - 1].GetMethod().GetType().GetProperty("FullName", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(stackFrames[i - 1].GetMethod(), null).ToString().Replace("()", "").Replace(" ", "");//方法名
|
|
//string FModelName = stackFrames[i - 1].GetMethod().DeclaringType.FullName;//dll+控件名
|
|
MethodExplainStr = GetMethodExplain(Fname, dataTable);
|
|
if (string.IsNullOrEmpty(MethodExplainStr))
|
|
ErrorInfo += Fname + "--";//拼接主键
|
|
else
|
|
ErrorInfo += MethodExplainStr + "--";//拼接主键
|
|
}
|
|
if (string.IsNullOrEmpty(MethodExplainStr))
|
|
stackTrace = stackFrames[i - 1].GetMethod().DeclaringType.FullName + "." + stackFrames[i - 1].GetMethod().Name + "--" + stackTrace;
|
|
else
|
|
stackTrace = MethodExplainStr + "--" + stackTrace;
|
|
}
|
|
Fdll = stackFrames.Select(x => x.GetMethod()).Where(m => m.Module.Name.Contains("Lskj")).Last().Module.Name; //dll名
|
|
}
|
|
else
|
|
{
|
|
string Fname = stackFrames.Select(x => x.GetMethod()).Last().Name;//方法名
|
|
string FModelName = stackFrames.Select(x => x.GetMethod()).Last().DeclaringType.FullName;//dll+控件名
|
|
ErrorInfo += FModelName + "." + Fname + "--";//拼接主键
|
|
Fdll = stackFrames.Select(x => x.GetMethod()).Last().Module.Name; ;//dll名
|
|
stackTrace = stackFrames.Last().GetMethod().DeclaringType.FullName + "," + stackFrames.Last().GetMethod().Name;
|
|
}
|
|
stackTrace = stackTrace.TrimEnd('-');
|
|
// StackTrace = Regex.Replace(ex.Message, "'", "''") + "\r\n" + Regex.Replace(stackTrace, "--", "\r\n");
|
|
//报错类型
|
|
string Ftype = ex.GetType().FullName.ToString();
|
|
if (ex.InnerException != null && !string.IsNullOrWhiteSpace(ex.InnerException.GetType().FullName.ToString()))
|
|
{
|
|
Ftype = ex.InnerException.GetType().FullName.ToString();
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(ModlecCode))
|
|
ErrorInfo = string.Format("在{0}时{1}", ErrorInfo.TrimEnd('-'), Regex.Replace(Message, "'", "''"));
|
|
else
|
|
ErrorInfo = string.Format("模块编号{0}在{1}时{2}", ModlecCode, ErrorInfo.TrimEnd('-'), Regex.Replace(Message, "'", "''"));
|
|
//创建对照表
|
|
CreationNoticeTable();
|
|
StackTrace = ErrorInfo;
|
|
//判断是否为数据库方面报错
|
|
if (Ftype.Equals("System.Data.SqlClient.SqlException"))
|
|
{
|
|
DataTable dt = SqlHelper.ExecuteDataTable(string.Format("select * from {1} where P_ErrType='{0}'", Ftype, theDatabase));//对照表
|
|
DataRow rows = dt.Rows.Cast<DataRow>().FirstOrDefault(x => ((Regex.Replace(Message, "'", "''") + " " + stackTrace).Contains(x["p_ErrAccurate"].ToString()) && !string.IsNullOrEmpty(x["p_ErrAccurate"] + "")));
|
|
if (rows != null)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(rows["p_PromptErro"].ToString()))
|
|
{
|
|
ReturnInformation = rows["p_PromptErro"].ToString();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
LogUtil.WriteP_ErrMessageCpt(ErrorInfo, Regex.Replace(Message, "'", "''") + " " + stackTrace, Fdll, Ftype, theDatabase);
|
|
try
|
|
{
|
|
// BaseImpl.GetResult(string.Format(" SELECT * FROM OPENDATASOURCE('SQLOLEDB','{0}').lserp.dbo.p_ErrMessageCpt", connStr));
|
|
// LogUtil.WriteP_ErrMessageCpt(ErrorInfo, Regex.Replace(ex.Message, "'", "''") + " " + stackTrace, Fdll, Ftype, string.Format("OPENDATASOURCE( 'SQLOLEDB ', '{0}').lserp.dbo.p_ErrMessageCpt", connStr));
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
DataTable dt = SqlHelper.ExecuteDataTable(string.Format("select * from {2} where p_DllName='{0}' and P_ErrType='{1}'", Fdll, Ftype, theDatabase));//对照表
|
|
DataRow[] rows = dt.Select("p_ErrId='" + ErrorInfo + "' and p_ErrMsg='" + Regex.Replace(Message, "'", "''") + " " + stackTrace + "'");
|
|
if (rows.Length == 1) //对照表已记录
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(rows[0]["p_PromptErro"].ToString()))
|
|
{
|
|
ReturnInformation = rows[0]["p_PromptErro"].ToString();
|
|
|
|
}
|
|
LogUtil.WriteP_ErrMessageCptTime(rows[0]["Id"].ToString());
|
|
}
|
|
else
|
|
{
|
|
LogUtil.WriteP_ErrMessageCpt(ErrorInfo, Regex.Replace(Message, "'", "''") + " " + stackTrace, Fdll, Ftype, theDatabase);
|
|
try
|
|
{
|
|
// BaseImpl.GetResult(string.Format(" SELECT * FROM OPENDATASOURCE('SQLOLEDB','{0}').lserp.dbo.p_ErrMessageCpt", connStr));
|
|
// LogUtil.WriteP_ErrMessageCpt(ErrorInfo, Regex.Replace(ex.Message, "'", "''") + " " + stackTrace, Fdll, Ftype, string.Format("OPENDATASOURCE( 'SQLOLEDB ', '{0}').lserp.dbo.p_ErrMessageCpt", connStr));
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
}
|
|
}
|
|
//错误日志表
|
|
LogUtil.WriteError(ErrorInfo, ex);
|
|
//关闭跨数据库权限
|
|
// BaseImpl.ExecSqlValue("exec sp_configure 'Ad Hoc Distributed Queries',0 reconfigure exec sp_configure 'show advanced options',0 reconfigure");
|
|
// if (ex.Message.Contains("触发器") && ReturnInformation.Equals("配置错误,请联系管理员")) ReturnInformation = "";
|
|
|
|
//删除存储过程中的表(条件报错不清空)
|
|
if (!PromptMessage.Contains("条件判断错误:"))
|
|
{
|
|
Lskj.Data.Caches.CacheSYSConfig.Instance().empty();
|
|
}
|
|
|
|
|
|
return ReturnInformation;
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>说明:判断数据库中是否存在错误对照表</para>
|
|
/// <para>创建人:王一帆</para>
|
|
/// <para>创建日期:2021-8-10 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <returns>DataTable.</returns>
|
|
public static void CreationNoticeTable()
|
|
{
|
|
if (!Lskj.Business.Impl.BaseImpl.HasExistsTable("p_ErrMessageCpt"))
|
|
{
|
|
string sqlValue = string.Format(@"Create Table p_ErrMessageCpt -- 错误对照表
|
|
(
|
|
Id int identity(1,1) primary key, -- 分类ID,主键,自动增长
|
|
p_ErrId text not null, -- 名字
|
|
p_ErrMsg text not null, -- 系统报错信息
|
|
p_PromptErro varchar(2000) , -- 配置报错信息
|
|
p_DllName varchar(2000) not null, --所在dll
|
|
P_ErrType varchar(2000) not null, --报错类型
|
|
p_ErrAccurate varchar(2000), --精确值
|
|
p_ErrTime datetime not null, --报错时间
|
|
)");
|
|
BaseImpl.ExecSqlValue(sqlValue);
|
|
//sqlValue = "alter table p_ErrMessageCpt alter column p_ErrId text not null";
|
|
//BaseImpl.ExecSqlValue(sqlValue);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>说明:获取C#连接字符串</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-08-07 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <returns>System.String.</returns>
|
|
public static string GetConnection()
|
|
{
|
|
return string.Format(AESUtil.Decrypt(Connection1), serverName, DataBase);
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:遍历获取方法中文注释</para>
|
|
/// <para>创建人:唐德馨</para>
|
|
/// <para>创建日期:2021-08-13 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <returns>System.String.</returns>
|
|
public static string GetMethodExplain(string methodName, DataTable DataTable)
|
|
{
|
|
DataTable dataTable = DataTable;
|
|
if (dataTable == null || dataTable.Rows.Count == 0) return string.Empty;
|
|
IEnumerable<DataRow> resultRow = dataTable.Select().Where(n => n["p_ErrMethod"].Equals(methodName));
|
|
if (resultRow.Count() != 0)
|
|
return resultRow.First()["p_interpretText"].ToString();
|
|
else
|
|
return string.Empty;
|
|
}
|
|
}
|
|
}
|