利用StackExchange.Redis和Log4Net構建日誌隊列

簡介:本文是一個簡單的demo用於展現利用StackExchange.Redis和Log4Net構建日誌隊列,爲高併發日誌處理提供一些思路。html

0、先下載安裝Redis服務,而後再服務列表裏啓動服務(Redis的默認端口是6379,貌似還有一個故事)(https://github.com/MicrosoftArchive/redis/releases)git

 

一、nuget中安裝Redis:Install-Package StackExchange.Redis -version 1.2.6
二、nuget中安裝日誌:Install-Package Log4Net -version 2.0.8github

三、建立RedisConnectionHelp、RedisHelper類,用於調用Redis。因爲是Demo我不打算用完整類,比較完整的能夠查閱其餘博客(例如:https://www.cnblogs.com/liqingwen/p/6672452.html)redis

/// <summary>
    /// StackExchange Redis ConnectionMultiplexer對象管理幫助類
    /// </summary>
    public class RedisConnectionHelp
    {
        //系統自定義Key前綴
        public static readonly string SysCustomKey = ConfigurationManager.AppSettings["redisKey"] ?? "";
        private static readonly string RedisConnectionString = ConfigurationManager.AppSettings["seRedis"] ?? "127.0.0.1:6379";

        private static readonly object Locker = new object();
        private static ConnectionMultiplexer _instance;
        private static readonly ConcurrentDictionary<string, ConnectionMultiplexer> ConnectionCache = new ConcurrentDictionary<string, ConnectionMultiplexer>();

        /// <summary>
        /// 單例獲取
        /// </summary>
        public static ConnectionMultiplexer Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (Locker)
                    {
                        if (_instance == null || !_instance.IsConnected)
                        {
                            _instance = GetManager();
                        }
                    }
                }
                return _instance;
            }
        }

        /// <summary>
        /// 緩存獲取
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public static ConnectionMultiplexer GetConnectionMultiplexer(string connectionString)
        {
            if (!ConnectionCache.ContainsKey(connectionString))
            {
                ConnectionCache[connectionString] = GetManager(connectionString);
            }
            return ConnectionCache[connectionString];
        }

        private static ConnectionMultiplexer GetManager(string connectionString = null)
        {
            connectionString = connectionString ?? RedisConnectionString;
            var connect = ConnectionMultiplexer.Connect(connectionString);       
            return connect;
        }
    }
View Code
public class RedisHelper
    {
        private int DbNum { get; set; }
        private readonly ConnectionMultiplexer _conn;
        public string CustomKey;

        public RedisHelper(int dbNum = 0)
            : this(dbNum, null)
        {
        }

        public RedisHelper(int dbNum, string readWriteHosts)
        {
            DbNum = dbNum;
            _conn =
                string.IsNullOrWhiteSpace(readWriteHosts) ?
                RedisConnectionHelp.Instance :
                RedisConnectionHelp.GetConnectionMultiplexer(readWriteHosts);
        }

       

        private string AddSysCustomKey(string oldKey)
        {
            var prefixKey = CustomKey ?? RedisConnectionHelp.SysCustomKey;
            return prefixKey + oldKey;
        }

        private T Do<T>(Func<IDatabase, T> func)
        {
            var database = _conn.GetDatabase(DbNum);
            return func(database);
        }

        private string ConvertJson<T>(T value)
        {
            string result = value is string ? value.ToString() : JsonConvert.SerializeObject(value);
            return result;
        }

        private T ConvertObj<T>(RedisValue value)
        {
            Type t = typeof(T);
            if (t.Name == "String")
            {                
                return (T)Convert.ChangeType(value, typeof(string));
            }

            return JsonConvert.DeserializeObject<T>(value);
        }

        private List<T> ConvetList<T>(RedisValue[] values)
        {
            List<T> result = new List<T>();
            foreach (var item in values)
            {
                var model = ConvertObj<T>(item);
                result.Add(model);
            }
            return result;
        }

        private RedisKey[] ConvertRedisKeys(List<string> redisKeys)
        {
            return redisKeys.Select(redisKey => (RedisKey)redisKey).ToArray();
        }

      

        /// <summary>
        /// 入隊
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void ListRightPush<T>(string key, T value)
        {
            key = AddSysCustomKey(key);
            Do(db => db.ListRightPush(key, ConvertJson(value)));
        }      
 

        /// <summary>
        /// 出隊
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T ListLeftPop<T>(string key)
        {
            key = AddSysCustomKey(key);
            return Do(db =>
            {
                var value = db.ListLeftPop(key);
                return ConvertObj<T>(value);
            });
        }

        /// <summary>
        /// 獲取集合中的數量
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public long ListLength(string key)
        {
            key = AddSysCustomKey(key);
            return Do(redis => redis.ListLength(key));
        }


    }
View Code

四、建立log4net的配置文件log4net.config。設置屬性爲:始終複製、內容。緩存

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>

  <log4net>
    <root>
      <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) -->
      <!--級別按以上順序,若是level選擇error,那麼程序中即使調用info,也不會記錄日誌-->
      <level value="ALL" />
      <!--appender-ref能夠理解爲某種具體的日誌保存規則,包括生成的方式、命名方式、展現方式-->
      <appender-ref ref="MyErrorAppender"/>
    </root>

    <appender name="MyErrorAppender" type="log4net.Appender.RollingFileAppender">
      <!--日誌路徑,相對於項目根目錄-->
      <param name= "File" value= "Log\\"/>
      <!--是不是向文件中追加日誌-->
      <param name= "AppendToFile" value= "true"/>
      <!--日誌根據日期滾動-->
      <param name= "RollingStyle" value= "Date"/>
      <!--日誌文件名格式爲:日期文件夾/Error_2019_3_19.log,前面的yyyyMMdd/是指定文件夾名稱-->
      <param name= "DatePattern" value= "yyyyMMdd/Error_yyyy_MM_dd&quot;.log&quot;"/>
      <!--日誌文件名是不是固定不變的-->
      <param name= "StaticLogFileName" value= "false"/>
      <!--日誌文件大小,可使用"KB", "MB" 或 "GB"爲單位-->
      <!--<param name="MaxFileSize" value="500MB" />-->
      <layout type="log4net.Layout.PatternLayout,log4net">
        <!--%n 回車-->
        <!--%d 當前語句運行的時刻,格式%date{yyyy-MM-dd HH:mm:ss,fff}-->
        <!--%t 引起日誌事件的線程,若是沒有線程名就使用線程號-->
        <!--%p 日誌的當前優先級別-->
        <!--%c 當前日誌對象的名稱-->
        <!--%m 輸出的日誌消息-->
        <!--%-數字 表示該項的最小長度,若是不夠,則用空格 -->
        <param name="ConversionPattern" value="========[Begin]========%n%d [線程%t] %-5p %c 日誌正文以下- %n%m%n%n" />
      </layout>
      <!-- 最小鎖定模型,能夠避免名字重疊。文件鎖類型,RollingFileAppender自己不是線程安全的,-->
      <!-- 若是在程序中沒有進行線程安全的限制,能夠在這裏進行配置,確保寫入時的安全。-->
      <!-- 文件鎖定的模式,官方文檔上他有三個可選值「FileAppender.ExclusiveLock, FileAppender.MinimalLock and FileAppender.InterProcessLock」,-->
      <!-- 默認是第一個值,排他鎖定,一次值能有一個進程訪問文件,close後另一個進程才能夠訪問;第二個是最小鎖定模式,容許多個進程能夠同時寫入一個文件;第三個目前還不知道有什麼做用-->
      <!-- 裏面爲何是一個「+」號。。。問得好!我查了好久文件也不知道爲何不是點,而是加號。反正必須是加號-->
      <param name="lockingModel"  type="log4net.Appender.FileAppender+MinimalLock" />

      <!--日誌過濾器,配置能夠參考其餘人博文:https://www.cnblogs.com/cxd4321/archive/2012/07/14/2591142.html -->
      <filter type="log4net.Filter.LevelMatchFilter">
        <LevelToMatch value="ERROR" />
      </filter>
      <!-- 上面的過濾器,其實能夠寫得很複雜,並且能夠多個以or的形式並存。若是符合過濾條件就會寫入日誌,若是不符合條件呢?不是不要了-->
      <!-- 相反是不符合過濾條件也寫入日誌,因此最後加一個DenyAllFilter,使得不符合上面條件的直接否決經過-->
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>
  </log4net>
</configuration>
View Code

五、建立日誌類LoggerFunc、日誌工廠類LoggerFactory安全

/// <summary>
    /// 日誌單例工廠
    /// </summary>
    public class LoggerFactory
    {
        public static string CommonQueueName = "DisSunQueue";
        private static LoggerFunc log;
        private static object logKey = new object();
        public static LoggerFunc CreateLoggerInstance()
        {
            if (log != null)
            {
                return log;
            }

            lock (logKey)
            {
                if (log == null)
                {
                    string log4NetPath = AppDomain.CurrentDomain.BaseDirectory + "Config\\log4net.config";
                    log = new LoggerFunc();
                    log.logCfg = new FileInfo(log4NetPath);
                    log.errorLogger = log4net.LogManager.GetLogger("MyError");
                    log.QueueName = CommonQueueName;//存儲在Redis中的鍵名
                    log4net.Config.XmlConfigurator.ConfigureAndWatch(log.logCfg);    //加載日誌配置文件S                
                }
            }

            return log;
        }
    }
View Code
/// <summary>
    /// 日誌類實體
    /// </summary>
    public class LoggerFunc
    {
        public FileInfo logCfg;
        public log4net.ILog errorLogger;
        public string QueueName;       

        /// <summary>
        /// 保存錯誤日誌
        /// </summary>
        /// <param name="title">日誌內容</param>
        public void SaveErrorLogTxT(string title)
        {
            RedisHelper redis = new RedisHelper();
            //塞進隊列的右邊,表示從隊列的尾部插入。
            redis.ListRightPush<string>(QueueName, title);           
        }

        /// <summary>
        /// 日誌隊列是否爲空
        /// </summary>
        /// <returns></returns>
        public bool IsEmptyLogQueue()
        { 
            RedisHelper redis = new RedisHelper();
            if (redis.ListLength(QueueName) > 0)
            {
                return false;
            }
            return true;        
        }

    }
View Code

六、建立本章最核心的日誌隊列設置類LogQueueConfig。併發

ThreadPool是線程池,經過這種方式能夠減小線程的建立與銷燬,提升性能。也就是說每次須要用到線程時,線程池都會自動安排一個尚未銷燬的空閒線程,不至於每次用完都銷燬,或者每次須要都從新建立。但其實我不太明白他的底層運行原理,在內部while,是讓這個線程一直不被銷燬一直存在麼?仍是說sleep結束後,能夠直接拿到一個線程池提供的新線程。爲何不是在ThreadPool.QueueUserWorkItem以外進行循環調用?瞭解的童鞋能夠給我留下言。app

/// <summary>
    /// 日誌隊列設置類
    /// </summary>
    public class LogQueueConfig
    {
        public static void RegisterLogQueue()
        {
            ThreadPool.QueueUserWorkItem(o =>
            {
                while (true)
                {
                    RedisHelper redis = new RedisHelper();
                    LoggerFunc logFunc = LoggerFactory.CreateLoggerInstance();
                    if (!logFunc.IsEmptyLogQueue())
                    {
                        //從隊列的左邊彈出,表示從隊列頭部出隊
                        string logMsg = redis.ListLeftPop<string>(logFunc.QueueName);

                        if (!string.IsNullOrWhiteSpace(logMsg))
                        {
                            logFunc.errorLogger.Error(logMsg);
                        }
                    }
                    else
                    {
                        Thread.Sleep(1000); //爲避免CPU空轉,在隊列爲空時休息1秒
                    }
                }
            });
        }
    }
View Code

七、在項目的Global.asax文件中,啓動隊列線程。本demo因爲是在winForm中,因此放在form中。
 ide

        public Form1()
        {
            InitializeComponent();
            RedisLogQueueTest.CommonFunc.LogQueueConfig.RegisterLogQueue();//啓動日誌隊列
        }

八、調用日誌類LoggerFunc.SaveErrorLogTxT(),插入日誌。高併發

            LoggerFunc log = LoggerFactory.CreateLoggerInstance();
            log.SaveErrorLogTxT("您插入了一條隨機數:"+longStr);

九、查看下入效果

 

 

十、完整源碼(winForm不懂?差很少的啦,打開項目直接運行就能夠看見界面):

https://gitee.com/dissun/RedisLogQueueTest

 

#### 原創:DisSun ##########

#### 時間:2019.03.19 #######

相關文章
相關標籤/搜索