示例#1
0
    /// <summary>
    /// 提供一個訊息回報的機制或者用以記錄一些資訊,取得特定的必要資料,讓未來取得更為方便的除錯工具。
    /// </summary>
    /// <param name="eType">這個回報訊息的類型。</param>
    /// <param name="clsFunction">請鍵入 MethodInfo.GetCurrentMethod()。</param>
    /// <param name="strMessage">要記錄的主要訊息。</param>
    /// <param name="eSaveItems">儲存寫檔的時機點。</param>
    /// <example>
    /// <code>
    /// private void Example()
    /// {
    ///		try
    ///		{
    ///			// ============
    ///			// 你要得動作
    ///			// ============
    ///
    ///			// 可以當成一般訊息紀錄使用 (很重要才紀錄)
    ///			XStatus.Report(XStatus.Type.Infomation, MethodInfo.GetCurrentMethod(),"想要紀錄的資訊");
    ///		}
    ///		catch(Exception ex)
    ///		{
    ///			XStatus.Report(XStatus.Type.Windows, MethodInfo.GetCurrentMethod(), XStatus.GetExceptionLine(ex));
    ///		}
    /// }
    /// </code>
    /// </example>
    public static void Report(Type eType, MethodBase clsFunction, string strMessage, ErrorLevel eErrorLevel = ErrorLevel.Alarm, SaveItems eSaveItems = SaveItems.Default)
    {
        string strClassName = (clsFunction != null) ? "{" + clsFunction.ReflectedType.Name + "}" : "{Unknow}";
        string functionName = (clsFunction != null) ? strClassName + "[" + clsFunction.Name + "]" : "Unknow[Unknow]";

        string strAllMessage = DateTime.Now.ToString(TIME_FORMAT) + ", " + eType.ToString() + ", " + functionName + " - " + strMessage;

        if (LogRecorder != null && SendLogRecorder)
        {
            LogRecorder.WriteLog(eErrorLevel.ToString() + "|" + strAllMessage);
        }
        else
        {
            if (g_strLogFolderPath.Length > 3)
            {
                string strFilePath = "";
                switch (g_eRecordType)
                {
                case RecordInterval.None:
                    strFilePath = g_strLogFolderPath;
                    break;

                case RecordInterval.Hours:
                    strFilePath = g_strLogFolderPath + "Log_" + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                    break;

                case RecordInterval.Days:
                    strFilePath = g_strLogFolderPath + "Log_" + DateTime.Now.ToString("yyyyMMdd") + ".log";
                    break;
                }

                lock (g_oLockReserveMessage)
                {
                    try
                    {
                        if (eType != Type.Null)
                        {
                            // 在下面這段程式碼不直接呼叫其他功能函數,以免造成無窮回圈。
                            // 因為只有單行,所以在此允許插隊讓硬碟做此工作。
                            g_strReserveMessages.Add(strAllMessage);
                        }
                        lock (XFile.DiskLock)
                        {
                            switch (eSaveItems)
                            {
                            case SaveItems.Default:
                            case SaveItems.NoImmediate:
                                if (g_strReserveMessages.Count >= g_iReserveMessageThreshold)
                                {
                                    File.AppendAllLines(strFilePath, g_strReserveMessages);
                                    g_strReserveMessages.Clear();
                                }
                                break;

                            case SaveItems.Immediate:
                                File.AppendAllLines(strFilePath, g_strReserveMessages);
                                g_strReserveMessages.Clear();
                                break;
                            }
                        }
                    }
                    catch
                    {
                    }
                }
            }
        }
        lock (g_oLockReport)
        {
            try
            {
                LogMessage.Insert(0, strAllMessage);
                while (LogMessage.Count > 500)
                {
                    LogMessage.RemoveAt(500);
                }
            }
            catch
            {
            }
        }

        // Events Process (Callback function)
        //-------------------------------------------------------------------
        try
        {
            // 此順序由 Frank on 2014.10.9 排出,以危險性高低來做排序。
            switch (eType)
            {
            case Type.Motion:
                if (Status_MotionReport != null)
                {
                    Status_MotionReport(strAllMessage);
                }
                break;

            case Type.Robot:
                if (Status_RobotReport != null)
                {
                    Status_RobotReport(strAllMessage);
                }
                break;

            case Type.Device:
                if (Status_DeviceReport != null)
                {
                    Status_DeviceReport(strAllMessage);
                }
                break;

            case Type.IO:
                if (Status_IOReport != null)
                {
                    Status_IOReport(strAllMessage);
                }
                break;

            case Type.Communication:
                if (Status_CommunicationReport != null)
                {
                    Status_CommunicationReport(strAllMessage);
                }
                break;

            case Type.Vision:
                if (Status_VisionReport != null)
                {
                    Status_VisionReport(strAllMessage);
                }
                break;

            case Type.Procedure:
                if (Status_ProcedureReport != null)
                {
                    Status_ProcedureReport(strAllMessage);
                }
                break;

            case Type.Windows:
                if (Status_WindowsReport != null)
                {
                    Status_WindowsReport(strAllMessage);
                }
                break;

            case Type.Events:
                if (Status_EventsReport != null)
                {
                    Status_EventsReport(strAllMessage);
                }
                break;

            default:
                if (Status_AlarmReport != null)
                {
                    Status_AlarmReport(strAllMessage);
                }
                break;
            }
            if (Status_Changed != null)
            {
                Status_Changed(strAllMessage);
            }
        }
        catch
        {
            strAllMessage = DateTime.Now.ToString() + ", Events, {XStatus}[Report] - Callback function error.";
            g_strReserveMessages.Add(strAllMessage);    // 放到暫存區等待存檔。
            LogMessage.Insert(0, strAllMessage);        // 插入歷史紀錄的暫存區域。
        }
    }