Пример #1
0
        /// <summary>
        /// 写日志文件
        /// </summary>
        /// <param name="logContent">日志内容</param>
        /// <param name="fileName">日志文件名</param>
        /// <returns>返回值true成功,false失败</returns>
        public static bool Write(string logContent, string fileName)
        {
            bool flag = true;

            if (string.IsNullOrEmpty(fileName))
            {
                fileName = "AppError";
            }
            try
            {
                string   str  = Path.Combine(APP_LOG_DIRECTORY, DateTime.Now.ToString("yyyyMMdd") + fileName + LOG_SUFFIX);
                FileInfo info = new FileInfo(str);
                if (info.Exists && (info.Length >= 0xc3500L))
                {
                    info.CopyTo(str.Replace(LOG_SUFFIX, TextUtility.CreateRandomNum(5) + LOG_SUFFIX));
                    File.Delete(str);
                }
                FileStream    stream  = new FileStream(str, FileMode.Append, FileAccess.Write, FileShare.Read);
                StreamWriter  writer  = new StreamWriter(stream, Encoding.UTF8);
                StringBuilder builder = new StringBuilder();
                builder.AppendFormat("{0:yyyy'/'MM'/'dd' 'HH':'mm':'ss}", DateTime.Now);
                builder.Append("|");
                if (WRITE_APP_LOG)
                {
                    builder.Append(logContent.Replace("\r", "").Replace("\n", "<br />"));
                    builder.Append("|");
                    builder.Append(GameRequest.GetUserIP());
                    builder.Append("|");
                    builder.Append(GameRequest.GetUrl());
                }
                else
                {
                    builder.Append(logContent);
                }
                builder.Append("\r\n");
                writer.Write(builder.ToString());
                writer.Flush();
                writer.Close();
                writer.Dispose();
                stream.Close();
                stream.Dispose();
            }
            catch
            {
                flag = false;
            }
            return(flag);
        }
Пример #2
0
        /// <summary>
        /// 发送传感数据线程
        /// </summary>
        private static void StartSendThread()
        {
            new Thread(new ThreadStart(() => {
                while (client != null && client.IsConnected)
                {
                    PushData datas = new PushData()
                    {
                        t        = REQ_TYPE.PUSH_DATA,
                        msgid    = TextUtility.CreateRandomNum(5).ToInt32(),
                        datatype = 1,
                    };


                    //传输JSON值的关键代码示例,比如要传输一个Member对象,可实例一个Member对象user,再将之序列号为JSON字符串然后附值给apiTag
                    Member user = new Member()
                    {
                        UserName = "******", Sex = 1, Age = TextUtility.CreateRandomNum(2).ToInt32(), IsMarry = false
                    };
                    var J = new JObject();
                    J.Add(Cfg.jsonApiTag, JsonConvert.SerializeObject(user));

                    //传输二进值的关键代码示例,将byte[]使用Base64编码为字符串
                    Byte[] ary = new Byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 };
                    J.Add(Cfg.binaryApiTag, Convert.ToBase64String(ary));

                    datas.datas = J;
                    try
                    {
                        //发送给云平台
                        String datagram = JsonConvert.SerializeObject(datas);
                        client.SendText(datagram);
                    }
                    catch { }

                    Thread.Sleep(5000);
                }
            })).Start();
        }