/// <summary> /// 日志记录到txt中。 /// </summary> /// <param name="msg">日志信息</param> /// <param name="category">自定义类别</param> /// <param name="level">日志等级:Info,Warn,Error,Fatal,Debug</param> /// <param name="user"></param> /// <param name="ip"></param> public virtual void LogToTxt(string msg, string category = "", EnLogLevel level = EnLogLevel.Info, string user = "", string ip = "") { var threadId = Thread.CurrentThread.ManagedThreadId; StringBuilder sb = new StringBuilder(); sb.AppendFormat("[Thread]:{0} [Recorder]:{1} [Msg]:{2} ", threadId, Recorder, msg); if (!string.IsNullOrWhiteSpace(category)) { sb.AppendFormat("[Category]:{0}", category); } if (level != EnLogLevel.Info) { sb.AppendFormat("[Level]:{0}", level.ToString()); } if (!string.IsNullOrWhiteSpace(user)) { sb.AppendFormat("[User]:{0}", user); } if (!string.IsNullOrWhiteSpace(ip)) { sb.AppendFormat("[Ip]:{0}", ip); } if (AsynThreadCount <= 0) {//同步处理 DoInsertLogToTxt(sb.ToString()); } else {//异步处理 LogQueue.Add(sb.ToString()); } }
private static void Log(string requestId, EnLogLevel logLevel, string className, string methodName, string message, string exception = "", bool skipNotification = false) { Task.Run(() => { const string LOG_FORMAT = "{0}:{1}:{2}"; const int MAX_LEN = 3850; message = string.Format(LOG_FORMAT, className, methodName, message); if (message.Length > MAX_LEN) { message = message.Substring(0, MAX_LEN); } TeamDbContext dbContext = new TeamDbContext(); dbContext.AppLogEntries.Add(new AppLogEntry { RequestId = requestId, Date = DateTime.Now, User = Utility.CurrentUserId, Level = logLevel.ToString(), Message = message, ExceptionMessage = exception, AdminNotified = skipNotification }); dbContext.SaveChanges(); }); }
override protected void WriteLog(EnLogLevel Level, string Message) { // format our data string LevelString = "[" + Level.ToString() + "]"; LevelString = LevelString.PadRight(LOG_TYPE_COL_WIDTH, ' '); string Line = String.Format("{0} {1}{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffzzz"), LevelString, Message); // if this is a warning or worse, add it to our errors list so we can email it later if (BufferErrors && (Level == EnLogLevel.ERROR || Level == EnLogLevel.EXCEPTION)) { Errors.Add(Line); } this.writeLine(Line); }
/// <summary> /// 日志记录到Db中。 /// </summary> public virtual void LogToDb(string msg, string category = "", EnLogLevel level = EnLogLevel.Info, string user = "", string ip = "") { var sqlParams = new LogModal { Message = msg, Recorder = Recorder, LogLevel = level.ToString(), LogCategory = category, CreateTime = DateTime.Now, Thread = Thread.CurrentThread.ManagedThreadId, LogUser = user, Ip = ip }; if (AsynThreadCount <= 0) {//同步处理 DoInsertLogToDb(InsertLogSql, sqlParams); } else {//异步处理 LogQueue.Add(sqlParams); } }