• 大小: 0.26M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2024-04-24
  • 语言: C#
  • 标签: log4net  NET  Log  磁盘  日志  

资源简介

log4net 高速磁盘写日志

资源截图

代码片段和文件信息

using log4net;
using log4net.Config;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Emrys.FlashLog
{
    public sealed class FlashLogger
    {
        /// 
        /// 记录消息Queue
        /// 

        private readonly ConcurrentQueue _que;

        /// 
        /// 信号
        /// 

        private readonly ManualResetEvent _mre;

        /// 
        /// 日志
        /// 

        private readonly ILog _log;

        /// 
        /// 日志
        /// 

        private static FlashLogger _flashLog = new FlashLogger();


        private FlashLogger()
        {
            var configFile = new FileInfo(Path.Combine(AppDomain.CurrentDomain.baseDirectory “log4net.config“));
            if (!configFile.Exists)
            {
                throw new Exception(“未配置log4net配置文件!“);
            }

            // 设置日志配置文件路径
            xmlConfigurator.Configure(configFile);

            _que = new ConcurrentQueue();
            _mre = new ManualResetEvent(false);
            _log = LogManager.GetLogger(System.Reflection.Methodbase.GetCurrentMethod().DeclaringType);
        }

        /// 
        /// 实现单例
        /// 

        /// 
        public static FlashLogger Instance()
        {
            return _flashLog;
        }

        /// 
        /// 另一个线程记录日志,只在程序初始化时调用一次
        /// 

        public void Register()
        {
            Thread t = new Thread(new ThreadStart(WriteLog));
            t.IsBackground = false;
            t.Start();
        }

        /// 
        /// 从队列中写日志至磁盘
        /// 

        private void WriteLog()
        {
            while (true)
            {
                // 等待信号通知
                _mre.WaitOne();

                FlashLogMessage msg;
                // 判断是否有内容需要如磁盘 从列队中获取内容,并删除列队中的内容
                while (_que.Count > 0 && _que.TryDequeue(out msg))
                {
                    // 判断日志等级,然后写日志
                    switch (msg.Level)
                    {
                        case FlashLogLevel.Debug:
                            _log.Debug(msg.Message msg.Exception);
                            break;
                        case FlashLogLevel.Info:
                            _log.Info(msg.Message msg.Exception);
                            break;
                        case FlashLogLevel.Error:
                            _log.Error(msg.Message msg.Exception);
                            break;
                        case FlashLogLevel.Warn:
                            _log.Warn(msg.Message msg.Exception);
                            break;
                        case 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2020-11-09 08:33  高速写日志文件\
     文件        4195  2017-04-13 11:33  高速写日志文件\.gitignore
     目录           0  2020-11-09 08:33  高速写日志文件\Emrys.FlashLog\
     文件        2793  2020-11-06 15:57  高速写日志文件\Emrys.FlashLog\Emrys.FlashLog.csproj
     文件         343  2017-04-13 11:33  高速写日志文件\Emrys.FlashLog\FlashLogLevel.cs
     文件         418  2017-04-13 11:33  高速写日志文件\Emrys.FlashLog\FlashLogMessage.cs
     文件        5431  2017-04-13 11:33  高速写日志文件\Emrys.FlashLog\FlashLogger.cs
     目录           0  2020-11-09 08:33  高速写日志文件\Emrys.FlashLog\Properties\
     文件        1354  2017-04-13 11:33  高速写日志文件\Emrys.FlashLog\Properties\AssemblyInfo.cs
     目录           0  2020-11-09 08:33  高速写日志文件\Emrys.FlashLog\bin\
     目录           0  2020-11-09 08:33  高速写日志文件\Emrys.FlashLog\bin\Debug\
     文件        7680  2020-11-06 15:57  高速写日志文件\Emrys.FlashLog\bin\Debug\Emrys.FlashLog.dll
     文件       15872  2020-11-06 15:57  高速写日志文件\Emrys.FlashLog\bin\Debug\Emrys.FlashLog.pdb
     文件      288768  2011-10-06 19:44  高速写日志文件\Emrys.FlashLog\bin\Debug\log4net.dll
     文件        1045  2017-04-13 11:33  高速写日志文件\Emrys.FlashLog\log4net.config
     目录           0  2020-11-09 08:33  高速写日志文件\Emrys.FlashLog\obj\
     目录           0  2020-11-09 08:33  高速写日志文件\Emrys.FlashLog\obj\Debug\
     文件        6822  2020-11-14 09:46  高速写日志文件\Emrys.FlashLog\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件         602  2020-11-06 15:57  高速写日志文件\Emrys.FlashLog\obj\Debug\Emrys.FlashLog.csproj.FileListAbsolute.txt
     文件        8589  2020-11-06 15:57  高速写日志文件\Emrys.FlashLog\obj\Debug\Emrys.FlashLog.csprojResolveAssemblyReference.cache
     文件        7680  2020-11-06 15:57  高速写日志文件\Emrys.FlashLog\obj\Debug\Emrys.FlashLog.dll
     文件       15872  2020-11-06 15:57  高速写日志文件\Emrys.FlashLog\obj\Debug\Emrys.FlashLog.pdb
     目录           0  2021-01-08 14:33  高速写日志文件\Emrys.FlashLog\obj\Debug\TempPE\
     文件           0  2020-11-06 15:56  高速写日志文件\Emrys.FlashLog\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
     文件           0  2020-11-06 15:56  高速写日志文件\Emrys.FlashLog\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
     文件           0  2020-11-06 15:56  高速写日志文件\Emrys.FlashLog\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
     文件         134  2017-04-13 11:33  高速写日志文件\Emrys.FlashLog\packages.config
     目录           0  2020-11-09 08:33  高速写日志文件\Emrys.FlashLog.Test\
     文件        4739  2017-04-13 11:33  高速写日志文件\Emrys.FlashLog.Test\Emrys.FlashLog.Test.csproj
     目录           0  2020-11-09 08:33  高速写日志文件\Emrys.FlashLog.Test\Properties\
     文件        1364  2017-04-13 11:33  高速写日志文件\Emrys.FlashLog.Test\Properties\AssemblyInfo.cs
............此处省略24个文件信息

评论

共有 条评论