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(); }); }
/// <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()); } }
/// <summary> /// 日志优先写入Db,当写入Db失败,才会写入txt。如果DoInsertLogToDb为null,则会自动选择写入txt。 /// (这也是最常用的模式,太多日志是不建议写入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 Log(string msg, string category = "", EnLogLevel level = EnLogLevel.Info, string user = "", string ip = "") { if (DoInsertLogToDb != null) { try { LogToDb(msg, category, level, user, ip); } catch (Exception e) { var exMsg = "-------------执行Log中的LogToDb时异常:" + LogHelper.GetExceptionDetailMsg(e); if (!string.IsNullOrWhiteSpace(DirectoryForTxt))//如果写入数据库失败,则写入本地txt { LogToTxt(exMsg); LogToTxt(msg, category, level, user, ip); } else { throw new Exception(exMsg); } } } else if (!string.IsNullOrWhiteSpace(DirectoryForTxt)) { LogToTxt(msg, category, level, user, ip); } }
public LogEntry(EnLogLevel level, string message, Exception ex = null) { this.Level = level; this.Timestamp = DateTime.UtcNow; this.Message = message; this.Seperator = false; this.Exception = ex; this.LineChar = ' '; this.NumChar = 0; }
private static void Log(string requestId, EnLogLevel logLevel, string className, string methodName, Exception ex, bool skipNotification = false) { string exceptionMessage = ex.Message; while (ex.InnerException != null) { ex = ex.InnerException; exceptionMessage += "\n" + ex.Message; } Log(requestId, logLevel, className, methodName, string.Empty, exceptionMessage, skipNotification); }
/// <summary> /// 日志写入Db和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 LogToBoth(string msg, string category = "", EnLogLevel level = EnLogLevel.Info, string user = "", string ip = "") { try { LogToDb(msg, category, level, user, ip); } catch (Exception e) { LogToTxt("-------------执行LogToBoth中的LogToDb时异常:" + e.Message); LogToTxt(msg, category, level, user, ip); return; } LogToTxt(msg, category, level, user, ip); }
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); }
public void Log(EnLogLevel Level, string Message, Exception ex = null) { if (LoggingEnabled) { lock (syncRoot) { if (LogReady) { if (Level >= LogLevel) { LogEntry entry = new LogEntry(Level, Message, ex); WriteLog(entry); } } } } }
public string Log(EnLogLevel Level, string Message) { if (!LoggingEnabled) { return(Message); } lock (syncRoot) { if (LogReady) { if (Level >= LogLevel) { WriteLog(Level, Message); } } } return(Message); }
/// <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); } }
public void Log(EnLogLevel Level, string Message) { Log(Level, Message, null); }
private static void Log(string requestId, EnLogLevel logLevel, string className, string methodName, string message, params object[] args) { Log(requestId, logLevel, className, methodName, string.Format(message, args)); }
public void Log(EnLogLevel Level, string Message) { }
virtual protected void WriteLog(EnLogLevel Level, string Line) { }