예제 #1
0
파일: ServerReport.cs 프로젝트: abel/sinan
 /// <summary>
 /// 提交到服务器.
 /// </summary>
 /// <param name="log"></param>
 /// <returns></returns>
 public bool Request(LogBase log)
 {
     sb.Clear();
     try
     {
         string query = log.ToString(sb);
         int offset = Encoding.UTF8.GetBytes(query, 0, query.Length, sendBuffer, headLen) + headLen;
         using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
         {
             s.Connect(m_host);
             if (!s.Connected)
             {
                 try
                 {
                     m_host = CreateEndPoint(hostStr);
                 }
                 catch (Exception err)
                 {
                     LogWrapper.Error(err);
                     m_host = null;
                 }
                 return false;
             }
             Buffer.BlockCopy(sendEnds, 0, sendBuffer, offset, sendEnds.Length);
             s.Send(sendBuffer, 0, offset + sendEnds.Length, SocketFlags.None);
             int len = 0;
             while (true)
             {
                 int bytes = s.Receive(recvBuffer, len, recvBuffer.Length - len, SocketFlags.None);
                 if (bytes == 0)
                 {
                     break;
                 }
                 len += bytes;
             }
             string result = (Encoding.UTF8.GetString(recvBuffer, 0, len));
             // TODO:检查是否写入成功...
             return result.Contains("\"ret\":0");
         }
     }
     catch
     {
         return false;
     }
 }
예제 #2
0
파일: ServerLogger.cs 프로젝트: abel/sinan
 /// <summary>
 /// 重新写日志
 /// </summary>
 /// <param name="log"></param>
 /// <param name="retry">重试指定次数,直到写入成功</param>
 /// <param name="sleep"></param>
 private static void TryWrite(LogBase log, int retry, int sleep)
 {
     // 最多缓存4万条,大概1个小时的数据量
     const int maxCount = 40000;
     for (int i = 0; i < retry; i++)
     {
         if (m_logs.Count > maxCount)
         {
             break;
         }
         try
         {
             LogAccess.Instance.Insert(log);
             return;
         }
         catch { }
         System.Threading.Thread.Sleep(sleep);
     }
     //尝试指定次数失败或缓存过多时,写入本地日志文件
     try
     {
         sb.Clear();
         sb.Append("Logid=");
         sb.Append(log.ID.ToString());
         sb.Append("&");
         logger.Info(log.ToString(sb));
     }
     catch { }
 }