分享

自己最近写的一组日志处理类(支持高并发处理)

 ThinkTank_引擎 2014-09-24

[csharp]
/// <summary>  
    /// 文件日志处理类,利用队列机制,让写日志调用和日志写到文件分离,调用  
    /// 方将要写的日志和目标文件插入到日志队列中去就返回,  
    /// 然后由内置线程去写到文件里去。这里用了单例模式。  
    /// </summary>  
    public class FileLogWriter : IDisposable 
    { 
        private class LocalLockObject 
        { 
        } 
        private  Dictionary<string, FileLogWorker> _Workers = null; 
        private Thread _writerThread = null; 
        static FileLogWriter() 
        { 
            Instance = new FileLogWriter(); 
        } 
        private FileLogWriter() 
        { 
            _Workers = new Dictionary<string, FileLogWorker>(); 
            _writerThread = new Thread(new ThreadStart(Execute)); 
            FStop = false; 
            _writerThread.Start(); 
        } 
        private FileLogWorker GetFileLogWorker(string FullPath) 
        { 
            try 
            { 
                return _Workers[FullPath]; 
            } 
            catch 
            { 
                lock (typeof(LocalLockObject)) 
                { 
                    if (_Workers.ContainsKey(FullPath) == false) 
                    { 
                        FileLogWorker theWorker = new FileLogWorker(FullPath); 
                        _Workers.Add(FullPath, theWorker); 
                        return theWorker; 
                    } 
                    else 
                    { 
                        return _Workers[FullPath]; 
                    } 
                } 
            } 
        } 
        
        private bool FStop = false;  
         
         
        public static FileLogWriter Instance { get; private set; } 
        private const int Timeout = 30;  
         
        /// <summary>  
        /// 向文件日志写日志内容 www.   
        /// </summary>  
        /// <param name="fileName">日志文件名</param>  
        /// <param name="logContent">日志内容</param>  
        public void WriteLogFile(string fileName, string logContent) 
        { 
            FileLogWorker theWorker = GetFileLogWorker(fileName); 
            theWorker.WriteLogContent(logContent); 
        } 
        /// <summary>  
        /// 清理20分钟没有发生读写的文件流。对于日志文件按天产生的非常有用。  
        /// </summary>  
        public void Execute() 
        { 
            while (FStop == false) 
            { 
                try 
                { 
 
                    lock (typeof(LocalLockObject)) 
                    { 
                        foreach (var theItem in _Workers) 
                        { 
                            if (DateTime.Now.Subtract(theItem.Value.LastExecTime).Minutes > Timeout) 
                            { 
                                theItem.Value.CloseLogThread(); 
                                _Workers.Remove(theItem.Key); 
                            } 
                        } 
                    } 
                    Thread.Sleep(100000); 
                } 
                catch (Exception ex) 
                { 
                    SysAppEventWriter.WriteEvent(-1, ex.Message, System.Diagnostics.EventLogEntryType.Error); 
                } 
            } 
        } 
        /// <summary>  
        /// 结束日志内置线程,并关闭所有文件流。程序正常退出时调用.  
        /// </summary>  
        public void CloseAll() 
        { 
            FStop = true; 
            if (_writerThread != null) 
            { 
                _writerThread.Join(); 
            } 
            try 
            { 
                lock (typeof(LocalLockObject)) 
                { 
                    if (_Workers != null) 
                    { 
                        foreach (var theItem in _Workers) 
                        { 
                            theItem.Value.CloseLogThread(); 
                            _Workers.Remove(theItem.Key); 
                        } 
                    } 
                } 
            } 
            catch (Exception ex) 
            { 
                SysAppEventWriter.WriteEvent(-1, ex.Message, System.Diagnostics.EventLogEntryType.Error); 
            } 
        } 
 
        public void Dispose() 
        { 
            try 
            { 
                CloseAll(); 
            } 
            catch 
            { 
            } 
        } 
        ~FileLogWriter() 
        { 
            try 
            { 
                CloseAll(); 
            } 
            catch 
            { 
            } 
        } 
    } 
/// <summary>
    /// 文件日志处理类,利用队列机制,让写日志调用和日志写到文件分离,调用
    /// 方将要写的日志和目标文件插入到日志队列中去就返回,
    /// 然后由内置线程去写到文件里去。这里用了单例模式。
    /// </summary>
    public class FileLogWriter : IDisposable
    {
        private class LocalLockObject
        {
        }
        private  Dictionary<string, FileLogWorker> _Workers = null;
        private Thread _writerThread = null;
        static FileLogWriter()
        {
            Instance = new FileLogWriter();
        }
        private FileLogWriter()
        {
            _Workers = new Dictionary<string, FileLogWorker>();
            _writerThread = new Thread(new ThreadStart(Execute));
            FStop = false;
            _writerThread.Start();
        }
        private FileLogWorker GetFileLogWorker(string FullPath)
        {
            try
            {
                return _Workers[FullPath];
            }
            catch
            {
                lock (typeof(LocalLockObject))
                {
                    if (_Workers.ContainsKey(FullPath) == false)
                    {
                        FileLogWorker theWorker = new FileLogWorker(FullPath);
                        _Workers.Add(FullPath, theWorker);
                        return theWorker;
                    }
                    else
                    {
                        return _Workers[FullPath];
                    }
                }
            }
        }
      
        private bool FStop = false;
       
       
        public static FileLogWriter Instance { get; private set; }
        private const int Timeout = 30;
       
        /// <summary>
        /// 向文件日志写日志内容
        /// </summary>
        /// <param name="fileName">日志文件名</param>
        /// <param name="logContent">日志内容</param>
        public void WriteLogFile(string fileName, string logContent)
        {
            FileLogWorker theWorker = GetFileLogWorker(fileName);
            theWorker.WriteLogContent(logContent);
        }
        /// <summary>
        /// 清理20分钟没有发生读写的文件流。对于日志文件按天产生的非常有用。
        /// </summary>
        public void Execute()
        {
            while (FStop == false)
            {
                try
                {

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多