最近在使用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慢一点,因为它每次都会打开关闭文件流。如果确定应从程序只有一个进程个人推荐 ExclusiveLock,如果不确定 用InterProcessLock也不错。
但是这里的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 == ‘"‘) //{ //} } public void WriteQuote() { textWriter.Write(‘"‘); } 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
" />
<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,
" />
<param name="ConversionPattern" value=""%date{yyyy-MM-dd HH:mm:ss}","%message%"
"/>
</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‘.csv‘" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <layout type="log4net.CSVPatternLayout,ConsoleApp"> <header value="Time,Thread,Level,Logger,Message,Exception " /> <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‘.csv‘" /> <lockingModel type="log4net.Appender.FileAppender+ExclusiveLock" /> <layout type="log4net.Layout.PatternLayout"> <header value="Time,Message,Type, " /> <param name="ConversionPattern" value=""%date{yyyy-MM-dd HH:mm:ss}","%message%" "/> </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>