十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
小编给大家分享一下c#中log4Net高性能写入和CSV格式的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
创新互联建站长期为上1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为祁门企业提供专业的网站设计、做网站,祁门网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。最近在使用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 有多个工作者) 如:
但是这里的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 { ////// 记录消息Queue /// private readonly ConcurrentQueue_que; /// /// 信号 /// private readonly ManualResetEvent _mre; ////// 日志 /// private readonly ILog _log; ////// 日志 /// private static QueueLogger flashLog = new QueueLogger(); private QueueLogger() { // 设置日志配置文件路径 //XmlConfigurator.Configure(new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config"))); _que = new ConcurrentQueue(); _mre = new ManualResetEvent(false); _log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); Task.Run(() => { WriteLog(); }); } /// /// 从队列中写日志至磁盘 /// 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(); } } ////// 写日志 /// /// 日志文本 /// 等级 /// Exception 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); } } ////// 日志等级 /// public enum QueueLogLevel { Debug, Info, Error, Warn, Fatal } ////// 日志内容 /// 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(); } } }
配置文件中需要加上逗号
这里 是\r\n,%newfield是一个逗号,%endrow是逗号+换行
看到这里其实我们可以自己拼接CSV的内容,也就是说只要有,\r\n就可以了
调用code:
StringBuilder sb = new StringBuilder(); sb.Append("test"); sb.Append("\",\""); sb.Append("debug"); QueueLogger.Debug(sb.ToString());
写入的信息是test","debug,在加上ConversionPattern里面的配置就是"test","debug".整个配置如下:
以上是“c#中log4Net高性能写入和CSV格式的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!