public AsyncLogEventArgs(string msg, string t = "", AsyncLogLevel lv = AsyncLogLevel.INFO, object obj = null) { message = msg; time = DateTime.Now; title = t; level = lv; tagObj = obj; }
/// <summary> /// /// </summary> /// <param name="pathForFileSave">a parent path,path will be appended a \logs dir end</param> /// <param name="logsNumForEachFile">define how many logs in which file,suggested num is around how many logs create pre second</param> /// <param name="logsContinerNum">nums of logs write buffer</param> public XLog(string pathForFileSave = null, int logsNumForEachFile = 100, int logsContinerNum = 0) { folderPath = pathForFileSave ?? @"\logs"; if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } buffSize = logsNumForEachFile; tf = new TaskFactory(); continerSize = logsContinerNum == 0 ? Convert.ToInt32(Math.Ceiling((double)(10000 / logsNumForEachFile))) : logsContinerNum; continer = new BufferContiner[++continerSize]; contineIndex = 0; continer[0] = new BufferContiner(buffSize); outputLevel = AsyncLogLevel.ALL; }
/// <summary> /// add a log,and log can be auto saved based on logsNumForEachFile num /// </summary> /// <param name="message">log content</param> /// <param name="title">title</param> /// <param name="level">level</param> /// <param name="obj">extend filed</param> public void Add(string message, string title = "", AsyncLogLevel level = AsyncLogLevel.INFO, object obj = null) { try { if ((int)outputLevel > (int)level) { return; } bool isfull = false; lock (continer) { isfull = continer[contineIndex].TryAdd(new AsyncLogEventArgs(message, title, level, obj)); } if (isfull) { Dump(); } } catch { } }