示例#1
0
文件: Xml_codec.cs 项目: jiangboh/CS
        /// <summary>
        /// 日志打印。
        /// </summary>
        /// <param name="str"></param>
        public static void StaticOutputLog(LogInfoType type, string str, string modeName,
                                           LogCategory category = LogCategory.I,
                                           [CallerMemberName] string memberName = "",
                                           [CallerFilePath] string filePath     = "",
                                           [CallerLineNumber] int lineNumber    = 0)
        {
            if (type < DataController.LogOutputLevel)
            {
                return;
            }

            string outStr = string.Format("{0}", str);

            //Console.WriteLine(outStr);

            FrmMainController.add_log_info(type, outStr, modeName, category, filePath, memberName, lineNumber);
            Logger.Trace(type, outStr, modeName, category, memberName, filePath, lineNumber);

            outStr = null;
        }
示例#2
0
文件: Logger.cs 项目: jiangboh/CS
        /// <summary>
        /// 用于处理Logger的线程
        /// </summary>
        /// <param name="obj"></param>
        private static void thread_for_logger(object obj)
        {
            bool          noMsg   = false;
            string        info    = "";
            List <string> lstData = new List <string>();

            DateTime startTime = System.DateTime.Now;
            DateTime endTime   = System.DateTime.Now;
            TimeSpan ts        = endTime.Subtract(startTime);

            List <string> batchData = new List <string>();

            DateTime startTimeConn = System.DateTime.Now;
            DateTime endTimeConn   = System.DateTime.Now;
            TimeSpan tsConn        = endTimeConn.Subtract(startTimeConn);

            DateTime startTimeMonitor = System.DateTime.Now;
            DateTime endTimeMonitor   = System.DateTime.Now;
            TimeSpan tsMonitor        = endTimeMonitor.Subtract(startTimeMonitor);


            while (true)
            {
                if (noMsg)
                {
                    //没消息时Sleep一大点
                    Thread.Sleep(100);
                }
                else
                {
                    //有消息时Sleep一小点
                    Thread.Sleep(2);
                }

                try
                {
                    #region 报告线程状态

                    endTimeMonitor = System.DateTime.Now;
                    tsMonitor      = endTimeMonitor.Subtract(startTimeMonitor);

                    if (tsMonitor.TotalSeconds >= 60)
                    {
                        FrmMainController.write_monitor_status("Logger_Status");

                        //计时复位
                        startTimeMonitor = System.DateTime.Now;
                    }

                    #endregion
                }
                catch (Exception ee)
                {
                    Logger.Trace(LogInfoType.EROR, ee.Message, "Logger", LogCategory.I);
                    continue;
                }

                try
                {
                    #region keepAlive处理

                    if (logOutType == LogOutType.OT_Net || logOutType == LogOutType.OT_Both)
                    {
                        endTimeConn = System.DateTime.Now;
                        tsConn      = endTimeConn.Subtract(startTimeConn);

                        if (tsConn.TotalSeconds >= DataController.CheckFtpConnStatTime)
                        {
                            string str       = string.Format("[{0}]--keepAlive-UDP", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"));
                            byte[] sendBytes = System.Text.Encoding.Default.GetBytes(str);

                            udpSender.Connect(DataController.StrLogIpAddr, int.Parse(DataController.StrLogPort));

                            //udpSender.Send(sendBytes, sendBytes.Length);

                            Trace(LogInfoType.INFO, str, "Logger", LogCategory.S);
                            FrmMainController.add_log_info(LogInfoType.INFO, str, "Logger", LogCategory.S);

                            //计时复位
                            startTimeConn = System.DateTime.Now;
                        }
                    }

                    #endregion
                }
                catch (Exception ee)
                {
                    Logger.Trace(LogInfoType.EROR, ee.Message, "Logger", LogCategory.I);
                    continue;
                }

                try
                {
                    #region 保存Logger

                    lock (mutex_Logger)
                    {
                        // 动态计算批量更新的数量
                        BatchValue = (4 - (int)DataController.LogOutputLevel) * 10;
                        if (gQueueLogger.Count < BatchValue)
                        {
                            #region 数量不足

                            endTime = System.DateTime.Now;
                            ts      = endTime.Subtract(startTime);

                            if (ts.TotalSeconds < DataController.LogMaxIdleSeconds)
                            {
                                noMsg = true;
                                continue;
                            }
                            else
                            {
                                //清空数据
                                //lstData = new List<string>();
                                lstData.Clear();
                                lstData.TrimExcess();

                                //拷贝数据
                                while (gQueueLogger.Count > 0)
                                {
                                    lstData.Add(gQueueLogger.Dequeue());
                                }

                                //复位计时
                                startTime = System.DateTime.Now;
                            }

                            #endregion
                        }
                        else
                        {
                            #region 数量充足

                            //清空数据
                            //lstData = new List<string>();
                            lstData.Clear();
                            lstData.TrimExcess();

                            //拷贝数据
                            for (int i = 0; i < BatchValue; i++)
                            {
                                lstData.Add(gQueueLogger.Dequeue());
                            }

                            //复位起始时间
                            startTime = System.DateTime.Now;

                            #endregion
                        }
                    }

                    noMsg = false;
                    switch (logOutType)
                    {
                    case LogOutType.OT_File:
                    {
                        //检测日志日期
                        StrategyLog();

                        info = null;
                        foreach (string str in lstData)
                        {
                            info += str + "\r\n";
                        }

                        System.Diagnostics.Trace.WriteLine(info);
                        if (fileFlushType == FileFlushType.RightNow)
                        {
                            System.Diagnostics.Trace.Close();
                        }

                        info = null;
                        break;
                    }

                    case LogOutType.OT_Net:
                    {
                        foreach (string str in lstData)
                        {
                            byte[] sendBytes = Encoding.Default.GetBytes(str);
                            udpSender.Send(sendBytes, sendBytes.Length);
                        }

                        break;
                    }

                    case LogOutType.OT_Both:
                    {
                        //检测日志日期
                        StrategyLog();

                        info = null;
                        foreach (string str in lstData)
                        {
                            info += str + "\r\n";
                        }

                        System.Diagnostics.Trace.WriteLine(info);
                        if (fileFlushType == FileFlushType.RightNow)
                        {
                            System.Diagnostics.Trace.Close();
                        }

                        foreach (string str in lstData)
                        {
                            byte[] sendBytes = Encoding.Default.GetBytes(str);
                            udpSender.Send(sendBytes, sendBytes.Length);
                        }

                        info = null;
                        break;
                    }
                    }

                    #endregion
                }
                catch (Exception ee)
                {
                    Logger.Trace(LogInfoType.EROR, ee.Message, "Logger", LogCategory.I);
                    continue;
                }
            }
        }