Rumah  >  Artikel  >  pembangunan bahagian belakang  >  log4Net 高性能写入和CSV格式的实例详解

log4Net 高性能写入和CSV格式的实例详解

Y2J
Y2Jasal
2017-04-25 09:21:433208semak imbas

最近在使用log4net,在使用之前我们必须知道文件流是如何操作的,否则就是盲人摸向。。。,在FileAppender.cs文件里面有LockingModelBase来控制流的锁,默认有3个子类

ExclusiveLock:默认的,Hold an exclusive lock on the output file,Open the file once for writing and hold it open until CloseFile is called.  Maintains an exclusive lock on the file during this time.

MinimalLock:Acquires the file lock for each write,Opens the file once for each AcquireLock / ReleaseLock cycle,  thus holding the lock for the minimal amount of time.This method of locking is considerably slower than FileAppender.ExclusiveLock but allows  other processes to move/delete the log file whilst logging continues.

InterProcessLock:Provides cross-process file locking.使用Mutex来实现多进程 

这里意思是MinimalLock比ExclusiveLock慢一点,因为它每次都会打开关闭文件流。

不过有2个类感觉比较重要PatternString.cs 


和PatternLayout.cs




如果log文件在一个公共的目录,建议大家log文件加上计算机名称、应用程序名称、进程ID(如web 有多个工作者) 如:   

<file type="log4net.Util.PatternString" value="\\192.168.0.1\logs\%env{COMPUTERNAME}\%appsetting{ApplicationName}\%processid\Log\" />

但是这里的log记录默认都是采用同步方式的,但是我个人更趋向用异步多线程的思路来写log,首先log的信息记录在内存ConcurrentQueue里面,然后在通过一个后台线程把ConcurrentQueue里面的东西记录到文件流里面。至于性能高出多少我想就不用多说了吧,写内存肯定比写流快啊

具体实现code如下:

[assembly: log4net.Config.XmlConfigurator(Watch = true, ConfigFile = "log4net.config")]namespace ConsoleApp
{    using log4net;    using System;    using System.Collections.Concurrent;    using System.Threading;    using System.Threading.Tasks;    public sealed class QueueLogger
    {        /// <summary>
        /// 记录消息Queue        /// </summary>
        private readonly ConcurrentQueue<QueueLogMessage> _que;        /// <summary>
        /// 信号        /// </summary>
        private readonly ManualResetEvent _mre;        /// <summary>
        /// 日志        /// </summary>
        private readonly ILog _log;        /// <summary>
        /// 日志        /// </summary>
        private static QueueLogger flashLog = new QueueLogger();        private QueueLogger()
        {            // 设置日志配置文件路径            //XmlConfigurator.Configure(new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config")));
            _que = new ConcurrentQueue<QueueLogMessage>();
            _mre = new ManualResetEvent(false);
            _log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            Task.Run(() => { WriteLog(); });
        }        /// <summary>
        /// 从队列中写日志至磁盘        /// </summary>
        private void WriteLog()
        {            while (true)
            {                // 等待信号通知                _mre.WaitOne();
                QueueLogMessage msg;                // 判断是否有内容需要如磁盘 从列队中获取内容,并删除列队中的内容
                while (_que.Count > 0 && _que.TryDequeue(out msg))
                {                    // 判断日志等级,然后写日志
                    switch (msg.Level)
                    {                        case QueueLogLevel.Debug:
                            _log.Debug(msg.Message, msg.Exception);                            break;                        case QueueLogLevel.Info:
                            _log.Info(msg.Message, msg.Exception);                            break;                        case QueueLogLevel.Error:
                            _log.Error(msg.Message, msg.Exception);                            break;                        case QueueLogLevel.Warn:
                            _log.Warn(msg.Message, msg.Exception);                            break;                        case QueueLogLevel.Fatal:
                            _log.Fatal(msg.Message, msg.Exception);                            break;
                    }
                }                // 重新设置信号                _mre.Reset();
            }
        }        /// <summary>
        /// 写日志        /// </summary>
        /// <param name="message">日志文本</param>
        /// <param name="level">等级</param>
        /// <param name="ex">Exception</param>
        public void EnqueueMessage(string message, QueueLogLevel level, Exception ex = null)
        {            if ((level == QueueLogLevel.Debug && _log.IsDebugEnabled)             || (level == QueueLogLevel.Error && _log.IsErrorEnabled)             || (level == QueueLogLevel.Fatal && _log.IsFatalEnabled)             || (level == QueueLogLevel.Info && _log.IsInfoEnabled)             || (level == QueueLogLevel.Warn && _log.IsWarnEnabled))
            {
                _que.Enqueue(new QueueLogMessage
                {                    // Message = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss,fff") + "]\r\n" + message,
                    Message = message,
                    Level = level,
                    Exception = ex
                });                // 通知线程往磁盘中写日志                _mre.Set();
            }
        }        public static void Debug(string msg, Exception ex = null)
        {
            flashLog.EnqueueMessage(msg, QueueLogLevel.Debug, ex);
        }        public static void Error(string msg, Exception ex = null)
        {
            flashLog.EnqueueMessage(msg, QueueLogLevel.Error, ex);
        }        public static void Fatal(string msg, Exception ex = null)
        {
            flashLog.EnqueueMessage(msg, QueueLogLevel.Fatal, ex);
        }        public static void Info(string msg, Exception ex = null)
        {
            flashLog.EnqueueMessage(msg, QueueLogLevel.Info, ex);
        }        public static void Warn(string msg, Exception ex = null)
        {
            flashLog.EnqueueMessage(msg, QueueLogLevel.Warn, ex);
        }
    }    /// <summary>
    /// 日志等级    /// </summary>
    public enum QueueLogLevel
    {
        Debug,
        Info,
        Error,
        Warn,
        Fatal
    }    /// <summary>
    /// 日志内容    /// </summary>
    public class QueueLogMessage
    {        public string Message { get; set; }        public QueueLogLevel Level { get; set; }        public Exception Exception { get; set; }
    }
}

 至于CSV格式有2中方法 实现,一是自定义PatternLayout类:

namespace log4net
{    using Layout;    using System.IO;    using System.Text;    using Util;    using Core;    public class CSVPatternLayout : PatternLayout
    {        public override void ActivateOptions()
        {
            AddConverter("newfield", typeof(CSVNewFiledConverter));
            AddConverter("endrow", typeof(CSVEndRowConverter));            base.ActivateOptions();
        }        public override void Format(TextWriter writer, LoggingEvent loggingEvent)
        {            var csvWriter = new CSVTextWriter(writer);
            csvWriter.WriteQuote();            base.Format(csvWriter, loggingEvent);
        }
    }    public class CSVTextWriter : TextWriter
    {        private readonly TextWriter textWriter;        public CSVTextWriter(TextWriter txtWriter)
        {
            textWriter = txtWriter;
        }        public override void Write(char value)
        {            // base.Write(value);            textWriter.Write(value);            //if (value == &#39;"&#39;)            //{            //}        }        public void WriteQuote()
        {
            textWriter.Write(&#39;"&#39;);
        }        public override Encoding Encoding
        {            get
            {                return textWriter.Encoding;
            }
        }
    }    public class CSVNewFiledConverter : PatternConverter
    {        protected override void Convert(TextWriter writer, object state)
        {            var csvWriter = writer as CSVTextWriter;
            csvWriter?.WriteQuote();
            writer.Write(",");
            csvWriter?.WriteQuote();
        }
    }    public class CSVEndRowConverter : PatternConverter
    {        protected override void Convert(TextWriter writer, object state)
        {            var csvWriter = writer as CSVTextWriter;
            csvWriter?.WriteQuote();
            writer.WriteLine();
        }
    }
}

配置文件中需要加上逗号

<layout type="log4net.CSVPatternLayout,ConsoleApp">
<header value="Time,Thread,Level,Logger,Message,Exception&#13;&#10;" />
<conversionPattern value="%date{yyyy-MM-dd HH:mm:ss}
%newfield
%thread%newfield%level%newfield%logger%newfield%message%newfield%exception
%endrow
" />
</layout>

这里 是\r\n,%newfield是一个逗号,%endrow是逗号+换行

看到这里其实我们可以自己拼接CSV的内容,也就是说只要有,\r\n就可以了

<layout type="log4net.Layout.PatternLayout">
<header value="Time,Message,Type
,&#13;&#10;
" />
<param name="ConversionPattern" value="
&quot;%date{yyyy-MM-dd HH:mm:ss}&quot;,&quot;%message%&quot;&#13;&#10;
"/>
</layout>

调用code:

StringBuilder sb = new StringBuilder();
sb.Append("test");
sb.Append("\",\"");
sb.Append("debug");
QueueLogger.Debug(sb.ToString());

写入的信息是test","debug,在加上ConversionPattern里面的配置就是"test","debug".整个配置如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <appender name="InfoLog" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Log\Info\Info" />
      <param name="AppendToFile" value="True" />
      <appendToFile value="true" />
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="false" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyyMMdd&#39;.csv&#39;" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.CSVPatternLayout,ConsoleApp">
        <header value="Time,Thread,Level,Logger,Message,Exception&#13;&#10;" />
        <conversionPattern  value="%date{yyyy-MM-dd HH:mm:ss}%newfield%thread%newfield%level%newfield%logger%newfield%message%newfield%exception%endrow" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="INFO" />
        <param name="LevelMax" value="INFO" />
      </filter>
    </appender>
    <appender name="DebugLog" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="Log\Debug\Debug" />
      <appendToFile value="true" />
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="false" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyyMMdd&#39;.csv&#39;" />
      <lockingModel type="log4net.Appender.FileAppender+ExclusiveLock" />
      <layout type="log4net.Layout.PatternLayout">
        <header value="Time,Message,Type,&#13;&#10;" />
        <param name="ConversionPattern" value="&quot;%date{yyyy-MM-dd HH:mm:ss}&quot;,&quot;%message%&quot;&#13;&#10;"/>
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG" />
        <param name="LevelMax" value="DEBUG" />
      </filter>
    </appender>
    <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
      <mapping>
        <level value="ERROR" />
        <foreColor value="Red" />
      </mapping>
      <mapping>
        <level value="INFO" />
        <foreColor value="Green" />
      </mapping>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="# %date{HH:mm:ss} [%thread] %-5level %logger #%newline%message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG" />
        <param name="LevelMax" value="FATAL" />
      </filter>
    </appender>
    <root>
      <!-- OFF < FATAL < ERROR < WARN < INFO < DEBUG < ALL -->
      <level value="ALL" />
      <appender-ref ref="InfoLog" />
      <appender-ref ref="DebugLog" />
      <!-- 
        <appender-ref ref="ColoredConsoleAppender" />
      -->
    </root>
  </log4net>
</configuration>

 

Atas ialah kandungan terperinci log4Net 高性能写入和CSV格式的实例详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn