Пример #1
0
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="log">日志配置</param>
        /// <param name="model">信息实体</param>
        private static void WriteModel(Log4netModel model)
        {
            if (model == null)
            {
                return;
            }

            if (model.Log == null)
            {
                return;
            }

            model.Message = string.Format("[{0}]{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), model.Message);

            //写日志
            if (model.Level == Level.Warn)
            {
                model.Log.Warn(model.Message);
            }
            else if (model.Level == Level.Error)
            {
                model.Log.Error(model.Message);
            }
            else
            {
                model.Log.Info(model.Message);
            }
        }
Пример #2
0
        /// <summary>
        /// 异步写日志
        /// </summary>
        private static void AsyncStart()
        {
            if (isStart)
            {
                return;
            }
            isStart = true;

            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    if (logQueue.Count >= 1)
                    {
                        Log4netModel model = null;
                        lock (logLock)
                            model = logQueue.Dequeue();

                        if (model == null)
                        {
                            continue;
                        }

                        if (model.Log == null)
                        {
                            continue;
                        }

                        WriteModel(model);
                    }
                    else
                    {
                        isStart = false; //标记下次可执行
                        break;           //跳出循环
                    }
                }
            });

            //4.5
            //Task.Run(() =>
            //{
            //    while (true)
            //    {
            //        if (logQueue.Count >= 1)
            //        {
            //            Log4netModel model = null;
            //            lock (logLock)
            //                model = logQueue.Dequeue();

            //            if (model == null)
            //                continue;

            //            if (model.Log == null)
            //                continue;

            //            WriteModel(model);
            //        }
            //        else
            //        {
            //            isStart = false;//标记下次可执行
            //            break;//跳出循环
            //        }
            //    }
            //});
        }