예제 #1
0
        /// <summary>
        /// 记录异常情况
        /// </summary>
        /// <param name="dbHelper">数据库连接</param>
        /// <param name="userInfo">用户</param>
        /// <param name="Exception">异常</param>
        /// <returns>主键</returns>
        public static string LogException(IDbHelper dbHelper, BaseUserInfo userInfo, Exception ex)
        {
            // 在控制台需要输出错误信息
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write(ex.InnerException);
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(string.Empty);

            string returnValue = string.Empty;

            // 系统里应该可以配置是否记录异常现象
            if (!BaseSystemInfo.LogException)
            {
                return(returnValue);
            }
            // Windows系统异常中
            if (BaseSystemInfo.EventLog)
            {
                if (!System.Diagnostics.EventLog.SourceExists(BaseSystemInfo.SoftName))
                {
                    System.Diagnostics.EventLog.CreateEventSource(BaseSystemInfo.SoftName, BaseSystemInfo.SoftFullName);
                }
                System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog();
                eventLog.Source = BaseSystemInfo.SoftName;
                eventLog.WriteEntry(ex.Message, EventLogEntryType.Error);
            }
            // 判断一下数据库是否打开状态,若数据库都没能打开,还记录啥错误,不是又抛出另一个错误了?
            if (dbHelper != null && dbHelper.GetDbConnection() != null)
            {
                if (dbHelper.GetDbConnection().State == ConnectionState.Open)
                {
                    BaseExceptionManager exceptionManager = new BaseExceptionManager(dbHelper, userInfo);
                    returnValue = exceptionManager.AddEntity(ex);
                }
            }
            return(returnValue);
        }
        /// <summary>
        /// 记录异常情况
        /// </summary>
        /// <param name="dbHelper">数据库连接</param>
        /// <param name="userInfo">用户</param>
        /// <param name="ex"></param>
        /// <param name="url"></param>
        /// <returns>主键</returns>
        public static string LogException(IDbHelper dbHelper, BaseUserInfo userInfo, Exception ex, string url = null)
        {
            // 在控制台需要输出错误信息
#if (DEBUG)
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write(ex.InnerException);
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(string.Empty);
#endif

            var result = string.Empty;
            // 系统里应该可以配置是否记录异常现象
            if (!BaseSystemInfo.LogException)
            {
                return(result);
            }
            // Windows系统异常中
            if (BaseSystemInfo.EventLog)
            {
                if (!EventLog.SourceExists(BaseSystemInfo.SoftName))
                {
                    EventLog.CreateEventSource(BaseSystemInfo.SoftName, BaseSystemInfo.SoftFullName);
                }
                var eventLog = new EventLog
                {
                    Source = BaseSystemInfo.SoftName
                };
                eventLog.WriteEntry(ex.Message, EventLogEntryType.Error);
            }

            //判断一下数据库是否打开状态,若数据库都没能打开,还记录啥错误,不是又抛出另一个错误了?
            var exceptionManager = new BaseExceptionManager(dbHelper, userInfo);
            result = exceptionManager.AddEntity(ex, url);

            return(result);
        }