コード例 #1
0
ファイル: LogRecorder.cs プロジェクト: wondial/EntityModel
        /// <summary>
        ///  日志记录独立线程
        /// </summary>
        private static void WriteRecordLoop()
        {
            SystemTrace(LogLevel.System, "日志开始");
            int cnt = 0;

            while (State != LogRecorderStatus.Shutdown)
            {
                //Thread.Sleep(10);//让子弹飞一会
                if (State < LogRecorderStatus.Initialized || !BaseRecorder.IsInitialized || !Recorder.IsInitialized)
                {
                    Thread.Sleep(50);
                    continue;
                }
                var items = RecordInfos.Switch();
                if (items.Count == 0)
                {
                    Thread.Sleep(50);
                    continue;
                }
                var array = items.ToList();
                items.Clear();
                foreach (var info in array)
                {
                    info.Index = ++_id;
                    if (_id == ulong.MaxValue)
                    {
                        _id = 1;
                    }
                    if (!_isTextRecorder && (info.Type >= LogType.System || info.Local))
                    {
                        BaseRecorder.RecordLog(info);
                    }
                    if (Listener != null || TraceToConsole)
                    {
                        DoTrace(info);
                    }
                }
                try
                {
                    Recorder.RecordLog(array);
                }
                catch (Exception ex)
                {
                    SystemTrace(LogLevel.Error, "日志写入发生错误", ex);
                }

                if (++cnt != 24)
                {
                    continue;
                }
                GC.Collect();
                cnt = 0;
            }

            SystemTrace(LogLevel.System, "日志结束");
            syncSlim.Release();
        }