/// <summary> /// 布局一条日志文本记录 /// </summary> /// <param name="item">日志信息对象</param> /// <returns>日志文本记录</returns> protected string LayoutLog(LogItem item) { string logMsg = string.Empty; try { string layoutFormat = this._config.Layout; List <object> args = new List <object>(); int index = 0; string tmp; //时间 if (layoutFormat.Contains(LogConstant.TIME)) { layoutFormat = layoutFormat.Replace(LogConstant.TIME, string.Format("{{{0}}}", index++)); tmp = item.Time.ToString(this._config.DateFormat); args.Add(tmp); } //日志级别 if (layoutFormat.Contains(LogConstant.LEVEL)) { layoutFormat = layoutFormat.Replace(LogConstant.LEVEL, string.Format("{{{0}}}", index++)); args.Add(this._config.GetLogLevelName(item.Level)); } //事件ID if (layoutFormat.Contains(LogConstant.EVENT)) { layoutFormat = layoutFormat.Replace(LogConstant.EVENT, string.Format("{{{0}}}", index++)); args.Add(item.EventID); } if (layoutFormat.Contains(LogConstant.TAG)) { layoutFormat = layoutFormat.Replace(LogConstant.TAG, string.Format("{{{0}}}", index++)); args.Add(item.Tag); } //线程 if (layoutFormat.Contains(LogConstant.THREAD)) { layoutFormat = layoutFormat.Replace(LogConstant.THREAD, string.Format("{{{0}}}", index++)); if (string.IsNullOrWhiteSpace(item.ThreadName)) { tmp = item.ThreadID.ToString(); } else { tmp = item.ThreadName; } args.Add(tmp); } //内容 if (layoutFormat.Contains(LogConstant.CONTENT)) { layoutFormat = layoutFormat.Replace(LogConstant.CONTENT, string.Format("{{{0}}}", index++)); args.Add(item.Content); } //堆栈位置信息 if (layoutFormat.Contains(LogConstant.STACKTRACE)) { layoutFormat = layoutFormat.Replace(LogConstant.STACKTRACE, string.Format("{{{0}}}", index++)); args.Add(item.StackTraceInfo); } //生成日志 if (args.Count > 0) { logMsg = string.Format(layoutFormat, args.ToArray()); } else { logMsg = item.Message; } } catch (Exception ex) { LogSysInnerLog.OnRaiseLog("LayoutManager", ex); logMsg = item.ToString(); } return(logMsg); }
/// <summary> /// 构造函数 /// </summary> /// <param name="appenderName">日志追加器名称</param> /// <param name="item">日志信息项</param> public RedirectOuputItem(string appenderName, LogItem item) : base() { this.AppenderName = appenderName; this.Item = item; }
/// <summary> /// 写日志 /// </summary> /// <param name="item">日志项</param> protected override void PrimitiveWriteLog(LogItem item) { throw new NotImplementedException(); }
/// <summary> /// 输出日志 /// </summary> /// <param name="appenderName">日志追加器名称</param> /// <param name="logItem">日志项</param> internal static void Output(string appenderName, LogItem logItem) { _logOutputQueue.Enqueue(new RedirectOuputItem(appenderName, logItem)); _logOutputAutoResetEvent.Set(); }