/// <summary> /// 写出到控制台 /// </summary> /// <param name="type">类型</param> /// <param name="message">消息</param> private void WriteConsole(LogType type, string message) { try { message = Cons.FormatLine(message); //处理日志信息(换行) Cons.SetColor(GetColor(type), ConsoleColor.Black); Console.WriteLine(LOG_FORMAT, DateTime.Now.ToString(TIME_FORMAT), type.ToString(), message); Cons.ResetColor(); //取消单独线程输出日志文件(单独线程输出日志必然会有延迟) //if (IsWriteFile) Queue.Enqueue(new LogModel() { Type = type, Message = message, CreateTime = DateTime.Now }); } catch { } }
/// <summary> /// 写出到日志文件 /// </summary> /// <param name="log"></param> private void WriteFile(LogModel log) { lock (LogFileLock) { //设置日志目录和日志文件 string filePath = GetFilePath(log.Type); string file = DirTool.Combine(filePath, DateTime.Now.ToString("yyyy-MM-dd") + ".txt"); //创建日志目录 DirTool.Create(filePath); //处理日志信息(换行) log.Message = Cons.FormatLine(log.Message); //写出日志 TxtTool.Append( file, string.Format(LOG_FORMAT, log.CreateTime.ToString(TIME_FORMAT), log.Type.ToString(), log.Message)); Cleaner(log.Type); } }