澳门新葡萄京娱乐场LotusPhp笔记之:Logger组件的使用方法

前天比较忙,本来想写多点内容,暂无艺术了,捡个比较轻便的机件来写下好了,现在的几章都以介绍组件。

行使log4net将日志文件输出为csv格式,log4netcsv

  大家在编写程序时,会在程序运维进度中著录一些日志。log4net作为大器晚成款经久耐用的日记组件,值得我们深信。在中型小型型公司中,往往未有正规的日记服务器来管理应用程序产生的日志,而格式化不好的日记文件又为上线前天志的总括、解析、查找形成了不方便。

  Excel作为朝气蓬勃款常用的办公软件,用来处理局地比相当小超大数量级的多寡或许张弛有度的。假如log4net输出的日记能够一直导入Excel,那么查询和深入分析起来岂不是要快超多?

  大家有好些个办法能够完结这一个意义,csv的优势是其文件格式比较容易,可以用随机的文件编辑器张开,并且剖析起来比较有利。效果如下:

1 using System.IO; 2 using System.Text; 3 4 namespace
CoderBusy.Log4Net.Layout 5 { 6 public class CsvTextWriter : TextWriter 7
{ 8 private readonly TextWriter _textWriter; 9 10 public
CsvTextWriter(TextWriter textWriter) 11 { 12 _textWriter = textWriter;
13 } 14 15 public override Encoding Encoding =>
_textWriter.Encoding; 16 17 public override void Write(char value) 18 {
19 _textWriter.Write(value); 20 if (value == ‘”‘) 21
_textWriter.Write(value); 22 } 23 24 public void WriteQuote() 25 { 26
_textWriter.Write(‘”‘); 27 } 28 } 29 }

NewFieldConverter.cs

 1 using System.IO;
 2 using log4net.Util;
 3 
 4 namespace CoderBusy.Log4Net.Layout
 5 {
 6     public class NewFieldConverter : PatternConverter
 7     {
 8         protected override void Convert(TextWriter writer, object state)
 9         {
10             var ctw = writer as CsvTextWriter;
11             ctw?.WriteQuote();
12 
13             writer.Write(',');
14 
15             ctw?.WriteQuote();
16         }
17     }
18 }

EndRowConverter.cs

 1 using System.IO;
 2 using log4net.Util;
 3 
 4 namespace CoderBusy.Log4Net.Layout
 5 {
 6     public class EndRowConverter : PatternConverter
 7     {
 8         protected override void Convert(TextWriter writer, object state)
 9         {
10             var ctw = writer as CsvTextWriter;
11 
12             ctw?.WriteQuote();
13 
14             writer.WriteLine();
15         }
16     }
17 }

CsvPatternLayout.cs

 1 using System.IO;
 2 using log4net.Core;
 3 using log4net.Layout;
 4 
 5 namespace CoderBusy.Log4Net.Layout
 6 {
 7     public class CsvPatternLayout : PatternLayout
 8     {
 9         public override void ActivateOptions()
10         {
11             AddConverter("newfield", typeof(NewFieldConverter));
12             AddConverter("endrow", typeof(EndRowConverter));
13             base.ActivateOptions();
14         }
15 
16         public override void Format(TextWriter writer, LoggingEvent loggingEvent)
17         {
18             var ctw = new CsvTextWriter(writer);
19             ctw.WriteQuote();
20             base.Format(ctw, loggingEvent);
21         }
22     }
23 }

  在挥洒 log4net 的配备文件时,只要将 appender 的 layout 设置为
CoderBusy.Log4Net.Layout.CsvPatternLayout
,且设置好日志头,日志格式就可以。注意,header后供给编写制定换行字符,%new田野同志代表字段分隔符,%endrow代表风华正茂行甘休。

      <layout type="CoderBusy.Log4Net.Layout.CsvPatternLayout,CoderBusy.Log4Net">
        <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>

  我为初读书人希图了一个log4net的安排文件。这些布局文件会将日志在调控台和csv文件中同期输出,每日七个CSV文件(本地时间),且调整新北,不相同的日志等第会有不一致的颜料。 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 
 3 <configuration>
 4   <configSections>
 5     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
 6   </configSections>
 7   <log4net xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 8     <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
 9       <param name="File" value="Logs/" />
10       <param name="AppendToFile" value="True" />
11       <param name="MaxSizeRollBackups" value="10" />
12       <param name="StaticLogFileName" value="false" />
13       <param name="DatePattern" value="yyyy-MM-dd&quot;.csv&quot;" />
14       <param name="RollingStyle" value="Date" />
15       <layout type="CoderBusy.Log4Net.Layout.CsvPatternLayout,CoderBusy.Log4Net">
16         <header value="Time,Thread,Level,Logger,Message,Exception&#13;&#10;" />
17         <conversionPattern
18           value="%date{yyyy-MM-dd HH:mm:ss}%newfield%thread%newfield%level%newfield%logger%newfield%message%newfield%exception%endrow" />
19       </layout>
20     </appender>
21 
22     <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
23       <mapping>
24         <level value="ERROR" />
25         <foreColor value="Red" />
26       </mapping>
27       <mapping>
28         <level value="INFO" />
29         <foreColor value="Green" />
30       </mapping>
31 
32       <layout type="log4net.Layout.PatternLayout">
33         <conversionPattern value="# %date{HH:mm:ss} [%thread] %-5level %logger #%newline%message%newline" />
34       </layout>
35       <filter type="log4net.Filter.LevelRangeFilter">
36         <param name="LevelMin" value="DEBUG" />
37         <param name="LevelMax" value="FATAL" />
38       </filter>
39     </appender>
40 
41     <root>
42       <!-- OFF < FATAL < ERROR < WARN < INFO < DEBUG < ALL -->
43       <level value="ALL" />
44       <appender-ref ref="RollingLogFileAppender" />
45       <appender-ref ref="ColoredConsoleAppender" />
46     </root>
47   </log4net>
48 </configuration>

  合作以上配置,测量检验一下功用。

 1 using System;
 2 using log4net;
 3 using log4net.Config;
 4 
 5 [assembly: XmlConfigurator(ConfigFile = "log4net.config")]
 6 
 7 namespace CoderBusy.Log4Net.Tests
 8 {
 9     internal class Program
10     {
11         public static void Main(string[] args)
12         {
13             var log = LogManager.GetLogger("Default");
14             log.Debug("Message", new Exception("Test Exception"));
15             log.Info("Hello World.");
16             log.WarnFormat("A={0} B={1}", ""123123", Environment.NewLine);
17             Console.ReadLine();
18         }
19     }
20 }

Logs文件夹中,生成的csv文件内容如下:

Time,Thread,Level,Logger,Message,Exception
"2016-08-25 23:13:19","9","DEBUG","Default","Message","System.Exception: Test Exception
"
"2016-08-25 23:13:19","9","INFO","Default","Hello World.",""
"2016-08-25 23:13:19","9","WARN","Default","A=""123123 B=
",""

  输出字段被接收双引号包裹,且音信体中的双引号被重新输出。那样的结果注明大家的程序是常规的。本文的代码能够在
下载,多谢阅读,并祝你成功。

 

大家在编写程序时,会在程序运转进程中记录一些日志。log4net作为意气风发款经久耐用的日记组件,…

LtLogger的施用或然比较容易的,基本代码都以在Autoloader的条件下运作的,所以本身也懒的去援用神马路线,调用神马类库,知道怎么用就足以了。

$log=new LtLogger();

//本人定义log的贮存路线
$log->conf(‘log_file’ => ‘/log/system.log’);

$log->log(date(‘Y-m-d’卡塔尔(قطر‎.’ 加上当前景序名,操作人便是一条日志了’卡塔尔国;

挺轻巧的啊,假使文件本人子虚乌有,会直接开立在钦点的路径里,文件存在的话,会追加到日志里,私下认可每条日志以”t”来扩充私分。

发表评论

电子邮件地址不会被公开。 必填项已用*标注