使用log4net记录日志


使用Log4net记录日志

第一步:下载Log4net.dll,添加引用

第二步:新建一个log4net.config,放到bin目录

配置文件代码

  1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3 <configSections>
4 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
5 </configSections>
6
7 <log4net>
8 <appender name="DebugLogFile" type="log4net.Appender.RollingFileAppender,log4net" >
9 <param name="debug" value="true" />
10 <param name="File" value="./Log/" />
11 <param name="AppendToFile" value="true" />
12 <param name="RollingStyle" value="Date" />
13 <param name="DatePattern" value="yyyyMMdd" />
14 <param name="StaticLogFileName" value="false" />
15 <datePattern value="yyyy-MM-dd&quot;-Debug.log&quot;" />
16
17 <layout type="log4net.Layout.PatternLayout,log4net">
18 <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
19 <param name="Header" value="&#13;&#10;----------------------header--------------------------&#13;&#10;" />
20 <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" />
21 </layout>
22 <filter type="log4net.Filter.LevelRangeFilter">
23 <param name="LevelMin" value="DEBUG" />
24 <param name="LevelMax" value="DEBUG" />
25 </filter>
26 </appender>
27
28 <!--信息日志输入格式-->
29 <appender name="InfoLogFile" type="log4net.Appender.RollingFileAppender,log4net" >
30 <param name="debug" value="true" />
31 <!--日志文件保存路径(以此配置文件位置为基准)-->
32 <param name="File" value="./Log/" />
33 <!--是否追加到日志文件-->
34 <param name="AppendToFile" value="true" />
35 <!--分隔类型,日期-->
36 <param name="RollingStyle" value="Date" />
37 <param name="DatePattern" value="yyyyMMdd" />
38 <param name="StaticLogFileName" value="false" />
39 <!--日志文件名称(此处使用.log为扩展名,可为.txt)(StaticLogFileName设置为False时使用自己设定的文件名)-->
40 <datePattern value="yyyy-MM-dd&quot;-Info.log&quot;" />
41
42 <!--布局-->
43 <layout type="log4net.Layout.PatternLayout,log4net">
44 <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
45 <param name="Header" value="&#13;&#10;----------------------header--------------------------&#13;&#10;" />
46 <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" />
47 </layout>
48 <!--过滤-->
49 <filter type="log4net.Filter.LevelRangeFilter">
50 <param name="LevelMin" value="INFO" />
51 <param name="LevelMax" value="INFO" />
52 </filter>
53 </appender>
54
55 <appender name="WarnLogFile" type="log4net.Appender.RollingFileAppender,log4net" >
56 <param name="debug" value="true" />
57 <param name="File" value="./Log/" />
58 <param name="AppendToFile" value="true" />
59 <param name="RollingStyle" value="Date" />
60 <param name="DatePattern" value="yyyyMMdd" />
61 <param name="StaticLogFileName" value="false" />
62 <datePattern value="yyyy-MM-dd&quot;-Warn.log&quot;" />
63
64 <layout type="log4net.Layout.PatternLayout,log4net">
65 <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
66 <param name="Header" value="&#13;&#10;----------------------header--------------------------&#13;&#10;" />
67 <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" />
68 </layout>
69 <filter type="log4net.Filter.LevelRangeFilter">
70 <param name="LevelMin" value="WARN" />
71 <param name="LevelMax" value="WARN" />
72 </filter>
73 </appender>
74
75 <appender name="ErrorLogFile" type="log4net.Appender.RollingFileAppender,log4net" >
76 <param name="debug" value="true" />
77 <param name="File" value="./Log/" />
78 <param name="AppendToFile" value="true" />
79 <param name="RollingStyle" value="Date" />
80 <param name="DatePattern" value="yyyyMMdd" />
81 <param name="StaticLogFileName" value="false" />
82 <datePattern value="yyyy-MM-dd&quot;-Error.log&quot;" />
83
84 <layout type="log4net.Layout.PatternLayout,log4net">
85 <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
86 <param name="Header" value="&#13;&#10;----------------------header--------------------------&#13;&#10;" />
87 <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" />
88 </layout>
89 <filter type="log4net.Filter.LevelRangeFilter">
90 <param name="LevelMin" value="ERROR" />
91 <param name="LevelMax" value="ERROR" />
92 </filter>
93 </appender>
94
95 <appender name="FatalLogFile" type="log4net.Appender.RollingFileAppender,log4net" >
96 <param name="debug" value="true" />
97 <param name="File" value="./Log/" />
98 <param name="AppendToFile" value="true" />
99 <param name="RollingStyle" value="Date" />
100 <param name="DatePattern" value="yyyyMMdd" />
101 <param name="StaticLogFileName" value="false" />
102 <datePattern value="yyyy-MM-dd&quot;-Fatal.log&quot;" />
103
104 <layout type="log4net.Layout.PatternLayout,log4net">
105 <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
106 <param name="Header" value="&#13;&#10;----------------------header--------------------------&#13;&#10;" />
107 <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" />
108 </layout>
109 <filter type="log4net.Filter.LevelRangeFilter">
110 <param name="LevelMin" value="FATAL" />
111 <param name="LevelMax" value="FATAL" />
112 </filter>
113 </appender>
114
115 <!--将日志写入数据库-->
116 <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
117 <!--当日志到达100条时写入数据库,关闭时未到达100也会写入数据库-->
118 <bufferSize value="100" />
119 <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
120 <connectionString value="data source=[database server];initial catalog=[database name];integrated security=false;persist security info=True;User ID=[user];Password=[password]" />
121 <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
122 <parameter>
123 <parameterName value="@log_date" />
124 <dbType value="DateTime" />
125 <layout type="log4net.Layout.RawTimeStampLayout" />
126 </parameter>
127 <parameter>
128 <parameterName value="@thread" />
129 <dbType value="String" />
130 <size value="255" />
131 <layout type="log4net.Layout.PatternLayout">
132 <conversionPattern value="%thread" />
133 </layout>
134 </parameter>
135 <parameter>
136 <parameterName value="@log_level" />
137 <dbType value="String" />
138 <size value="50" />
139 <layout type="log4net.Layout.PatternLayout">
140 <conversionPattern value="%level" />
141 </layout>
142 </parameter>
143 <parameter>
144 <parameterName value="@logger" />
145 <dbType value="String" />
146 <size value="255" />
147 <layout type="log4net.Layout.PatternLayout">
148 <conversionPattern value="%logger" />
149 </layout>
150 </parameter>
151 <parameter>
152 <parameterName value="@message" />
153 <dbType value="String" />
154 <size value="4000" />
155 <layout type="log4net.Layout.PatternLayout">
156 <conversionPattern value="%message" />
157 </layout>
158 </parameter>
159 <parameter>
160 <parameterName value="@exception" />
161 <dbType value="String" />
162 <size value="2000" />
163 <layout type="log4net.Layout.ExceptionLayout" />
164 </parameter>
165 </appender>
166 <logger name="logApp">
167 <level value="ALL" />
168 </logger>
169 <root>
170 <!--设置记录哪些日志-->
171 <level value="ALL" />
172 <!--appender-ref ref="DebugLogFile" /-->
173 <appender-ref ref="InfoLogFile" />
174 <!--appender-ref ref="WarnLogFile" /-->
175 <appender-ref ref="ErrorLogFile" />
176 <!--appender-ref ref="FatalLogFile" /-->
177 <appender-ref ref="AdoNetAppender" />
178 </root>
179
180 </log4net>
181 </configuration>

第三步:引用配置文件

在Properties----AssemblyInfo.cs中添加[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

1 [assembly: AssemblyVersion("1.0.0.0")]
2 [assembly: AssemblyFileVersion("1.0.0.0")]
3 [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

第四步:使用

 

 1 public class PublicRunTask
2 {
3 //使用类名
4 private log4net.ILog _log = log4net.LogManager.GetLogger("PublicRunTask");
5 //
6 //private log4net.ILog _log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType);
7 public void test()
8 {
9 try
10 {
11 //复杂操作代码 此处略
12
13 //记录(如果设置了记录添加到数据库,会在程序关闭或到达指定条数时自动添加到数据库)
14 _log.Info("我是执行信息");
15 }
16 catch (Exception ex)
17 {
18 _log.Error("下载评价发生异常", ex);
19 }
20 }

注:如果需要将记录添加到Sql数据库,创建数据库表

1 CREATE TABLE [dbo].[Log] (
2 [Id] [int] IDENTITY (1, 1) NOT NULL,
3 [Date] [datetime] NOT NULL,
4 [Thread] [varchar] (255) NOT NULL,
5 [Level] [varchar] (50) NOT NULL,
6 [Logger] [varchar] (255) NOT NULL,
7 [Message] [varchar] (4000) NOT NULL,
8 [Exception] [varchar] (2000) NULL
9 )


posted @ 2012-01-09 18:39  only_copy  阅读(296)  评论(0编辑  收藏  举报