示例#1
0
        public static void WritePVInfo(string jsonPath, string userHostAddress, string actionAddress)
        {
            try
            {
                using (StreamReader sr = new StreamReader(jsonPath))
                {
                    {
                        JsonSerializer serializer = new JsonSerializer();
                        serializer.Converters.Add(new JavaScriptDateTimeConverter());
                        serializer.NullValueHandling = NullValueHandling.Ignore;

                        //构建Json.net的读取流
                        JsonReader reader = new JsonTextReader(sr);
                        //对读取出的Json.net的reader流进行反序列化,并装载到模型中
                        var webPV = serializer.Deserialize <WebPVInfo>(reader);
                        if (webPV == null)
                        {
                            webPV           = new WebPVInfo();
                            webPV.PVCount   = 1;
                            webPV.LstPVInfo = new List <PVInfo>()
                            {
                                new PVInfo()
                                {
                                    PVIP = userHostAddress, PVTime = DateTime.Now.ToString(), PVActionAddress = actionAddress
                                }
                            };
                        }
                        else
                        {
                            webPV.PVCount = webPV.PVCount + 1;
                            webPV.LstPVInfo.Add(new PVInfo()
                            {
                                PVIP = userHostAddress, PVTime = DateTime.Now.ToString(), PVActionAddress = actionAddress
                            });
                        }
                        reader.Close();

                        using (StreamWriter sw = new StreamWriter(jsonPath))
                        {
                            //构建Json.net的写入流
                            JsonWriter writer = new JsonTextWriter(sw);
                            //把模型数据序列化并写入Json.net的JsonWriter流中
                            serializer.Serialize(writer, webPV);
                            //ser.Serialize(writer, ht);
                            writer.Close();
                            sw.Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
示例#2
0
        /// <summary>
        /// 获取PV数据
        /// </summary>
        /// <param name="pvJsonPath"></param>
        /// <returns></returns>
        public static WebPVInfo InitWebPVInfo(string pvJsonPath)
        {
            _pvJsonPath = pvJsonPath;
            if (_webPVInfo == null)
            {
                if (!File.Exists(pvJsonPath))
                {
                    var pvFile = File.Create(pvJsonPath);
                    pvFile.Flush();
                    _webPVInfo = new WebPVInfo()
                    {
                        LstPVInfo      = new List <PVInfo>(),
                        SalesLstPVInfo = new ConcurrentBag <PVInfo>()
                    };
                    return(_webPVInfo);
                }


                using (var stream = new FileStream(_pvJsonPath, FileMode.OpenOrCreate))
                {
                    try
                    {
                        StreamReader   sr         = new StreamReader(stream);
                        JsonSerializer serializer = new JsonSerializer
                        {
                            NullValueHandling = NullValueHandling.Ignore,
                            Converters        = { new JavaScriptDateTimeConverter() }
                        };
                        //构建Json.net的读取流
                        using (var reader = new JsonTextReader(sr))
                        {
                            _webPVInfo = serializer.Deserialize <WebPVInfo>(reader);
                            _webPVInfo.SalesLstPVInfo = new ConcurrentBag <PVInfo>();
                            _webPVInfo?.LstPVInfo.ForEach(item => _webPVInfo.SalesLstPVInfo.Add(item));
                        }
                        //对读取出的Json.net的reader流进行反序列化,并装载到模型中
                        // LogHelper.WriteLog("The First WebPVInfo Deserialize Success!");
                    }
                    catch (Exception ex)
                    {
                        _webPVInfo = new WebPVInfo()
                        {
                            LstPVInfo      = new List <PVInfo>(),
                            SalesLstPVInfo = new ConcurrentBag <PVInfo>()
                        };

                        // LogHelper.WriteError("Deserialize WebPVInfo Exception", ex);
                    }
                }
            }
            return(_webPVInfo);
        }
示例#3
0
        /// <summary>
        /// 获取PV数据
        /// </summary>
        /// <param name="pvJsonPath"></param>
        /// <returns></returns>
        public static WebPVInfo InitWebPVInfo(string pvJsonPath)
        {
            _pvJsonPath = pvJsonPath;
            if (_webPVInfo == null)
            {
                if (!File.Exists(pvJsonPath))
                {
                    var pvFile = File.Create(pvJsonPath);
                    pvFile.Close();
                    _webPVInfo                = new WebPVInfo();
                    _webPVInfo.LstPVInfo      = new List <PVInfo>();
                    _webPVInfo.SalesLstPVInfo = new ConcurrentBag <PVInfo>();
                    return(_webPVInfo);
                }

                using (StreamReader sr = new StreamReader(_pvJsonPath))
                {
                    try
                    {
                        JsonSerializer serializer = new JsonSerializer();
                        serializer.Converters.Add(new JavaScriptDateTimeConverter());
                        serializer.NullValueHandling = NullValueHandling.Ignore;

                        //构建Json.net的读取流
                        JsonReader reader = new JsonTextReader(sr);
                        //对读取出的Json.net的reader流进行反序列化,并装载到模型中
                        _webPVInfo = serializer.Deserialize <WebPVInfo>(reader);
                        _webPVInfo.SalesLstPVInfo = new ConcurrentBag <PVInfo>();
                        if (_webPVInfo.LstPVInfo != null)
                        {
                            _webPVInfo.LstPVInfo.ForEach(item => _webPVInfo.SalesLstPVInfo.Add(item));
                        }
                        reader.Close();
                        LogHelper.WriteLog("The First WebPVInfo Deserialize Success!");
                    }catch (Exception ex)
                    {
                        _webPVInfo                = new WebPVInfo();
                        _webPVInfo.LstPVInfo      = new List <PVInfo>();
                        _webPVInfo.SalesLstPVInfo = new ConcurrentBag <PVInfo>();

                        LogHelper.WriteError("Deserialize WebPVInfo Exception", ex);
                    }
                }
            }
            return(_webPVInfo);
        }