/// <summary>
 /// Show a status message at the console and optionally include in the log file
 /// </summary>
 /// <param name="statusMessage">Status message</param>
 /// <param name="isError">True if this is an error</param>
 /// <param name="writeToLog">True to write to the log file; false to only display at console</param>
 public static void LogMessage(string statusMessage, bool isError = false, bool writeToLog = true)
 {
     if (writeToLog)
     {
         if (isError)
         {
             LogTools.LogError(statusMessage);
         }
         else
         {
             LogTools.LogMessage(statusMessage);
         }
     }
     else
     {
         if (isError)
         {
             ConsoleMsgUtils.ShowErrorCustom(statusMessage, false);
         }
         else
         {
             Console.WriteLine(statusMessage);
         }
     }
 }
        private void SetTaskComplete(int taskID, int completionCode, string completionMessage, IEnumerable <int> cachedFileIDs)
        {
            try
            {
                //Setup for execution of the stored procedure
                var cmd = mDbTools.CreateCommand(SP_NAME_SET_TASK_COMPLETE, CommandType.StoredProcedure);

                mDbTools.AddParameter(cmd, "@Return", SqlType.Int, ParameterDirection.ReturnValue);
                mDbTools.AddParameter(cmd, "@processorName", SqlType.VarChar, 128, ProcessorName);
                mDbTools.AddParameter(cmd, "@taskID", SqlType.Int).Value         = taskID;
                mDbTools.AddParameter(cmd, "@CompletionCode", SqlType.Int).Value = completionCode;
                mDbTools.AddParameter(cmd, "@CompletionMessage", SqlType.VarChar, 255, completionMessage);
                mDbTools.AddParameter(cmd, "@CachedFileIDs", SqlType.VarChar, -1, string.Join(",", cachedFileIDs));
                var messageParam = mDbTools.AddParameter(cmd, "@message", SqlType.VarChar, 512, ParameterDirection.Output);

                ReportMessage("Calling " + cmd.CommandText + " on " + MTSServer, BaseLogger.LogLevels.DEBUG);

                //Execute the SP (retry the call up to 4 times)
                mDbTools.TimeoutSeconds = 20;
                var resCode = mDbTools.ExecuteSP(cmd, 4);

                if (resCode != 0)
                {
                    LogTools.LogError("Error " + resCode + " setting cache task complete: " + (string)messageParam.Value);
                }
            }
            catch (Exception ex)
            {
                ReportError("Error in SetTaskComplete for server " + MTSServer + ": " + ex.Message, true, ex);
            }
        }
        private int RequestTask()
        {
            var taskID = 0;

            try
            {
                //Setup for execution of the stored procedure
                var cmd = mDbTools.CreateCommand(SP_NAME_REQUEST_TASK, CommandType.StoredProcedure);

                mDbTools.AddParameter(cmd, "@Return", SqlType.Int, ParameterDirection.ReturnValue);
                mDbTools.AddParameter(cmd, "@processorName", SqlType.VarChar, 128, ProcessorName);
                var taskAvailableParam = mDbTools.AddParameter(cmd, "@taskAvailable", SqlType.TinyInt, ParameterDirection.Output);
                var taskIdParam        = mDbTools.AddParameter(cmd, "@taskID", SqlType.Int, ParameterDirection.Output);
                var messageParam       = mDbTools.AddParameter(cmd, "@message", SqlType.VarChar, 512, ParameterDirection.Output);

                ReportMessage("Calling " + cmd.CommandText + " on " + MTSServer, BaseLogger.LogLevels.DEBUG);

                //Execute the SP (retry the call up to 4 times)
                cmd.CommandTimeout = 20;
                var resCode = mDbTools.ExecuteSP(cmd, 4);

                if (resCode == 0)
                {
                    var taskAvailable = Convert.ToInt16(taskAvailableParam.Value);

                    if (taskAvailable > 0)
                    {
                        taskID = Convert.ToInt32(taskIdParam.Value);

                        ReportMessage("Received cache task " + taskID + " from " + MTSServer);
                    }
                    else
                    {
                        ReportMessage(cmd.CommandText + " returned taskAvailable = 0 ", BaseLogger.LogLevels.DEBUG);
                    }
                }
                else
                {
                    LogTools.LogError("Error " + resCode + " requesting a cache task: " + (string)messageParam.Value);
                    taskID = 0;
                }
            }
            catch (Exception ex)
            {
                ReportError("Error in RequestTask for server " + MTSServer + ": " + ex.Message, true, ex);
                taskID = 0;
            }

            return(taskID);
        }
Beispiel #4
0
        public bool Send(byte[] byteData)
        {
            if (!SocketValid())
            {
                return(false);
            }

            if (LastLen < byteData.Length)
            {
                LogTools.LogError("SendFailed Too Long");
                return(false);
            }
            Array.Copy(byteData, 0, sendBuffer, sendBufferOffset, byteData.Length);
            sendBufferOffset += byteData.Length;

            mSocket.BeginSend(sendBuffer, 0, sendBufferOffset, SocketFlags.None, OnSend, mSocket);
            return(true);
        }
        public void TestLogTools(string logDirectory, string logFileNameBase, BaseLogger.LogLevels entryType, BaseLogger.LogLevels logThresholdLevel)
        {
            var logFilePath = Path.Combine(logDirectory, logFileNameBase);

            LogTools.CreateFileLogger(logFilePath, logThresholdLevel);
            Console.WriteLine("Log file; " + LogTools.CurrentLogFilePath);

            var message = "Test log " + entryType + " via LogTools (log threshold is " + logThresholdLevel + ")";

            switch (entryType)
            {
            case BaseLogger.LogLevels.DEBUG:
                LogTools.LogDebug(message);
                break;

            case BaseLogger.LogLevels.INFO:
                LogTools.LogMessage(message);
                break;

            case BaseLogger.LogLevels.WARN:
                LogTools.LogWarning(message);
                break;

            case BaseLogger.LogLevels.ERROR:
                LogTools.LogError(message);
                break;

            case BaseLogger.LogLevels.FATAL:
                LogTools.LogFatalError(message);
                break;

            default:
                LogTools.LogError("Unrecognized log type: " + entryType);
                break;
            }

            ProgRunner.SleepMilliseconds(100);

            LogTools.FlushPendingMessages();
        }
Beispiel #6
0
        void OnSend(IAsyncResult ar)
        {
            try
            {
                Socket mSendSocket = mSocket;
                int    curLen      = mSendSocket.EndSend(ar);

                sendBufferOffset -= curLen;

                Array.Copy(sendBuffer, curLen, sendBuffer, 0, sendBufferOffset);
                Array.Clear(sendBuffer, sendBufferOffset, sendBuffer.Length - sendBufferOffset);

                if (sendBufferOffset <= 0)
                {
                    return;
                }
                mSendSocket.BeginSend(sendBuffer, 0, sendBufferOffset, SocketFlags.None, OnSend, mSendSocket);
            }
            catch (Exception e)
            {
                LogTools.LogError(e.ToString());
                Destroy();
            }
        }
Beispiel #7
0
        void OnRecv(IAsyncResult ar)
        {
            if (mSocket == null)
            {
                return;
            }

            try
            {
                Socket curSvr = mSocket;
                int    curLen = curSvr.EndReceive(ar);
                LogTools.Log(string.Format("收到数据,长度:[{0}]", curLen));

                if (curLen < NetData.NetDataLen)
                {
                    LogTools.LogError("解析失败,数据长度不足解析报文长度");
                    return;
                }
                int dataOffset = 0;
                int lastLen    = curLen - dataOffset; //剩余长度
                while (lastLen > 0)                   //如果还有数据
                {
                    if (unCompleteData == null)
                    {
                        if (lastLen < NetData.NetDataLen)//如果剩余长度不足以解析数据长度则跳出循环继续接收
                        {
                            break;
                        }
                        int dataLen = NetData.GetDataLen(recvBuffer, ref dataOffset);
                        lastLen = curLen - dataOffset;
                        bool    curLenEnough = dataLen <= lastLen;
                        int     realLen      = curLenEnough ? dataLen : lastLen;
                        NetData data         = new NetData(recvBuffer, dataOffset, realLen, dataLen, this);
                        dataOffset += realLen;
                        if (data.DataComplete())
                        {
                            mDataCache.EnqueueRecvData(data);
                        }
                        else
                        {
                            //如果数据不完整说明当前buffer的数据已经使用完,需要重新recv
                            unCompleteData = data;
                            break;
                        }
                    }
                    else
                    {
                        int unRecvLen = unCompleteData.GetUnRecvLen();
                        lastLen = curLen - dataOffset;
                        bool curLenEnough = unRecvLen <= lastLen;
                        int  realLen      = curLenEnough ? unRecvLen : lastLen;
                        if (unCompleteData.MergeData(recvBuffer, dataOffset, realLen))
                        {
                            mDataCache.EnqueueRecvData(unCompleteData);
                        }
                        dataOffset += realLen;
                    }
                    lastLen = curLen - dataOffset;
                }
                offset = curLen - dataOffset;
                if (offset > 0)
                {
                    Array.Copy(recvBuffer, dataOffset, recvBuffer, 0, offset);
                }
                Array.Clear(recvBuffer, offset, recvBuffer.Length - offset);
            }
            catch (Exception e)
            {
                LogTools.LogError(e.ToString());

                Destroy();
            }
            StartRecv();
        }
 /// <summary>
 /// Log an error message and exception
 /// </summary>
 /// <param name="errorMessage">Error message</param>
 /// <param name="ex">Exception to log</param>
 public static void LogError(string errorMessage, Exception ex)
 {
     LogTools.LogError(errorMessage, ex);
 }
 /// <summary>
 /// Log an error message
 /// </summary>
 /// <param name="errorMessage">Error message</param>
 /// <param name="logToDb">When true, log the message to the database and the local log file</param>
 public static void LogError(string errorMessage, bool logToDb = false)
 {
     LogTools.LogError(errorMessage, null, logToDb);
 }