123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Aitex.Core.Util;
- using log4net.Core;
- using System.Diagnostics;
- using System.IO;
- namespace Aitex.Core.RT.Log
- {
- public class LogManager : ICommonLog
- {
- public const int MaxLogsMonth = 3;
- PeriodicJob _loggingJob;
- PeriodicJob _threadDeleteLogs;
- FixSizeQueue<LogItem> _logQueue;
- LogWriter _writer;
- public LogManager()
- {
- }
- public void Initialize()
- {
- _logQueue = new FixSizeQueue<LogItem>(1000);
- _loggingJob = new PeriodicJob(300, this.PeriodicRun, "Save Log Job", true);
- _threadDeleteLogs = new PeriodicJob(1000 * 60 * 60 * 24, OnDeleteLog, "Delete Log Thread", true);
- _writer = new LogWriter();
- LOG.InnerLogger = this;
- }
- public void Terminate()
- {
- try
- {
- if (_loggingJob != null)
- {
- _loggingJob.Stop();
- _loggingJob = null;
- }
- }
- catch (Exception ex)
- {
- System.Diagnostics.Trace.WriteLine(ex.Message);
- }
- }
- public void Info(string message, bool isTraceOn)
- {
- 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;
- }
- bool OnDeleteLog()
- {
- try
- {
- string path = string.Format(@"{0}", "Logs");
- FileInfo[] fileInfos;
- DirectoryInfo curFolderInfo = new DirectoryInfo(path);
- fileInfos = curFolderInfo.GetFiles();
- foreach (FileInfo info in fileInfos)
- {
- if (info.Name.Contains("appLog") || (info.Name.Contains("log")) && info.Extension == ".txt")
- {
- DateTime lastWriteTime = DateTime.Parse(info.LastWriteTime.ToShortDateString());
- DateTime intervalTime = DateTime.Now.AddMonths(-MaxLogsMonth);// DateTime.Parse(DateTime.Now.AddMonths(-1).ToShortDateString());
- if (lastWriteTime < intervalTime)
- {
- File.Delete(info.FullName);
- //LOG.Write(string.Format("【log】自动删除log成功,logName:{0}", info.Name));
- }
- }
- }
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- }
- return true;
- }
- void CacheLog(string message, Level level, Exception exception, bool isTraceOn)
- {
- if (isTraceOn)
- {
- //System.Diagnostics.Trace.WriteLine(message + (exception == null ? "" : exception.Message));
- }
- StackTrace st = new StackTrace(true);
- StackFrame sf = st.GetFrame(3);
-
- _logQueue.Enqueue(new LogItem(message, sf, level, exception));
- }
- }
- }
|