Home Backend Development C#.Net Tutorial Detailed examples of log4Net high-performance writing and CSV format

Detailed examples of log4Net high-performance writing and CSV format

Apr 25, 2017 am 09:21 AM
.net c# csv

最近在使用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\" />
Copy after login

但是这里的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; }
    }
}
Copy after login

至于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();
        }
    }
}
Copy after login

配置文件中需要加上逗号

<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>
Copy after login

这里 是\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>
Copy after login

调用code:

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

写入的信息是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>
Copy after login

 

The above is the detailed content of Detailed examples of log4Net high-performance writing and CSV format. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

See all articles