Ejemplo n.º 1
0
        private void InitRequestLogLocalForSqlServer()
        {
            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";

            SqlServerCMSDbContext dbContext = new SqlServerCMSDbContext();

            ((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();
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void InitRequestLogIpForSqlServer()
        {
            InitRequestLogLocalForSqlServer();
            SqlServerCMSDbContext dbContext = new SqlServerCMSDbContext();

            ((System.Data.Entity.Infrastructure.IObjectContextAdapter)dbContext).ObjectContext.CommandTimeout = 0;
            List <RequestLogEntity> models = dbContext.RequestLogEntitys.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(RequestLogEntity requestModel)
                                    {
                                        if (requestModel.IPAddress == IpAddress)
                                        {
                                            requestModel.Country     = data.country;
                                            requestModel.CountryNo   = data.country_id;
                                            requestModel.BigArea     = data.area;
                                            requestModel.Isp         = data.isp;
                                            requestModel.Province    = data.region;
                                            requestModel.City        = data.city;
                                            requestModel.Area        = data.county;
                                            requestModel.IsProcessIp = true;
                                            dbContext.RequestLogEntitys.Add(requestModel);
                                            dbContext.Entry <RequestLogEntity>(requestModel).State = EntityState.Modified;
                                            dbContext.SaveChanges();
                                        }
                                    });
                                }
                            }
                            else
                            {
                                iLog.WriteLog("InitAccessLogIpForMySql:未获取到结果", 1);
                            }
                        }
                        catch (Exception ex)
                        {
                            iLog.WriteLog("InitRequestLogIpForMySql:处理异常:" + ex.Message, 1);
                        }
                    }
                    iLog.WriteLog("InitRequestLogIpForMySql:当前处理条数:" + num + " 总条数:" + models.Count, 0);
                    num++;
                }
            }
        }