예제 #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)
        {
            // 在控制台需要输出错误信息
            #if (DEBUG)
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write(ex.InnerException);
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(string.Empty);
            #endif

            string result = string.Empty;
            // 系统里应该可以配置是否记录异常现象
            if (!BaseSystemInfo.LogException)
            {
                return(result);
            }
            // 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);
            }

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

            return(result);
        }