public FileLogger(string logDir) { _logRootDir = logDir; list.Add(this); Directory.CreateDirectory(_logRootDir); _DeleteOldLogFiles(); DateTime currTime = DateTime.Now; string date = string.Format("{0}{1:00}{2:00}_{3:00}{4:00}{5:00}", currTime.Year, currTime.Month, currTime.Day, currTime.Hour, currTime.Minute, currTime.Second); _currFileTitleName = _logRootDir + "/log_" + date; try { _writer = File.CreateText(_currFileTitleName + ".txt"); _writer.AutoFlush = true; } catch (Exception e) { EADebug.Error(e.Message); } }
bool _WriteText(string text) { try { _writer.Write(text); _accumSize += text.Length; if (_accumSize > SizeForAutoFlush) { _accumSize = 0; _writer.Flush(); } } catch (Exception e) { EADebug.Error(e.Message); return(false); } return(true); }
public void Log(LogType type, LogTag tag, string logString, int colorTag = -1) { if (_hasError) { return; } DateTime currTime = DateTime.Now; #if SERVER logString = string.Format("{0:00}:{1:00}:{2:00} {3} \r\n", currTime.Hour, currTime.Minute, currTime.Second, logString); if (_writer != null && _WriteText(logString)) { return; } #else char logTypeC = (type == LogType.Log) ? 'I' : (type == LogType.Warning || type == LogType.Assert) ? 'W' : 'E'; if (tag != null) { logString = string.Format("{0:00}:{1:00}:{2:00} {3} [{4}] {5}\r\n", currTime.Hour, currTime.Minute, currTime.Second, logTypeC, tag.Name, logString); } else { logString = string.Format("{0:00}:{1:00}:{2:00} {3} {4}\r\n", currTime.Hour, currTime.Minute, currTime.Second, logTypeC, logString); } if (_writer != null && _WriteText(logString)) { return; } if (_writer != null) { _writer.Dispose(); _writer = null; } try { _writer = new StreamWriter(_currFileTitleName + (_reopenCnt == 0 ? string.Empty : "_" + _reopenCnt) + ".txt", true); } catch (Exception e2) { EADebug.Error(e2.Message); } if (_writer == null) { _reopenCnt++; try { _writer = File.CreateText(_currFileTitleName + (_reopenCnt == 0 ? string.Empty : "_" + _reopenCnt) + ".txt"); _writer.AutoFlush = false; } catch (Exception e3) { EADebug.Error(e3.Message); } } if (_writer != null) { _WriteText(logString); } if (_writer == null) { _hasError = true; } #endif }