Example #1
0
        private void InitRequestLogLocalForMySql()
        {
            string strSql = @"SELECT distinct A.id,A.IPAddress,B.Country,B.CountryNo,B.BigArea,B.Isp,B.Province,B.City,B.Area from (
	                            SELECT A.Id,A.IPAddress from sys_requestlog as A
	                            where IsProcessIp is NULL or IsProcessIp =0
                            )as A
                            left join sys_requestlog as B ON A.ipaddress=b.IPAddress AND B.IsProcessIp=1";

            MySqlCMSDbContext dbContext = new MySqlCMSDbContext();

            ((System.Data.Entity.Infrastructure.IObjectContextAdapter)dbContext).ObjectContext.CommandTimeout = 0;
            List <CommonOldLogEntity> models = new List <CommonOldLogEntity>();

            models = dbContext.Database.SqlQuery <CommonOldLogEntity>(strSql).ToList <CommonOldLogEntity>();
            if (models != null && models.Count > 0)
            {
                List <string>           modelIds = models.Select(m => m.Id).ToList();
                List <RequestLogEntity> modelsT  = dbContext.RequestLogEntitys.Where(m => modelIds.Contains(m.Id)).ToList();
                if (modelsT != null && modelsT.Count > 0)
                {
                    foreach (RequestLogEntity model in modelsT)
                    {
                        CommonOldLogEntity modelT = models.Find(m => m.Id == model.Id);
                        if (modelT != null)
                        {
                            model.Country     = modelT.Country;
                            model.CountryNo   = modelT.CountryNo;
                            model.BigArea     = modelT.BigArea;
                            model.Isp         = modelT.Isp;
                            model.Province    = modelT.Province;
                            model.City        = modelT.City;
                            model.Area        = modelT.Area;
                            model.IsProcessIp = true;
                            dbContext.RequestLogEntitys.Add(model);
                            dbContext.Entry <RequestLogEntity>(model).State = EntityState.Modified;
                            dbContext.SaveChanges();
                        }
                    }
                }
            }
        }
Example #2
0
        private void InitAccessLogIpForMySql()
        {
            InitAccessLogIpLocalForMySql();

            MySqlCMSDbContext dbContext = new MySqlCMSDbContext();

            ((System.Data.Entity.Infrastructure.IObjectContextAdapter)dbContext).ObjectContext.CommandTimeout = 0;
            List <AccessLogEntity> models = dbContext.AccessLogEntitys.Where(m => m.IsProcessIp != true && m.IPAddress != "::1").ToList();

            List <string> IpLst = models.Select(m => m.IPAddress).Distinct().ToList();

            if (IpLst != null && IpLst.Count > 0)
            {
                int num   = 1;
                int sTime = 0;
                foreach (string IpAddress in IpLst)
                {
                    sTime++;
                    if (sTime >= 100)
                    {
                        sTime = 0;
                        System.Threading.Thread.Sleep(5000);
                    }
                    if (!string.IsNullOrEmpty(IpAddress))
                    {
                        try
                        {
                            ResultIpData data = GetIpAddress(IpAddress);
                            if (data != null && !string.IsNullOrEmpty(data.ip))
                            {
                                if (models != null && models.Count > 0)
                                {
                                    models.ForEach(delegate(AccessLogEntity accessModel)
                                    {
                                        if (accessModel.IPAddress == IpAddress)
                                        {
                                            accessModel.Country     = data.country;
                                            accessModel.CountryNo   = data.country_id;
                                            accessModel.BigArea     = data.area;
                                            accessModel.Isp         = data.isp;
                                            accessModel.Province    = data.region;
                                            accessModel.City        = data.city;
                                            accessModel.Area        = data.county;
                                            accessModel.IsProcessIp = true;
                                            dbContext.AccessLogEntitys.Add(accessModel);
                                            dbContext.Entry <AccessLogEntity>(accessModel).State = EntityState.Modified;
                                            dbContext.SaveChanges();
                                        }
                                    });
                                }
                            }
                            else
                            {
                                iLog.WriteLog("InitAccessLogIpForMySql:未获取到结果", 1);
                            }
                        }
                        catch (Exception ex)
                        {
                            iLog.WriteLog("InitAccessLogIpForMySql:处理异常:" + ex.Message, 1);
                        }
                    }
                    iLog.WriteLog("InitAccessLogIpForMySql:当前处理条数:" + num + " 总条数:" + models.Count, 0);
                    num++;
                }
            }
        }