Пример #1
0
        private bool LogSaveAtawChanges()
        {
            try
            {
                if (!IsSaveAtawChanges)
                {
                    IsSaveAtawChanges = true;
                    SaveAtawChanges();
                }
                else
                {
                    return(false);//已经被记录过了
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                //string exx = string.Join("|",
                //    dbEx.EntityValidationErrors.Select(
                //    a => string.Join("-", a.ValidationErrors.Select(b => b.ErrorMessage),"-实体:",a.Entry.Entity.ToString(),"-状态",a.Entry.State.ToString())
                //    ),Environment.NewLine);
                //exx = exx + AtawAppContext.Current.FastJson.ToJSON(dbEx.EntityValidationErrors);
                string exx = this.logDbEx(dbEx);
                AtawTrace.WriteFile(LogType.EfErrot, exx);
                throw dbEx;
            }

            return(true);
        }
Пример #2
0
 public void Flush(LogType logType)
 {
     if (fStringBuilder.Length > 0)
     {
         AtawTrace.WriteFile(logType, fStringBuilder.ToString());
     }
     fStringBuilder = new StringBuilder();
 }
Пример #3
0
 public static T SafeJsonObject<T>(this string str)
 {
     try
     {
         return JsonConvert.DeserializeObject<T>(str);
     }
     catch (Exception ex) {
         AtawTrace.WriteStringFile("JsonConvert",ex.Message);
         return default(T);
     }
 }
Пример #4
0
 public T Get <T>(string key)
 {
     if ((Cache.Contains(key)))
     {
         AtawTrace.WriteFile(LogType.AppCacheHit,
                             "命中注册 : {0} {1} 类型 : {2} {1}".AkFormat(key, Environment.NewLine, typeof(T).Name));
         // return (T)PageItems[regName];
         return((T)Cache[key]);
     }
     else
     {
         return(default(T));
     }
 }
Пример #5
0
        public DataSet QueryDataSet(string sqlString, params SqlParameter[] cmdParms)
        {
            SqlCommand cmd  = new SqlCommand();
            var        conn = this.Database.Connection as SqlConnection;

            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            cmd.Connection  = conn as SqlConnection;
            cmd.CommandText = sqlString;

            cmd.CommandType = CommandType.Text;
            if (cmdParms != null)
            {
                foreach (SqlParameter parm in cmdParms)
                {
                    cmd.Parameters.Add(parm);
                }
            }
            StringBuilder sb = new StringBuilder();

            DBUtil.DbCommandToString(cmd, sb);
            AtawTrace.WriteFile(LogType.PageQuerySql, sb.ToString());
            DataSet ds = new DataSet();

            try
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(ds);
                    cmd.Parameters.Clear();
                }
            }
            catch (Exception ex)
            {
                AtawTrace.WriteFile(LogType.SqlQueryError, sb.ToString());
                throw ex;
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }

            return(ds);
        }
Пример #6
0
        public object QueryObject(string sqlString, params SqlParameter[] cmdParms)

        {
            SqlCommand cmd  = new SqlCommand();
            var        conn = this.Database.Connection as SqlConnection;

            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            cmd.Connection  = conn as SqlConnection;
            cmd.CommandText = sqlString;

            cmd.CommandType = CommandType.Text;



            if (cmdParms != null)
            {
                foreach (SqlParameter parm in cmdParms)
                {
                    cmd.Parameters.Add(parm);
                }
            }


            StringBuilder sb = new StringBuilder();

            DBUtil.DbCommandToString(cmd, sb);
            AtawTrace.WriteFile(LogType.QueryObject, sb.ToString());

            var res = cmd.ExecuteScalar();

            if (conn != null)
            {
                conn.Close();
            }
            return(res);
        }
Пример #7
0
        protected string ReturnJsonObject <T>(T res)
        {
            JsResponseResult <T> ree = new JsResponseResult <T>()
            {
                ActionType = JsActionType.Object,
                Obj        = res
            };

            //  ree.SaveString(System.Xml.Formatting.Indented);
            // string str = JsonConvert.SerializeObject(ree);
            ree.EndTimer  = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffff");
            ree.BeginTime = AtawAppContext.Current.GetItem("__beginTime").Value <DateTime>().ToString("yyyy/MM/dd HH:mm:ss.ffff");
            string str = AtawAppContext.Current.FastJson.ToJSON(ree);

            AtawTrace.WriteFile(LogType.PageViewData, str);



            // HttpContext.Response.HeaderEncoding = Encoding.UTF8;
            //HttpContext.Response.
            return(str);
        }
Пример #8
0
        public static T AppKv<T>(this string regName, T def)
        {
            T result = default(T);
            if (def != null)
            {
                result = def;
            }
            var _obj = AtawAppContext.Current.ApplicationXml.AppSettings[regName];
            if (_obj != null)
            {
                try
                {
                    string _exeStr = _obj.ForceExeValue() ?? _obj.Value;
                    return _exeStr.Value<T>(def);
                }
                catch (Exception ex){
                    AtawTrace.WriteFile(LogType.AppKvError,
                        "注册表达式为{0}的弘插件在Key为{1}的配置执行上发生错误{2}{3}".AkFormat(_obj.Value,regName,Environment.NewLine,ex.Message+ ex.StackTrace));
                    result = def; 
                }
            }

            return result;
        }
Пример #9
0
        public int ADOSubmit()
        {
            bool _haslog = LogSaveAtawChanges();//是否需要记录日志
            var  con     = Database.Connection as SqlConnection;
            //check
            int result = 0;

            if ((CommandItems != null && CommandItems.Count > 0) || (SqlTransList.IsNotEmpty()) || AtawDbContextList.IsNotEmpty())
            {
                StringBuilder sb = new StringBuilder();
                if (_haslog)
                {
                    sb.Append(con.ConnectionString);
                    sb.AppendLine();
                    LogCommand(sb);
                    foreach (IUnitOfData unit in AtawDbContextList)
                    {
                        unit.IsChildUnit = true;
                        unit.LogCommand(sb);
                    }
                    if (AtawAppContext.Current.Logger != null)
                    {
                        AtawAppContext.Current.Logger.Debug(sb.ToString());
                    }
                    AtawTrace.WriteFile(LogType.SubmitSql, sb.ToString());
                }
                using (con)
                {
                    if (con.State == ConnectionState.Closed)
                    {
                        con.Open();
                    }
                    var trans = con.BeginTransaction();

                    using (trans)
                    {
                        try
                        {
                            if (SqlTransList.Count > 0)
                            {
                                foreach (var sql in SqlTransList)
                                {
                                    result = result + SqlHelper.ExecuteNonQuery(trans, sql.CommandType,
                                                                                sql.sql, sql.param);
                                }
                            }
                            foreach (var com in CommandItems)
                            {
                                result = result + SqlHelper.ExecuteNonQuery(trans, com.CommandType,
                                                                            com.CommandText, com.Parameters.ToArray());
                            }

                            foreach (var context in AtawDbContextList)
                            {
                                result = result + context.Submit();
                            }
                            if (ApplyFun != null && ApplyFun.Count > 0)
                            {
                                foreach (var fun in ApplyFun)
                                {
                                    result = result + fun(null);
                                }
                            }
                            //if (!IsChildUnit)
                            //{
                            //    AtawTrace.WriteFile(LogType.SubmitSql, sb.ToString());
                            //}
                            trans.Commit();
                        }
                        //catch (System.Data.Common.DbException wex)
                        //{
                        //    trans.Rollback();
                        //    throw wex;
                        //}
                        catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                        {
                            string exx = string.Join("|",
                                                     dbEx.EntityValidationErrors.Select(
                                                         a => string.Join("-", a.ValidationErrors.Select(b => b.ErrorMessage))
                                                         ));
                            AtawTrace.WriteFile(LogType.EfErrot, exx);
                            trans.Rollback();
                            throw dbEx;
                        }
                        catch (Exception ex)
                        {
                            AtawTrace.WriteFile(LogType.SqlSubmitError, sb.ToString());
                            trans.Rollback();
                            throw ex;
                        }
                        finally {
                        }

                        //catch (SqlException sqlEx)
                        //{
                        //    trans.Rollback();
                        //    throw sqlEx;
                        //}
                    }
                }
            }

            return(result);
        }
Пример #10
0
        public void Initialization()
        {
            IsInit = true;
            SignTime("启动");
            string appGuid = Guid.NewGuid().ToString();

            StringBuilder exSb = new StringBuilder();

            LoadApplicationXml();
            SignTime("全局配置完毕...");
            // Logger = LogManager.GetLogger(typeof(AtawAppContext), ApplicationXml.HasLogger);
            FastJson  = JSON.Current;
            AtawCache = DefaultAtawCache.Current;
            //if (ApplicationXml.HasLogger)
            //{
            string fpath = Path.Combine(BinPath, AtawApplicationConfig.REAL_PATH, "AtawLogs\\log\\log.txt");

            LogManager.Configure(fpath, 1000, false);
            //  }
            // Logger.Debug("程序初始化开始: " + appGuid);
            var dconn = DBConfigXml.Databases.First(a => a.IsDefault);

            AtawDebug.AssertNotNull(dconn, "至少有一个默认的数据库连接字符串", this);
            DefaultConnString = dconn.ConnectionString;
            // MemCachedCache mcc = MemCachedCache.Current;
            SignTime("日志初始化完毕....");
            if ("EBS".AppKv <bool>(false))
            {
                //初始化 消息总线
                //AtawBus = ServiceBus.Create().Start();
            }
            SignTime("消息总线初始化完毕....");
            //载入代码插件
            //AssamblyTable at = new AssamblyTable();
            //at.Exe();
            //------------web的程序装载器
            IAppRegistration webApp = AtawIocContext.Current.FetchInstance <IAppRegistration>(ATAW_WEB_REGNAME);

            AtawDebug.AssertNotNull(webApp, "web的程序装载器 加载失败", this);
            //webApp.Initialization();
            //this.
            AppDomainTypeFinder finder = new AppDomainTypeFinder();

            finder.LoadMatchingAssemblies(Path.Combine(BinPath, "bin"));
            var assList = finder.GetAssemblies();

            assList.ForEach(
                a =>
            {
                try
                {
                    LogBuilder(System.Environment.NewLine + "开始检查程序集:" + a.FullName);
                    var types = a.GetTypes();
                    var dll   = this.dllLog(a.FullName);
                    foreach (var t in types)
                    {
                        try
                        {
                            LogBuilder(System.Environment.NewLine + "开始检查类型:" + t.FullName);
                            SetAssamblyClassType(t, dll);
                            webApp.SetAssamblyClassType(t);
                        }
                        catch (ReflectionTypeLoadException rtlex)
                        {
                            // LogBuilder();

                            string _mesg = string.Join(System.Environment.NewLine, rtlex.LoaderExceptions.Select(b => b.Message));
                            LogBuilder(t.FullName + "插件异常单个记录", _mesg);
                            //exSb.Append(System.Environment.NewLine);
                            //SysTxtBuilder.Current.NewLine();

                            //exSb.Append(rtlex.Message);
                            //SysTxtBuilder.Current.Append(rtlex.Message);

                            //exSb.Append(System.Environment.NewLine);
                            //SysTxtBuilder.Current.NewLine();
                        }
                        catch (Exception ex)
                        {
                            // if(ex.InnerException != null)


                            LogBuilder(t.FullName + "插件异常单个记录", ex.Message, ex.InnerException == null ? "" : ex.InnerException.Message);
                            //exSb.Append(System.Environment.NewLine);
                            //SysTxtBuilder.Current.NewLine();
                            dll.ClassInfoList.Add(
                                new ClassInfo()
                            {
                                Error = t.FullName + "插件异常单个记录" +
                                        ex.Message +
                                        (ex.InnerException == null ? "" : ex.InnerException.Message)
                            }
                                );
                            //exSb.Append(ex.Message);
                            //SysTxtBuilder.Current.Append(ex.Message);

                            //exSb.Append(System.Environment.NewLine);
                            //SysTxtBuilder.Current.NewLine();
                        }
                    }
                }

                catch (ReflectionTypeLoadException rtlex)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (Exception exc in rtlex.LoaderExceptions)
                    {
                        sb.AppendLine("dll载入异常记录 " + exc.Message + (exc.InnerException == null ? "" : exc.InnerException.Message));

                        // LogBuilder("dll载入异常记录", exc.Message, exc.InnerException == null ? "" : exc.InnerException.Message);
                    }

                    this.dllErrorLog(sb.ToString());
                }
                catch (Exception ex2)
                {
                    LogBuilder(a.FullName + "插件程序集异常单个记录", ex2.Message, ex2.InnerException == null ? "" : ex2.InnerException.Message);
                    //exSb.Append(System.Environment.NewLine);
                    //SysTxtBuilder.Current.NewLine();
                    this.dllErrorLog(a.FullName + "插件程序集异常单个记录 " + ex2.Message + (ex2.InnerException == null ? "" : ex2.InnerException.Message));
                    //exSb.Append(ex2.Message);
                    //SysTxtBuilder.Current.Append(ex2.Message);

                    //exSb.Append(System.Environment.NewLine);
                    //SysTxtBuilder.Current.NewLine();
                }
            }
                );
            SignTime("程序集插件注册完毕....");
            //注册享元插件
            PageFlyweight = AtawIocContext.Current.FetchInstance <IFlyweight>("PageFlyweight");
            ////把工作单元做为享元
            //PageFlyweight.Set<IUnitOfData>("PFW_UFD", CreateDbContext());
            //载入xml 插件

            //执行全局配置的宏插件
            //   this.ApplicationXml.ExeMacro();
            SignTime("执行全局配置的宏插件完毕....");

            string rightRegName = ApplicationXml.RightBuilder.RegName;

            AtawRightBuilder = new Lazy <IAtawRightBuilder>(
                () => AtawIocContext.Current.FetchInstance <IAtawRightBuilder>(rightRegName)
                );

            string groupRegName = ApplicationXml.GroupBuilder.RegName;

            GroupBuilder = new Lazy <IGroupBuilder>(
                () => AtawIocContext.Current.FetchInstance <IGroupBuilder>(groupRegName)
                );

            //注册gps
            string gpsRegName = ApplicationXml.GPSBuilder.RegName;

            GPSBuilder = new Lazy <IGPSBuilder>(
                () => AtawIocContext.Current.FetchInstance <IGPSBuilder>(gpsRegName)
                );

            //注册gpsSet
            string gpsSetRegName = ApplicationXml.GPSSetBuilder.RegName;

            GPSSetBuilder = new Lazy <IGPSSet>(
                () => AtawIocContext.Current.FetchInstance <IGPSSet>(gpsSetRegName)
                );

            //注册内部服务
            string ServiceRegName = ApplicationXml.ServiceBuilder.RegName;

            AtawServiceBuilder = new Lazy <IAtawServiceBuilder>(
                () => AtawIocContext.Current.FetchInstance <IAtawServiceBuilder>(ServiceRegName)
                );
            //注册内部信息接口
            string MessagesRegName = ApplicationXml.MessagesBuilder.RegName;

            AtawMessagesBuilder = new Lazy <IMessagesBuilder>(
                () => AtawIocContext.Current.FetchInstance <IMessagesBuilder>(MessagesRegName)
                );
            ////注册工作流模式接口
            //string WorkflowDefRegName = ApplicationXml.WorkflowDefBuilder.RegName;
            //AtawWorkflowDefBuilder = new Lazy<IWorkflowDefBuilder>(
            //    () => AtawIocContext.Current.FetchInstance<IWorkflowDefBuilder>(WorkflowDefRegName)
            //    );
            //载入  codeTable xml
            SignTime("app配置注册完毕....");
            InitAppXml();

            SignTime("固定接口注册完毕....");
            InitCodeTableXml();
            SignTime("XMl代码表注册完毕....");
            InitDataDictXml();
            SignTime("XMl数据字典注册完毕....");
            InitCustomScriptXml();


            SignTime("自定义js注册完毕....");
            //载入控制器
            //AtawIocContext.Current.FetchInstance<IAppRegistration>(AtawAppContext.ATAW_WEB_REGNAME).Initialization();
            webApp.Initialization();

            ExeAppRegistration();
            SignTime("全局插件执行完毕....");
            // Logger.Debug("程序初始化结束: " + appGuid);
            //  Logger.Debug("插件异常总结: " + exSb.ToString());
            // SysTxtBuilder.Current.Flush(LogType.Plugin);
            AtawTrace.WriteFile(LogType.Plugin, this.fLogInfo.ToJson());


            if ("CombineJs".AppKv <bool>(false))
            {
                //ICombineJs combine = "CombineJs".CodePlugIn<ICombineJs>();
                //bool CombinJsResult = combine.init(AtawAppContext.Current.BinPath);
                //if (!CombinJsResult)
                //{
                //    throw new AtawException("压缩JS出错!", this);
                //}
                //SignTime("js压缩执行完毕....");
            }
        }