1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- 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<LogItem> _logQueue;
- LogWriter _writer;
- public LogManager()
- {
- _logQueue = new FixSizeQueue<LogItem>(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));
- }
- }
- }
|