最近在写一些C#的程序,想找一个记录日志的软件,当然会想起在java中大名鼎鼎的log4j,查了一下还真的发现有.net版的,就上网找了一些资料学了一下,把它用起来再说,用法很简单,只要把Log4Net.dll加入工程中,然后在App.config添加如下配置文件:
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->
1
<?
xml version="1.0" encoding="utf-8"
?>
2 < configuration >
3 < configSections >
4 < section name ="log4net" type ="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
5 </ configSections >
6 < log4net >
7 <!-- 定义输出到文件中 -->
8 < appender name ="LogFileAppender" type ="log4net.Appender.FileAppender" >
9 <!-- 定义文件存放位置 -->
10 < file value ="log.log" />
11 < layout type ="log4net.Layout.PatternLayout" >
12 <!-- 每条日志末尾的文字说明 -->
13 < footer value ="[Footer]--Test By Ring1981 " />
14 <!-- 输出格式
15 每种转换符号都以%开始,后面跟着一个格式符号和换符号。
16 %-数字 :该项的最小长度,小于最小长度的用空格填充
17 %m(message):输出的日志消息
18 %n(new line):换行
19 %d(datetime):输出当前语句运行的时刻
20 %r(run time):输出程序从运行到执行到当前语句时消耗的毫秒数
21 %t(thread id):当前语句所在的线程ID
22 %p(priority): 日志的当前优先级别,即DEBUG、INFO、WARN…等
23 %c(class):当前日志对象的名称,
24 %L(line ):输出语句所在的行号
25 %F(file name):输出语句所在的文件名
26 %logger 日志名称
27 -->
28 < conversionPattern value ="时间:%date [线程:%thread] %-5level [类:%c][%L行]%n %-5level:%message%newline" />
29 </ layout >
30 </ appender >
31 <!-- 定义输出到控制台命令行中 -->
32 < appender name ="ConsoleAppender" type ="log4net.Appender.ConsoleAppender" >
33 < layout type ="log4net.Layout.PatternLayout" >
34 <!-- conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" / -->
35 < conversionPattern value ="时间:%date [线程:%thread] %-5level [类:%c][%L行]%n %-5level:%message%newline" />
36 </ layout >
37 </ appender >
38 <!-- 定义输出到windows事件中 -->
39 < appender name ="EventLogAppender" type ="log4net.Appender.EventLogAppender" >
40 < layout type ="log4net.Layout.PatternLayout" >
41 < conversionPattern value ="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
42 </ layout >
43 </ appender >
44 <!-- RollingFileAppender是输出源也是输出到文件中,只是以自增长的方式 -->
45 < appender name ="RollingLogFileAppender" type ="log4net.Appender.RollingFileAppender" >
46 < param name ="File" value ="log/system_log_" />
47 <!-- 是否覆盖 -->
48 < param name ="AppendToFile" value ="true" />
49 <!-- 设置无限备份=-1 ,最大备份数为1000 -->
50 < param name ="MaxSizeRollBackups" value ="100" />
51 <!-- 每个文件的最大20k -->
52 < param name ="MaximumFileSize" value ="1KB" />
53 <!-- 名称是否可以更改 为false为可以更改 -->
54 < param name ="StaticLogFileName" value ="false" />
55 <!-- 文件名称 -->
56 < param name ="DatePattern" value ="yyyyMMdd".log"" />
57 < param name ="RollingStyle" value ="Date" />
58 < layout type ="log4net.Layout.PatternLayout" >
59 <!-- conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %SysMessage%newline" / -->
60 < conversionPattern value ="时间:%date [线程:%thread] %-5level [类:%c][%L行]%n %-5level:%message%newline" />
61 </ layout >
62 </ appender >
63 <!-- 定义日志的输出媒介,下面定义日志以四种方式输出。也可以下面的按照一种类型或其他类型输出。 -->
64 < root >
65 < appender-ref ref ="LogFileAppender" />
66 < appender-ref ref ="ConsoleAppender" />
67 < appender-ref ref ="EventLogAppender" />
68 < appender-ref ref ="RollingLogFileAppender" />
69 </ root >
70 </ log4net >
71 </ configuration >
72
2 < configuration >
3 < configSections >
4 < section name ="log4net" type ="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
5 </ configSections >
6 < log4net >
7 <!-- 定义输出到文件中 -->
8 < appender name ="LogFileAppender" type ="log4net.Appender.FileAppender" >
9 <!-- 定义文件存放位置 -->
10 < file value ="log.log" />
11 < layout type ="log4net.Layout.PatternLayout" >
12 <!-- 每条日志末尾的文字说明 -->
13 < footer value ="[Footer]--Test By Ring1981 " />
14 <!-- 输出格式
15 每种转换符号都以%开始,后面跟着一个格式符号和换符号。
16 %-数字 :该项的最小长度,小于最小长度的用空格填充
17 %m(message):输出的日志消息
18 %n(new line):换行
19 %d(datetime):输出当前语句运行的时刻
20 %r(run time):输出程序从运行到执行到当前语句时消耗的毫秒数
21 %t(thread id):当前语句所在的线程ID
22 %p(priority): 日志的当前优先级别,即DEBUG、INFO、WARN…等
23 %c(class):当前日志对象的名称,
24 %L(line ):输出语句所在的行号
25 %F(file name):输出语句所在的文件名
26 %logger 日志名称
27 -->
28 < conversionPattern value ="时间:%date [线程:%thread] %-5level [类:%c][%L行]%n %-5level:%message%newline" />
29 </ layout >
30 </ appender >
31 <!-- 定义输出到控制台命令行中 -->
32 < appender name ="ConsoleAppender" type ="log4net.Appender.ConsoleAppender" >
33 < layout type ="log4net.Layout.PatternLayout" >
34 <!-- conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" / -->
35 < conversionPattern value ="时间:%date [线程:%thread] %-5level [类:%c][%L行]%n %-5level:%message%newline" />
36 </ layout >
37 </ appender >
38 <!-- 定义输出到windows事件中 -->
39 < appender name ="EventLogAppender" type ="log4net.Appender.EventLogAppender" >
40 < layout type ="log4net.Layout.PatternLayout" >
41 < conversionPattern value ="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
42 </ layout >
43 </ appender >
44 <!-- RollingFileAppender是输出源也是输出到文件中,只是以自增长的方式 -->
45 < appender name ="RollingLogFileAppender" type ="log4net.Appender.RollingFileAppender" >
46 < param name ="File" value ="log/system_log_" />
47 <!-- 是否覆盖 -->
48 < param name ="AppendToFile" value ="true" />
49 <!-- 设置无限备份=-1 ,最大备份数为1000 -->
50 < param name ="MaxSizeRollBackups" value ="100" />
51 <!-- 每个文件的最大20k -->
52 < param name ="MaximumFileSize" value ="1KB" />
53 <!-- 名称是否可以更改 为false为可以更改 -->
54 < param name ="StaticLogFileName" value ="false" />
55 <!-- 文件名称 -->
56 < param name ="DatePattern" value ="yyyyMMdd".log"" />
57 < param name ="RollingStyle" value ="Date" />
58 < layout type ="log4net.Layout.PatternLayout" >
59 <!-- conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %SysMessage%newline" / -->
60 < conversionPattern value ="时间:%date [线程:%thread] %-5level [类:%c][%L行]%n %-5level:%message%newline" />
61 </ layout >
62 </ appender >
63 <!-- 定义日志的输出媒介,下面定义日志以四种方式输出。也可以下面的按照一种类型或其他类型输出。 -->
64 < root >
65 < appender-ref ref ="LogFileAppender" />
66 < appender-ref ref ="ConsoleAppender" />
67 < appender-ref ref ="EventLogAppender" />
68 < appender-ref ref ="RollingLogFileAppender" />
69 </ root >
70 </ log4net >
71 </ configuration >
72
然后在AssemblyInfo.cs文件最后加入[assembly: log4net.Config.DOMConfigurator()]即可用了
示例:
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->
1
class
Program
2 {
3 private static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
4
5 static void Main( string [] args)
6 {
7 log.Debug( " 测试日志 " );
8 log.Info( " 测试日志 " );
9 log.Warn( " 测试日志 " );
10 Console.Read();
11 }
12 }
2 {
3 private static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
4
5 static void Main( string [] args)
6 {
7 log.Debug( " 测试日志 " );
8 log.Info( " 测试日志 " );
9 log.Warn( " 测试日志 " );
10 Console.Read();
11 }
12 }
运行效果: