/// <summary> /// 构造函数... /// </summary> /// <param name="clientMsg"></param> /// <param name="msgLever"></param> /// <param name="innerException"></param> public APPException(string clientMsg, APPMessageType msgLever, Exception innerException) : base(clientMsg, innerException) { _MsgLever = msgLever; string msg = clientMsg; if (innerException != null) { string innerMsg = string.Empty; if (innerException is APPException) { APPException appEx = innerException as APPException; _MsgLever = appEx.MsgLever; innerMsg = appEx.Message; } else { try { getErrMessage(innerException, ref innerMsg); } catch { } } TraceEx.Write(innerMsg, APPMessageType.SysErrInfo); if (!string.IsNullOrEmpty(innerException.StackTrace)) { TraceEx.Write(innerException.StackTrace); } } TraceEx.Write(msg, msgLever); }
/// <summary> /// 构造函数... /// </summary> /// <param name="pMsg">抛出异常的信息</param> /// <param name="pMsgLever">异常信息的类型等级</param> public APPException(string clientMsg, APPMessageType msgLever, string errorCode) : base(clientMsg) { _MsgLever = msgLever; _ErrorCode = errorCode; TraceEx.Write(clientMsg, msgLever); }
/// <summary> /// 创建异常。 /// </summary> /// <param name="ex"></param> /// <param name="msgLever"></param> /// <returns></returns> public static APPException PromoteException(Exception ex, APPMessageType defaultMsgLever) { if (ex is APPException) { return(ex as APPException); } else { APPException aex = new APPException(ex.Message, defaultMsgLever, ex); return(aex); } }
/// <summary> /// 根据错误的类型获取对应的错误描述信息。 /// </summary> /// <param name="msgLevel"></param> /// <returns></returns> public static string GetErrorMessageByType(APPMessageType msgLevel) { switch (msgLevel) { case APPMessageType.CodeRunInfo: return("代码执行轨迹"); case APPMessageType.SysDatabaseInfo: return("数据库操作失败"); case APPMessageType.SysErrInfo: return("系统错误"); case APPMessageType.SysFileInfo: return("硬盘文件操作失败"); case APPMessageType.OtherSysInfo: return("代码执行失败"); default: return(string.Empty); } }
/// <summary> /// 构造函数... /// </summary> /// <param name="pMsg"></param> /// <param name="pMsgLever"></param> public APPException(string clientMsg, APPMessageType msgLever) : this(clientMsg, msgLever, "-1") { }
/// <summary> /// 把运行的代码过程记录下来 /// </summary> /// <param name="msgStr"></param> /// <param name="msgLevel"></param> public static void Write(string msgStr, APPMessageType msgLevel) { WriteIf(false, msgStr, msgLevel); }
/// <summary> /// SaveIf /// </summary> /// <param name="condition"> condition :如果要禁止写入消息,那么为true,否则为false</param> /// <param name="msgStr"></param> /// <param name="msgLevel"></param> /// <returns></returns> public static bool SaveIf(bool condition, string msgStr, APPMessageType msgLevel) { if (condition) { return(true); } LogLevel level = LogLevel.None; switch (msgLevel) { case APPMessageType.CodeRunInfo: level = LogLevel.Debug; break; case APPMessageType.DataInvalid: level = LogLevel.Warn; break; case APPMessageType.DisplayToUser: level = LogLevel.Info; break; case APPMessageType.OtherSysInfo: level = LogLevel.Info; break; case APPMessageType.SysDatabaseInfo: level = LogLevel.Info; break; case APPMessageType.SysErrInfo: level = LogLevel.Error; break; case APPMessageType.SysFileInfo: level = LogLevel.Info; break; case APPMessageType.SysWarning: level = LogLevel.Warn; break; default: level = LogLevel.None; break; } if (string.IsNullOrEmpty(msgStr)) { return(true); } if (msgStr.Length > MB.BaseFrame.SOD.L_SINGLE_MAX_LOG_LENGTH) { msgStr = msgStr.Substring(0, MB.BaseFrame.SOD.L_SINGLE_MAX_LOG_LENGTH) + " [[单行日志超长,部分字符已经被截取掉]]"; } //显示给用户看的消息不需要存储 if (msgLevel == APPMessageType.DisplayToUser) { return(true); } //判断是否为记录代码运行的信息,如果是,将根据应用程序的配置来决定是否存储 if (msgLevel == APPMessageType.CodeRunInfo) { var allow = checkAllowWriteRunLog(); if (!allow) { return(true); } } string errTypeStr = GetErrorMessageByType(msgLevel) + "-->"; string requestIP = MB.Util.General.GetRequestIP(); if (!string.IsNullOrEmpty(requestIP)) { requestIP = "(" + requestIP + ")"; } string strLine = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":" + requestIP + errTypeStr + msgStr; lock (_LogsStack) { _LogsStack.Push(strLine); } s_logger.LogAll(level, null, msgStr); return(true); }
/// <summary> /// 根据条件把运行的代码过程记录下来 /// </summary> /// <param name="condition">condition :如果要禁止显示消息,那么为true,否则为false</param> /// <param name="msgStr"></param> /// <param name="msgLevel"></param> public static void WriteIf(bool condition, string msgStr, APPMessageType msgLevel) { SaveIf(condition, msgStr, msgLevel); }