using System; using System.Collections.Generic; using System.Linq; using System.Text; using log4net.Core; using System.Diagnostics; using Aitex.DataAnalysis.Core; namespace Aitex.DataAnalysis.Log { public class LogManager : ICommonLog { PeriodicJob _loggingJob; FixSizeQueue _logQueue; LogWriter _writer; public LogManager() { _logQueue = new FixSizeQueue(1000); _loggingJob = new PeriodicJob(200, this.PeriodicRun, "Save Log Job", true); _writer = new LogWriter(); LOG.InnerLogger = this; } public void Terminate() { if (_loggingJob != null) { _loggingJob.Stop(); _loggingJob = null; } } public void Info(string message, bool isTraceOn=true) { CacheLog(message, Level.Info, null, isTraceOn); } public void Warning(string message) { CacheLog(message, Level.Warn, null, true); } public void Error(string message) { CacheLog(message, Level.Error, null, true); } public void Warning(string message, Exception ex) { CacheLog(message, Level.Warn, ex, true); } public void Error(string message, Exception ex) { CacheLog(message, Level.Error, ex, true); } bool PeriodicRun() { LogItem item; while (_logQueue.TryDequeue(out item)) { string log = _writer.Write(item); } return true; } void CacheLog(string message, Level level, Exception exception, bool isTraceOn) { if (isTraceOn) { System.Diagnostics.Trace.WriteLine(message + (exception == null ? "" : exception.Message)); } _logQueue.Enqueue(new LogItem(message, new StackTrace(true).GetFrame(3), level, exception)); } } }